Presentation is loading. Please wait.

Presentation is loading. Please wait.

Destruction and Copying

Similar presentations


Presentation on theme: "Destruction and Copying"— Presentation transcript:

1 Destruction and Copying

2 Destructor Destructor : Special function called when object destroyed
Automatically called Almost never called by hand ~CLASSNAME()

3 Destructors Stack objects destroyed when leave scope:

4 Destructors Heap objects must be destroyed manually:

5 Cleaning Up Memory allocated by object = potential leak

6 Cleaning Up Memory allocated by object = potential leak

7 Destructor Proper place to clean up memory object owns:
New in constructor  delete in destructor

8 Copy Constructor Every class has a default implicit copy constructor
Class::Class (const Class &other) Created invisibly and automatically

9 Copy Constructor Using copy constructor: p2 is copying p1

10 The Issue Default copy constructor for Polygon

11 The Issue Default copy constructor for Polygon

12 The Issue Default copy constructor for Polygon

13 The Issue Default copy constructor for Polygon

14 Shallow copy Shallow copy : duplicate pointers of an object
Copy shares memory with original

15 Deep copy Deep copy : duplicate memory owned by original
Copy gets own version of data

16 Copy Constructor Deep copy constructor Allocate own storage
Copy data to it

17 Copy Constructor Deep copy constructor Allocate own storage
Copy data to it

18 Copy Constructor Deep copy constructor Allocate own storage
Copy data to it

19 Copy Constructor Deep copy constructor Allocate own storage
Copy data to it

20 Copy Constructor Deep copy constructor Allocate own storage
Copy data to it

21 Copy Constructor Deep copy constructor Allocate own storage
Copy data to it

22 Book Sample

23 Book Example Course maintains a list of student names:

24 Book Example Course.h

25 Book Example Book Ex11.20 copy constructor
Has typo – missing last 2 lines in book Should be:


Download ppt "Destruction and Copying"

Similar presentations


Ads by Google