Multiple Inheritance - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Multiple Inheritance

Description:

A C idiom to addressing name ambiguity.... Professor. getDepartment () Student. getDepartment ... Idiom Con't. Class ProfessorParent : public Professor { public: ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 15
Provided by: JA
Category:

less

Transcript and Presenter's Notes

Title: Multiple Inheritance


1
Multiple Inheritance
  • Fall 2005 OOPD
  • John Anthony

2
The Problem
Male
North American
Parent
Professor
Author
3
The (Conceptual) Solution
By breaking down the hierarchy, each abstract
type can be combined to create valid is-a
Relationships.
4
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.

5
Consider
Professor
Student
getDepartment ()
getDepartment ()
Name Ambiguity Which getDepartment() is Student
going to inherit?
TeacherAssistant
getDepartment ()
6
Resolving Name Ambiguity
Force the client of the child class to resolve
the ambiguity
  • TeacherAssistant assistant
  • assistant-gtProfessorgetDepartment()
  • assistant-gtStudentgetDepartment()

7
Resolving Name Ambiguity
If the signatures are different (and overloading
supported) then child class can simply include
both methods.
8
Resolving Name Ambiguity
If signatures are similar or if overloading not
allowed, then programmer may override one method
and rename another.
  • Class TeachingAssistant public Professor,
    public Student
  • public
  • virtual Department getTeachingDepartment()
  • return ProfessorgetDepartment()
  • virtual Department getDepartment()
  • return StudentgetDepartment()

What about polymorphism?
Professor p new TeachAssistant() p-gtgetDepart
ment() //returns the Department the TA is
studying in rather than teaching in.
9
Resolving Name Ambiguity
A C idiom to addressing name ambiguity.
Professor
Student
getDepartment ()
getDepartment ()
ProfessorParent
StudentParent
getDepartment ()
getDepartment ()
getMajorDepartment()
getTeachingDepartment()
TeacherAssistant
getTeachingDepartment()
getMajorDepartment()
10
Idiom Cont.
  • Class ProfessorParent public Professor
  • public
  • virtual Department getDepartment()
  • return getTeachingDepartment()
  • virtual Department getTeachingDepartment()
  • return Professor getDepartment()

11
Resolving Name Ambiguity
  • TeachingAssistant ta new TeachingAssistant()
  • Professor prof ta
  • Student student ta
  • prof-gtgetDepartment() //OK, will execute
    getTeachingDepartment
  • Student-gtgetDepartment() //OK, will execute
    getMajorDepartment
  • ta-gtgetDepartment() //compiler error, ambiguous
    invocation

12
Redefinition in Eiffel
  • cClass TeachingAssistant
  • inherit
  • Professor
  • rename
  • getDepartment as getTeachingDepartment
  • end
  • Student
  • rename
  • getDepartment as getMajorDepartment
  • end

13
Common Ancestors
A
A
A
B
C
B
C
D
D
14
Common Ancestors
Person
String title
the diamond of death
Professor
Student
String title
String title
TeacherAssistant
Should there be two titles or one?
Write a Comment
User Comments (0)
About PowerShow.com