Title: Review of Inheritance
1Review of Inheritance
2Several Levels of Inheritance
Base Class B
Derived class D
Derived class D1
3Type Conversions
Base Class B
B b //is not of type D or D1
Derived class D
D d //is also of type B
Derived class D1
D1 d1 //is also of type D, B
4Assignment b d
Base Class B
Derived class D
D d //is also of type B
Derived class D1
D1 d1 //is also of type D, B
5Assignment d b
Base Class B
B b //is not of type D
Derived class D
D d //is also of type B
Derived class D1
D1 d1 //is also of type D, B
6Function calls
Base Class B
void print() cout ltlt B
Derived class D
D d //is also of type B
void print() Baseprint()
Derived class D1
7Function calls
Base Class B
B b //is not of type D
void print() Dprint()
Derived class D
D d //is also of type B
void print() Baseprint()
Derived class D1
D1 d1 //is also of type D, B
8Use of Virtual (Dynamic Binding)
- To enable programming with pointers to different
object - types
- The compiler generates code to
- inspect the type of the object the pointer
points to at run- time and then call the
appropriate function
D
D1
9Use of Virtual (Dynamic Binding)
- D ptr new D1()
- ptr?f() //dynamic binding calls f from D1
- For dynamic binding to occur for function f
- - must use pointers or references
- - f must exist in D
- - f must be declared virtual in D
-
10Use of Virtual (Dynamic Binding)
- D d
- d.f() //static binding calls f from D
- For dynamic binding to occur for function f
- - must use pointers or references
- - f must exist in D
- - f must be declared virtual in D
-
11Use of Virtual (Dynamic Binding)
- D ptr new D1()
- ptr?print() //dynamic binding calls print from
D1 - For dynamic binding to occur for function print
- - must use pointers or references
- - print must exist in D (could be implemented or
pure - virtual)
- - print must be declared virtual in D
-
12Use of Virtual (Dynamic Binding)
- D ptr new D1()
- ptr?print() //dynamic binding calls print from
D1 - Make sure you go through the steps
- Find type of ptr (D here)
- Look in that class (D here) for the function
(print) - If print - is not there (neither declared in D
nor inherited - from B) ? compiler error !
- - is there and is virtual (either declared
virtual explicitly - or inherited from B) ? dynamic binding (print
from D1) - - is there and not virtual ? static binding
(print from D)
13Inheritance with Virtual Functions
Base Class B
virtual void print()cout ltltB
Derived class D
not overriden
Derived class D1
void print()cout ltltD1 which print() is
called?
D ptr new D1 ptr-gtprint() D1 Dynamic
binding
14Inheritance with Virtual Functions
Base Class B
virtual void print()cout ltltB
Derived class D
not overriden
Derived class D1
not overriden
which one is
it then ?
Dynamic binding occurs but print is not overriden
in D1, it is inherited as is from B
D ptr new D1 ptr-gtprint() B the one
closest in the hierarchy
15Inheritance with Virtual Functions
Base Class B
void print()cout ltltB
Derived class D
void print() cout ltlt D overridden
Derived class D1
which one is it?
Dynamic binding occurs but print is not
overridden in D1
D ptr new D1 ptr-gtprint() -- D the one
closest in the hierarchy
16Virtual Destructors !
Base Class B
B()
Derived class D
D()
Derived class D1
which destructor
is called ?
D1()
D ptr new D1 Delete ptr
17Virtual Destructors !
Base Class B
B()
Derived class D
D()
Derived class D1
which destructor
is called ?
D1()
Static binding occurs, because D is not virtual
D ptr new D1 Delete ptr //D and B called
18Virtual Destructors !
Base Class B
B()
Derived class D
virtual D()
Derived class D1
which destructor
is called ?
D1()
Dynamic binding occurs, because D is virtual
D ptr new D1 Delete ptr //D1, D and B
19Virtual Destructors !
Base Class B
virtual B()
Derived class D
D()
Derived class D1
which destructor
is called ?
D1()
D ptr new D1 Delete ptr //D1, D and B