Review of Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Review of Inheritance

Description:

void print() {cout 'D'} overridden. which one is it? D *ptr ... print is not overridden in D1. 16. Virtual Destructors ! Base Class B ~B(); Derived class D ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 20
Provided by: cristia93
Category:

less

Transcript and Presenter's Notes

Title: Review of Inheritance


1
Review of Inheritance
2
Several Levels of Inheritance
Base Class B
Derived class D
Derived class D1
3
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
4
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
5
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
6
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
7
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
8
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
9
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

10
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

11
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

12
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)

13
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
14
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
15
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
16
Virtual Destructors !
Base Class B
B()
Derived class D
D()
Derived class D1
which destructor
is called ?
D1()
D ptr new D1 Delete ptr
17
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
18
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
19
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