140 likes | 242 Views
Multiple Inheritance. Fall 2005 OOPD John Anthony. The Problem. Male. North American. Parent. Professor. Author. Male. North American. Parent. Professor. Author. The (Conceptual) Solution.
E N D
Multiple Inheritance Fall 2005 OOPD John Anthony
The Problem Male North American Parent Professor Author John J. Anthony
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
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
Consider… Professor Student getDepartment (…) getDepartment (…) Name Ambiguity Which getDepartment(…) is Student going to inherit? TeacherAssistant getDepartment (…) John J. Anthony
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
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
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
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
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
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
Redefinition in Eiffel cClass TeachingAssistant inherit Professor rename getDepartment as getTeachingDepartment end Student rename getDepartment as getMajorDepartment end John J. Anthony
Common Ancestors A A A B C B C D D John J. Anthony
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