Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance & Destructors

Similar presentations


Presentation on theme: "Inheritance & Destructors"— Presentation transcript:

1 Inheritance & Destructors

2 Constructing/Destructing
Object built/destroyed in layers Base class constructed Derived class constructed Geometric Object Circle

3 Constructing/Destructing
Object built/destroyed in layers Base class constructed Derived class constructed Derived class destroyed – clean up after itself Geometric Object

4 Constructing/Destructing
Object built/destroyed in layers Base class constructed Derived class constructed Derived class destroyed – clean up after itself Base class destroyed – clean up after itself

5 Example Simple list has array of ints
LessSimpleList is a SimpleList with one extra char on heap Each new has a matching delete

6 Example Base created Derived created Derived destroyed Base destroyed 1 5 3 2 4

7 Example Base created Derived created Derived destroyed Base destroyed 1 5 3 2 4

8 Copy Constructor Issue
No copy constructor = trouble:

9 Copy Constructor Issue
Copying child class defaults to no-arg construction of parent:

10 Copy Constructor Fix Child copy ctor should explicitly call parent one

11 Assignment Operator Assignment operator also needs to call parent version:

12 Virtual Messiness Also need to worry about virtual dispatch to handle: SimpleList* list = new LessSimpleList(); delete list; Fixes: Virtual destructor Child classes need to override parent Copy/Assignment


Download ppt "Inheritance & Destructors"

Similar presentations


Ads by Google