OOP - Lec 1 - Classes (Cont.)
OOP - Lec 1 - Classes (Cont.)
Spring 2025
CS103 – Object Oriented Programming 1
Copy Constructors
CS103 – Object Oriented Programming 2
Content
s What is a copy constructor?
Defining copy constructors
Shallow copy vs. deep copy
CS103 – Object Oriented Programming 3
Copy
Constructors
Copy constructors are automatically called:
1. At the time of initializing an object B using another object A
Data members of B are initialized with values of data members of A
In other words, B is created as a copy of A
2. When an object is passed by value to a function
CS103 – Object Oriented Programming 4
Pass by reference
• Forwards the actual parameters to the function
• We use pass by reference when we want the function to change the
values of the passed parameter
CS103 – Object Oriented Programming
Back to Copy
Constructor…
Copy constructors are automatically called:
• At the time of initializing an object B using another object A
• When an object is passed by value to a function
void func1(Rectangle rect)
{
…
}
void main()
{
Rectangle a;
Rectangle Creating a new object from an
b(a); existing one
Rectangle c = Passing an object to a
} a; func1(a); function by value
CS103 – Object Oriented Programming 11
Defining copy
constructor
Copy constructors are automatically called:
• At the time of initializing an object B using another object A
• When an object is passed by value to a function
void func1(Rectangle Rectangle::Rectangle(Rectangle
rect) rect)
{ {
… Width
Height = = rect.Width;
} } rect.Height;
void main()
{ WRON
Rectangle a; G!
Rectangle WHY
Rectangle c = a; Because if we pass an object to a function by value,
b(a);
func1(a) copy constructor. But the copy constructor is itself
it’ll need to call the ?
} ; using the object by
CS103 – Object Oriented Programming 12
Defining copy
constructor
Copy constructors are automatically called:
• At the time of initializing an object B using another object A
• When an object is passed by value to a function
void func1(Rectangle Rectangle::Rectangle(Rectangle
rect) &rect)
{ {
… Width = = rect.Width;
Height
} } rect.Height;
void main()
{
Rectangle a; The solution is to pass the object by reference in the
b(a); copy constructor. Now the copy constructor will be
Rectangle c = called only once whenever we are creating new
} a; func1(a); object or passing an object by value to some
CS103 – Object Oriented Programming 13
Defining copy
constructor
Copy constructors are automatically called:
• At the time of initializing an object B using another object A
• When an object is passed by value to a function
void func1(Rectangle Rectangle::Rectangle(Rectangle
rect) &rect)
{ {
… Width
Height = + = ++rect.Width;
} } +rect.Height;
void main()
{ We’re trying to change the data of the item being
Rectangle a; copied… WRONG!
Rectangle We don’t want to accidentally change any
b(a); data member of the object being copied (rect,
Rectangle c = in the example), either during new object
} a; func1(a); creation or by a function which is using rect
CS103 – Object Oriented Programming 14
Defining copy
constructor
Copy constructors are automatically called:
• At the time of initializing an object B using another object A
• When an object is passed by value to a function
void func1(Rectangle Rectangle::Rectangle(Rectangle const
rect) &rect)
{ … { Width = rect.Width;
} Height = rect.Height
;
void main() }
{ Rectangle a;
Rectangle So we use the keyword const in the copy
b(a); constructor to tell the compiler that the
Rectangle c = object being copied cannot be changed
} a; func1(a);
CS103 – Object Oriented Programming 15
Sequence::Sequence( Output:
) 10
{
NumElements =
What happened here? We changed
20; one object but it also affected the
List = new other object! But why?
int[NumElements When we said “create seq2 as a copy of
]; seq1” the copy constructor copied the
CS103 – Object Oriented Programming 16
Somewhere in
Memory
CS103 – Object Oriented Programming 17
Somewhere in
Seq Memory
Sequence seq1;
1 N
L
• NumEleme An object of Sequence class is
created in Memory. Memory is
nts
allocated for an integer
• List (NumElements) and an integer
pointer (List)
CS103 – Object Oriented Programming 18
Somewhere else in
Somewhere in
Memory
Seq Memory
1 20
L
• NumEleme
nts
• List
1 20
L
• NumEleme
nts
• List
• NumEleme
nts
• List
CS103 – Object Oriented Programming 20
Somewhere else in
Somewhere in
Memory
Seq Memory
1 20
L
• NumEleme
nts
• List
Seq
2
• NumEleme Sequence::Sequence(Sequence const
&seq)
nts {
• List NumElements =
seq.NumElements; List =
seq.List;
CS103 – Object Oriented Programming 21
Somewhere else in
Somewhere in
Memory
Seq Memory
1 20
L
• NumEleme
nts
• List
• NumEleme
nts
• List
CS103 – Object Oriented Programming 22
Somewhere else in
Somewhere in
Memory
Seq Memory
1 20
L
• NumEleme
nts
• List
Seq2
• NumEleme
nts
• List
CS103 – Object Oriented Programming 23
Somewhere else in
Somewhere in
Memory
Seq Memory
1 20
L
• NumEleme
nts
• List
Seq2
• NumEleme
nts
• List
CS103 – Object Oriented Programming 24
Sequence::Sequence( Output:
) 10
{
NumElements =
This is shallow copy.
20; For ‘normal’ data members shallow copy
List = new is fine because the values of variables
int[NumElements are being copied. Pointers hold memory
]; addresses, so shallow copy copies
CS103 – Object Oriented Programming 25
Shallow/deep copy
constructor
Copy constructor should be made to perform deep copy
If we do not make a copy constructor then the compiler performs
shallow copy by default
CS103 – Object Oriented Programming 27
this
pointer
When we define a class, the compiler reserves the space for all the
functions defined in the class
• No space is allocated for data!
Because no object is created yet…
Function space in memory is common for all objects of a class
Whenever a new object is created, memory is reserved for its
variables
• Same copy of functions is called for each object
CS103 – Object Oriented Programming
class Rectangle
{
A public:
int Width=10, Height=10;
int Area()
{
return Width *
Height;
}
};
Somewhere in
Memory
CS103 – Object Oriented Programming 30
class Rectangle
rect {
1 A public:
• Widt int Width=10, Height=10;
h int Area()
• Heigh {
1 return Width *
t 0 Height;
1 }
0 };
Rectangle
rect1;
Somewhere in
Memory
CS103 – Object Oriented Programming 31
class Rectangle
rect {
1 A public:
• Widt int Width=10, Height=10;
h int Area()
• Heigh {
1 return Width *
t 0 Height;
1 }
0 };
1
Rectangle
rect 0
rect1;
1
2 0 Rectangle
• Width rect2;
• Heigh
t Somewhere in
Memory
CS103 – Object Oriented Programming 32
class Rectangle
rect {
1 A public:
• Widt int Width=10, Height=10;
h int Area()
• Heigh {
1 return Width *
t 0 Height;
20 }
1 };
Rectangle rect1;
rect 0
Rectangle rect2;
1
2 0 rect1.Height =
• Width 20;
• Heigh
t Somewhere in
Memory
CS103 – Object Oriented Programming 33
class Rectangle
rect {
1 A public:
• Widt int Width=10, Height=10;
h int Area()
• Heigh {
1 return Width *
t 0 Height;
20 }
30 };
Rectangle rect1;
rect 1
Rectangle rect2;
0
2 rect1.Height =
• Widt 20;
h rect2.Width =
• Heigh 30;
t Somewhere in
Memory
CS103 – Object Oriented Programming 34
class Rectangle
rect {
1 A public:
• Widt int Width=10, Height=10;
h int Area()
• Heigh {
1 return Width *
t 0 Height;
20 }
30 };
Rectangle rect1;
rect 1
Rectangle rect2;
0
2 rect1.Height =
• Widt 20;
rect2.Width =
h 30;
cout <<
• Heigh rect1.Area();
t Somewhere in
Memory
CS103 – Object Oriented Programming 35
class Rectangle
rect {
1 A public:
• Widt int Width=10, Height=10;
h int Area()
• Heigh {
1 return Width *
t 0 Height;
20 }
30 };
Rectangle rect1;
rect 1
Rectangle rect2;
0
2 rect1.Height =
• Widt 20;
rect2.Width =
h 30;
cout <<
• Heigh rect1.Area(); cout
<< rect2.Area();
t Somewhere in
Memory
CS103 – Object Oriented Programming 36
this
pointer
How does a function know on which object to operate?
this
pointer
For example, the function
is internally represented as
this
pointer
This is your code
Rectangle::SetWidth(int w) {
Width = w;
}