Copy Constructors Fall 2008 Dr. David A. Gaitros

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
1 Class Constructors a class constructor is a member function whose purpose is to initialize the private data members of a class object the name of a constructor.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Virtual Functions Fall 2008 Dr. David A. Gaitros
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
CSE 333 – SECTION 4. Overview Pointers vs. references Const Classes, constructors, new, delete, etc. More operator overloading.
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.
CSCI 383 Object-Oriented Programming & Design Lecture 13 Martin van Bommel.
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
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.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
More C++ Features True object initialisation
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Chapter 9 Classes: A Deeper Look, Part I Part II.
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Data Structures Using C++1 Chapter 3 Pointers Dr. Liu.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
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 This Pointer Programming in C++ Fall 2008 Dr. David A. Gaitros
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.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Memory as byte array Pointers Arrays relationship to pointers Operator ‘new’ Operator ‘delete’ Copy ctor Assignment operator ‘this’ const pointer Allocating.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
ECE 264 Object-Oriented Software Development
Learners Support Publications Constructors and Destructors.
Constructors and Destructors
Pointer to an Object Can define a pointer to an object:
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
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
Basic C++ What’s a declaration? What’s a definition?
Automatics, Copy Constructor, and Assignment Operator
Dynamic Memory Copy Challenge
Constructors and Destructors
Destruction and Copying
The Destructor and the Assignment Operator
Chapter 15-3 Pointers, Dynamic Data, and Reference Types
Essential Class Operations
CS148 Introduction to Programming II
9-10 Classes: A Deeper Look.
Review Chapter 10 PPT for full coverage.
Destruction and Copying
COP 3330 Object-oriented Programming in C++
Dynamic Memory Copy Challenge
COP 3330 Object-oriented Programming in C++
Class: Special Topics 2 For classes using memory allocation
The Constructors Lecture 7 Fri, Feb 2, 2007.
Essential Class Operations
Copy Constructor CSCE 121.
Rule of Three Part 1 & 2.
9-10 Classes: A Deeper Look.
Copy Constructors and Overloaded Assignment
C++ support for Object-Oriented Programming
SPL – PS3 C++ Classes.
Presentation transcript:

Copy Constructors Fall 2008 Dr. David A. Gaitros

Automatic Functions Functions automatically created for each class: – Constructor: Creates objects – Destructor: Deletes objects – Copy Constructor – Assignment operator = We have covered Constructor and Destructor.

Copy Constructor Copy constructor is a “constructor” It is a function with the same name as the class and no return type. However, it is invoked implicitly – An object is defined to have the value of another object of the same type. – An object is passed by value into a function – an object is returned by value from a function

Copy Constructor Examples Fraction f1, f2(3,4) Fraction f3 = f2; // Copy Const. f1 = f2; // Not a copy but assignment // operator.

Copy Constructor Declaring and Defining – A copy constructor always has one (1) parameter, the original object. – Must be the same type as the object being copied to. – Always passed by reference (must be because to pass by value would invoke the copy constructor). – Copy constructor not required Fraction (const Fraction &f); Timer (const timer & t);

Copy Constructor Shallow copy vs deep copy – The default version is a shallow copy. I.E. the object is copies exactly as is over to the corresponding member data in the new object location. – Example: Fraction f1(3,4); The object example illustrates the definition of an object f1 of type Fraction. If passed as a parameter, a shallow copy will be sufficient.

Copy Constructor When there is a pointer to dynamic data, a shallow copy is not sufficient. Why? Because a default or shallow copy will only copy the pointer value (Address). Essentially both objects are pointing to the same item. Here we need a deep copy.

Copy constructor Deep Copy Directory::Directory (const Directory & d) { maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int i=0; i<currentsize; i++) entryList[i] = d.entryList[i]; }