What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization.

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

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Introduction to Programming Lecture 39. Copy Constructor.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
Operator Overloading Fundamentals
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Pointers Revisited l What is variable address, name, value? l What is a pointer? l How is a pointer declared? l What is address-of (reference) and dereference.
A RRAYS, P OINTERS AND R EFERENCES 1. A RRAYS OF O BJECTS Arrays of objects of class can be declared just like other variables. class A{ … }; A ob[4];
Classes Separating interface from implementation
Constructors & Destructors Review CS 308 – Data Structures.
Copy Constructors Shallow Copy: –The data members of one object are copied into the data members of another object without taking any dynamic memory pointed.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
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.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor-II.
CS212: Object Oriented Analysis and Design Lecture 12: Operator Overloading-II.
CS212: Object Oriented Analysis and Design Lecture 10: Copy constructor.
Copy Constructors Fall 2008 Dr. David A. Gaitros
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Slide 1 Chapter 10 Pointers and Dynamic Arrays. Slide 2 Learning Objectives  Pointers  Pointer variables  Memory management  Dynamic Arrays  Creating.
Object Oriented Programming Elhanan Borenstein Lecture #3 copyrights © Elhanan Borenstein.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Pointers and Dynamic Arrays
More C++ Features True object initialisation
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Chapter 9 Classes: A Deeper Look, Part I Part II.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
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.
Constructors & Destructors, Proxy Classes, Friend Function and example of static member.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
93 4/11/98 CSE 143 Class Constructors [Sections ]
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Dynamic Memory Review l what is static, automatic, dynamic variables? Why are dynamic(ally allocated) variables needed l what is program stack? Function.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 26 Clicker Questions December 3, 2009.
Classes II Lecture 7 Course Name: High Level Programming Language Year : 2010.
Constructor It is a special member of a class that has the following characteristic 1)It has the same name as of its class. 2)It don’t have an explicit.
Constructors And Destructors. 2 Constructor Special Member Function used for Initialization -- Same Name as the Class Name Special Member Function used.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Memory Management in Java Mr. Gerb Computer Science 4.
OOP Details Constructors, copies, access. Initialize Fields are not initialized by default:
CS212: Object Oriented Analysis and Design Polymorphism (Using C++)
Chapter 2 Objects and Classes
Constructors and Destructors
Pointers and Dynamic Arrays
Learning Objectives Pointers as dada members
OBJECT ORIENTED PROGRAMMING
Programming with ANSI C ++
Constructors & Destructors
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Operator Overloading CSCE 121 J. Michael Moore
Const in Classes CSCE 121 J. Michael Moore.
Contents Introduction to Constructor Characteristics of Constructor
Chapter 4 (part 2).
Constructors and Destructors
Java Programming Language
CS410 – Software Engineering Lecture #5: C++ Basics III
A Deeper Look at Classes
Class: Special Topics Overloading (methods) Copy Constructors
Class: Special Topics 2 For classes using memory allocation
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Copy Constructor CSCE 121.
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Constructors & Destructors
Presentation transcript:

What is a copy constructor? A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance during initialization. It must have one of the following form MyClass( const MyClass& other ); MyClass( MyClass& other ); MyClass( volatile const MyClass& other ); MyClass( volatile MyClass& other ); If not specified, the compiler will automatically create a default copy constructor. It does a member-wise copy of the source object.

Default copy constructor example class MyClass { int x; char c; std::string s; }; Consider the following class The compiler will create a default (or implicit) copy constructor equivalent to: MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c ), s( other.s ) {} When the class contain pointer type member variables, the above default constructor is insufficient!

Explicitly specify copy constructor example class MyClass { int x; char c; char *s; }; Consider the following class The compiler will create a default (or implicit) copy constructor equivalent to: MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c ), s( other.s ) {} When the class contain pointer type member variables, the above default constructor is insufficient! Recall the overloading assignment operator “=“!

Explicitly specify copy constructor example class MyClass { int x; char c; char *s; }; Consider the following class Specify your copy constructor as: MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c) { s = new char[strlen(other.s)+1]; strcpy (s, other.s); //deep copy } Recall the overloading assignment operator “=“!

When the copy constructor is called class MyClass { int x; char c; char *s; }; MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c) { s = new char[strlen(other.s)+1]; strcpy (s, other.s); //deep copy } MyClass c_obj1 ( ); //doing some process on c_obj1 MyClass c_obj2(c_obj1); MyClass c_obj3 = c_obj1; // call the copy constructor c_obj3 = c_obj2; // call the assignment operator

When the copy constructor is called class MyClass { int x; char c; char *s; }; MyClass::MyClass( const MyClass& other ) : x( other.x ), c( other.c) { s = new char[strlen(other.s)+1]; strcpy (s, other.s); //deep copy } class DClass { // Dclass’s member functions MyClass c_obj; }; DClass::DClass ( const MyClass& other ) : c_obj (other) { } initialized the member objects in the constructor of the container class in object composition

When the copy constructor is called The following cases may result in a call to a copy constructor: 1.When an object is returned by value 2.When an object is passed (to a function) by value as an argument 3.When an object is thrown 4.When an object is caught 5.When an object is placed in a brace-enclosed initializer list