1 / 14

Multiple Inheritance

Multiple Inheritance. Fall 2005 OOPD John Anthony. The Problem. Male. North American. Parent. Professor. Author. Male. North American. Parent. Professor. Author. The (Conceptual) Solution.

Download Presentation

Multiple Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Multiple Inheritance Fall 2005 OOPD John Anthony

  2. The Problem Male North American Parent Professor Author John J. Anthony

  3. Male North American Parent Professor Author The (Conceptual) Solution By breaking down the hierarchy, each abstract type can be combined to create valid “is-a” Relationships. John J. Anthony

  4. Parent Professor North American Male Author Multiple Inheritance • 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…. John J. Anthony

  5. Consider… Professor Student getDepartment (…) getDepartment (…) Name Ambiguity Which getDepartment(…) is Student going to inherit? TeacherAssistant getDepartment (…) John J. Anthony

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

  7. Professor Student getDepartment (int majorCode) getDepartment () TeachingAssistant getDepartment () getDepartment (int majorCode) Resolving Name Ambiguity If the signatures are different (and overloading supported) then child class can simply include both methods. John J. Anthony

  8. 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. John J. Anthony

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

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

  11. Resolving Name Ambiguity 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 John J. Anthony

  12. Redefinition in Eiffel cClass TeachingAssistant inherit Professor rename getDepartment as getTeachingDepartment end Student rename getDepartment as getMajorDepartment end John J. Anthony

  13. Common Ancestors A A A B C B C D D John J. Anthony

  14. Common Ancestors Person String *title; …the diamond of death Professor Student String *title; String *title; TeacherAssistant Should there be two titles or one? John J. Anthony

More Related