Review%20of%20Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Review%20of%20Inheritance

Description:

If print - is not there (neither declared in D nor inherited. from B) compiler error ! ... or inherited from B) dynamic binding (print from D1) ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 30
Provided by: cristia93
Category:

less

Transcript and Presenter's Notes

Title: Review%20of%20Inheritance


1
Review of Inheritance
2
Inheritance Hierarchy
Base Class B
Derived class D public B
Private Protected Public
3
Inheritance Access Types
Base Class B
Derived class D public B
Inherited but cannot access private members of B
within D
Private Protected Public
4
Inherited Member Access
Base Class B
Derived class D public B
Can access protected members of B within D
Private Protected Public
5
Inherited 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
6
Several Levels of Inheritance
Base Class B
Derived class D
Derived class D1
7
Constructors
Base Class B
B b
Derived class D
D d
Derived class D1
D1 d1 // constructors for //B, D, D1 called
8
Which Constructor ?
  • The default constructor.
  • If you want another one use initializer lists
  • D D(int m, int n) B(int n)

9
Inheriting 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()
10
Inheriting 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
11
Inheriting 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()
12
Inheriting 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
13
Type 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
14
Assignment 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
15
Assignment 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
16
Function 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
17
Function 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
18
Use 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
19
Use 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

20
Use 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

21
Use 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

22
Use 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)

23
Inheritance 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
24
Inheritance 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
25
Inheritance 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
26
Virtual Destructors !
Base Class B
B()
Derived class D
D()
Derived class D1
which destructor
is called ?
D1()
D ptr new D1 Delete ptr
27
Virtual 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
28
Virtual 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
29
Virtual 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
Write a Comment
User Comments (0)
About PowerShow.com