Title: Review%20of%20Inheritance
1Review of Inheritance
2Inheritance Hierarchy
Base Class B
Derived class D public B
Private Protected Public
3Inheritance Access Types
Base Class B
Derived class D public B
Inherited but cannot access private members of B
within D
Private Protected Public
4Inherited Member Access
Base Class B
Derived class D public B
Can access protected members of B within D
Private Protected Public
5Inherited Member Access
Base Class B
Derived class D public B
Can access protected members of B within
D, cannot access these in main()
Private Protected Public
D obj Obj.protected_member
6Several Levels of Inheritance
Base Class B
Derived class D
Derived class D1
7Constructors
Base Class B
B b
Derived class D
D d
Derived class D1
D1 d1 // constructors for //B, D, D1 called
8Which Constructor ?
- The default constructor.
- If you want another one use initializer lists
- D D(int m, int n) B(int n)
9Inheriting and Overriding Functions
Base Class B
public void print()cout ltltB
Derived class D
void print() cout ltlt D overriden
Derived class D1
D1 obj obj.print()
10Inheriting and Overriding Functions
Base Class B
void print()cout ltltB
Derived class D
void print() cout ltlt D overriden
Derived class D1
D1 obj obj.print() -- D the one closest in
the hierarchy
11Inheriting and Overriding Functions
Base Class B
void print()cout ltltB
Derived class D
not overriden
Derived class D1
which one is it?
D1 obj obj.print()
12Inheriting and Overriding Functions
Base Class B
void print()cout ltltB
Derived class D
not overriden
Derived class D1
which one is it?
D1 obj obj.print() B the one closest in the
hierarchy
13Type 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
14Assignment 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
15Assignment 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
16Function 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
17Function 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
18Use 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
19Use 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
-
20Use 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
-
21Use 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
-
22Use 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)
23Inheritance 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
24Inheritance 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
25Inheritance 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
26Virtual Destructors !
Base Class B
B()
Derived class D
D()
Derived class D1
which destructor
is called ?
D1()
D ptr new D1 Delete ptr
27Virtual 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
28Virtual 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
29Virtual 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