Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Multiple Inheritance Fall 2005 OOPD John Anthony.

Similar presentations


Presentation on theme: "1 Multiple Inheritance Fall 2005 OOPD John Anthony."— Presentation transcript:

1 1 Multiple Inheritance Fall 2005 OOPD John Anthony

2 2John J. Anthony Object Oriented Programming & Design The Problem Male North American Parent Professor Author

3 3John J. Anthony Object Oriented Programming & Design The (Conceptual) Solution Male North AmericanParentProfessor Author By breaking down the hierarchy, each abstract type can be combined to create valid “is-a” Relationships.

4 4John J. Anthony Object Oriented Programming & Design Multiple Inheritance MaleNorth AmericanParentProfessorAuthor An Author “is-a” Male and “is-a” North American and…. An Author can be viewed “as-a” Male and “as-a” North American and….

5 5John J. Anthony Object Oriented Programming & Design Consider… ProfessorStudent getDepartment (…) TeacherAssistant getDepartment (…) Name Ambiguity Which getDepartment(…) is Student going to inherit?

6 6John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity TeacherAssistant assistant; assistant->Professor::getDepartment();assistant->Student::getDepartment(); Force the client of the child class to resolve the ambiguity…

7 7John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity If the signatures are different (and overloading supported) then child class can simply include both methods. ProfessorStudent getDepartment () getDepartment (int majorCode) TeachingAssistant getDepartment () getDepartment (int majorCode)

8 8John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity Class TeachingAssistant : public Professor, public Student { public: virtual Department * getTeachingDepartment() { return Professor::getDepartment(); } virtual Department * getDepartment() { return Student::getDepartment(); }} If signatures are similar or if overloading not allowed, then programmer may override one method and rename another. What about polymorphism? Professor * p = new TeachAssistant(); p->getDepartment(); //returns the Department the TA is studying in rather than teaching in.

9 9John J. Anthony Object Oriented Programming & Design Resolving Name Ambiguity A C++ idiom to addressing name ambiguity…. Professor getDepartment () Student getDepartment () ProfessorParent getDepartment () StudentParent getDepartment () TeacherAssistant getTeachingDepartment() getMajorDepartment()

10 10John J. Anthony Object Oriented Programming & Design Idiom Con’t. Class ProfessorParent : public Professor { public: virtual Department * getDepartment() { return getTeachingDepartment(); } virtual Department * getTeachingDepartment() { return Professor ::getDepartment(); }} Professor getDepartment () ProfessorParent getDepartment () getTeachingDepartment()

11 11John J. Anthony Object Oriented Programming & Design TeachingAssistant * ta = new TeachingAssistant(); Professor *prof = ta; Student *student = ta; prof->getDepartment(); //OK, will execute getTeachingDepartment Student->getDepartment(); //OK, will execute getMajorDepartment ta->getDepartment(); //compiler error, ambiguous invocation Resolving Name Ambiguity

12 12John J. Anthony Object Oriented Programming & Design Redefinition in Eiffel cClass TeachingAssistant inheritProfessorrename getDepartment as getTeachingDepartment endStudentrename getDepartment as getMajorDepartment end

13 13John J. Anthony Object Oriented Programming & Design Common Ancestors A BC D AA BC D

14 14John J. Anthony Object Oriented Programming & Design Common Ancestors ProfessorStudent TeacherAssistant Person String *title; …the diamond of death Should there be two titles or one?


Download ppt "1 Multiple Inheritance Fall 2005 OOPD John Anthony."

Similar presentations


Ads by Google