Rule of Three Part 1 & 2.

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.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
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.
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.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Copy Constructors Fall 2008 Dr. David A. Gaitros
1 Inside the Vector Class: with additional needed methods CPS212CPS212 Gordon College.
The Big Three Based on Weiss “Data Structures and algorithm Analysis CS240 Computer Science II.
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.
The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review 2.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
1 Ugly Realities The Dark Side of C++ Chapter 12.
@ George Wolberg, CSC212 Data Structure Lecture 6 Dynamic Classes and the Law of the Big Three Instructor: George Wolberg Department of Computer.
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.
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
Memory Management with Classes
Copy Constructor / Destructors Stacks and Queues
C++ Memory Management Idioms
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamic Memory Review what is static, automatic, dynamic variables? why are dynamic(ally allocated) variables needed what is program stack? function.
Array Lists Chapter 6 Section 6.1 to 6.3
Automatics, Copy Constructor, and Assignment Operator
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Automatics, Copy Constructor, and Assignment Operator
The Assignment Operator
Constructors and destructors
Copy Assignment CSCE 121 J. Michael Moore.
Destruction and Copying
Indirection.
Inheritance & Destructors
CISC/CMPE320 - Prof. McLeod
Dynamic Memory Management
Essential Class Operations
Destructor CSCE 121 J. Michael Moore.
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
Destructor CSCE 121.
CSC212 Data Structure - Section RS
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
Destructors, Copy Constructors & Copy Assignment Operators
Inheritance & Destructors
Rule of Five.
Destructors, Copy Constructors & Copy Assignment Operators
9-10 Classes: A Deeper Look.
Rule Of Three Part 3.
Copy Constructors and Overloaded Assignment
Issues with classes.
SPL – PS3 C++ Classes.
Presentation transcript:

Rule of Three Part 1 & 2

Rule of Three Any object which manages memory needs: Custom Destructor Custom Copy Constructor Custom Assignment Operator

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 Destructor used to clean up owned resources: 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

Copy Constructor Default works for simple objects: p1 p2 x: 4 y: 3

The Issue Default copy constructor for Polygon

The Issue Copies sides

The Issue Copies vertices pointer

The Issue Two objects now share one list of points

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 Copy basic variables Allocate own storage Copy data to it

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

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

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

Book Sample

Book Example Course class introduced in Ch 11

Book Example Constructor allocates array Destructor deletes

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

Copy Constructor Deep copy recipe: Copy basic variables Allocate own storage/resources Copy data from other to own storage

Copy Constructor Recipee For Course: