COPY CONSTRUCTORS
W E E K - 4 ( PA R T 2 )
CSSE-2133 Object Oriented Programming
POINTERS
• Variable whose value refers to another value stored somewhere
else in the computer
• Definition:
Datatype * variable_name = & name_variable_pointed_to
Defines variable a pointer Returns an address
• Uses:
• Dynamically allocated memory to a variable
CSSE-2133 Object Oriented Programming
Simple
variables
Pointers structures
Types of
Variables
Objects Arrays
CSSE-2133 Object Oriented Programming
CONSTRUCTORS
Non- Parameterized Parameterized
Default Constructor Copy Constructors
Constructor Constructor
CSSE-2133 Object Oriented Programming
COPY CONSTRUCTOR
• A copy constructor is a member
function which initializes an
object using another object of the
same class
• Syntax:
CSSE-2133 Object Oriented Programming
TYPES OF COPY CONSTRUCTORS
Types of
Copies of
Object
Shallow
Deep Copy
Copy
Constructor
Constructor
Also known as Default Copy Constructor
CSSE-2133 Object Oriented Programming
SHALLOW VS DEEP COPY CONSTRUCTOR
Shallow Copy Constructor Deep Copy Constructor
• Does a member by member • Designed to handle dynamic
copy of the source object variables while creating the
• provided by the compiler as copy of the source object
a default copy constructor if
not defined
• Used in the Absence of
dynamic variables
CSSE-2133 Object Oriented Programming
SHALLOW VS DEEP COPY
CSSE-2133 Object Oriented Programming
SHALLOW VS DEEP COPY (IN CASE OF
DYNAMIC VARIABLES)
Source Copied
Object Object
Memory
########## Shallow Copy
Source Copied
Object Object
Memory
########## ########## Deep Copy
CSSE-2133 Object Oriented Programming
EXAMPLE – SHALLOW COPY
CSSE-2133 Object Oriented Programming