Destruction and Copying

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Introduction to Programming Lecture 39. Copy Constructor.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Classes and Objects: Chapter 8, Slide 1 Object and its encapsulation.
CSE 332: C++ copy control I Copy Control (Part I) Copy control consists of 5 distinct operations –A copy constructor initializes an object by duplicating.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Csi2172 class 5 Midterm: June 12. constructor Special method used to create objects of the class Never has a return type. Is called automatically upon.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
LECTURE LECTURE 11 Constructors and destructors Copy constructor Textbook: p , 183.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Container Classes. How do we do better? Tough to delete correctly: startLocation.
On dynamic memory and classes ● We previously had only discussed dynamic memory in regards to structs and dynamic arrays. ● However, they can be used (to.
Memory Management.
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Copy Constructor / Destructors Stacks and Queues
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
C++ Memory Management Idioms
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Automatics, Copy Constructor, and Assignment Operator
Classes with Dynamically Allocated Data
understanding memory usage by a c++ program
Automatics, Copy Constructor, and Assignment Operator
Constructors and destructors
Constructors and Destructors
Copy Assignment CSCE 121 J. Michael Moore.
Indirection.
Inheritance & Destructors
CISC/CMPE320 - Prof. McLeod
Dynamic Memory Management
Essential Class Operations
Destructor CSCE 121 J. Michael Moore.
Destructor CSCE 121.
Review Chapter 10 PPT for full coverage.
Destruction and Copying
COP 3330 Object-oriented Programming in C++
Inheritance & Destructors
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
Essential Class Operations
Copy Assignment CSCE 121.
Dynamic Memory Management
Copy Constructor CSCE 121.
Destructors, Copy Constructors & Copy Assignment Operators
Inheritance & Destructors
Rule of Three Part 1 & 2.
Destructors, Copy Constructors & Copy Assignment Operators
Rule Of Three Part 3.
SPL – PS3 C++ Classes.
Presentation transcript:

Destruction and Copying

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

Destructors Stack objects destroyed when leave scope:

Destructors Heap objects must be destroyed manually:

Cleaning Up Memory allocated by object = potential leak

Cleaning Up Memory allocated by object = potential leak

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

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

Copy Constructor Using copy constructor: p2 is copying p1

The Issue Default copy constructor for Polygon

The Issue Default copy constructor for Polygon

The Issue Default copy constructor for Polygon

The Issue Default copy constructor for Polygon

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

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

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

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

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

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

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

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

Book Sample

Book Example Course maintains a list of student names:

Book Example Course.h

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