0% found this document useful (0 votes)
4 views42 pages

OOP - Lec 1 - Classes (Cont.)

The document provides an overview of copy constructors in object-oriented programming, explaining their purpose and how they are automatically invoked during object initialization and when passing objects by value to functions. It distinguishes between shallow and deep copies, highlighting the potential issues with shallow copies when dealing with pointers. The document also emphasizes the importance of defining copy constructors for proper memory management and avoiding unintended side effects.

Uploaded by

hunnyasrfrz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views42 pages

OOP - Lec 1 - Classes (Cont.)

The document provides an overview of copy constructors in object-oriented programming, explaining their purpose and how they are automatically invoked during object initialization and when passing objects by value to functions. It distinguishes between shallow and deep copies, highlighting the potential issues with shallow copies when dealing with pointers. The document also emphasizes the importance of defining copy constructors for proper memory management and avoiding unintended side effects.

Uploaded by

hunnyasrfrz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

CS 103

Object Oriented Programming

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

Recap: Passing parameters to


functions
 Pass by Value
• Makes a copy of the actual parameters in memory
• We use pass by value when we are only using the parameter for some
computation, not changing it

 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

Recap: Passing parameters to


functions
Pass by Value Pass by Pointer Pass by reference
int inc(int int inc(int *a) int inc(int &a)
a) { {
{ *a = *a + 1; a = a +
a = a + return *a; 1; return
1; return } a;
a; }
}
int main() int main() int main()
{ int x = { int x = { int x =
0; 0; 0;
inc(x); inc(x); inc(x);
cout << x << endl; cout << x << endl; cout << x
return 0; return 0; << endl;
} } return 0;
}
CS103 – Object Oriented Programming

Recap: Passing parameters to


functions
Pass by Value Pass by Pointer Pass by reference
void inc(int a) void inc(int void inc(int
{ {
*a) &a)
{
a = a + * a = a +
1; return a 1; return
} a; } } a;
Function
Fun = Fun
signature
ctio
int main() *
int main()
ctio
int main()
{ intnx = a { int x = { intn x =
0; sig 0; 0; sig
inc(x);
nat + inc(x); nat
inc(x);
cout <<ure
x << endl; cout << x << endl; cout <<urex
return 0; 1 return 0; << endl;
} }; return 0;
r }
e
t
CS103 – Object Oriented Programming

Recap: Passing parameters to


functions
Pass by Value Pass by Pointer Pass by reference
void inc(int void inc(int void inc(int
a) *a) &a)
{ a = a + {*a = *a + 1; { a = a +
1; return return *a; 1; return
} a; } } a;

Function Function Function


Definition Definition Definition
int main() int main() int main()
{ int x = { int x = { int x =
0; 0; 0;
inc(x); inc(x); inc(x);
cout << x << endl; cout << x << endl; cout << x
return 0; return 0; << endl;
} } return 0;
}
CS103 – Object Oriented Programming

Recap: Passing parameters to


functions
Pass by Value Pass by Pointer Pass by reference
void inc(int a) void inc(int *a) void inc(int &a)
{ { {
a = a + *a = *a + 1; a = a +
1; return return *a; 1; return
a; } a;
} }
F
F u F
u n u
inc(x); n inc(x);c inc(x); n
cout << c
x << cout <<t x << cout << c
x <<
endl; return
t 0; endl; return 0; endl; t
}
i
} } return 0;
i o i
o n o
n n
C
CS103 – Object Oriented Programming 9

Recap: Pointers and


References
 A Reference variable is an alias for a variable which is assigned to it
 It is different from a pointer because:
• The reference variable can only be initialized at the time of its creation
• The reference variable can never be reinitialized again in the program
• The reference variable can never refer to NULL
CS103 – Object Oriented Programming 10

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

Shallow copy vs. Deep


copy
class Sequence
{ public:
int main()
{
int *List; Sequence seq1;
int Sequence seq2 =
NumElements; seq1;
Sequence();
Sequence(Seque seq2.List[0] = 10;
nce const } cout <<
&seq); seq1.List[0];
};

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

In the default constructor, memory


for 10 integers is dynamically
allocated and its address is stored
in ‘List’
Sequence::Sequence()
{
NumElements = 20;
List = new
int[NumElements];
}
CS103 – Object Oriented Programming 19
Somewhere else in
Somewhere in
Memory
Seq Memory

1 20
L
• NumEleme
nts
• List

Sequence seq2 = seq1;


Another object of Sequence class is N
created L
and initialized using copy
constructor Seq2

• NumEleme
nts

• List
CS103 – Object Oriented Programming 20
Somewhere else in
Somewhere in
Memory
Seq Memory

1 20
L
• NumEleme
nts
• List

In the copy constructor, values


of NumElements and List are 20
copied from SEq1 to SEq2 L

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

So ‘List’ in Seq2 contains the


same value as List in ‘Seq1’… 20
They are pointing to the same L
memory location!
Seq2

• NumEleme
nts

• List
CS103 – Object Oriented Programming 22
Somewhere else in
Somewhere in
Memory
Seq Memory

1 20
L
• NumEleme
nts
• List

If we write something to ‘List’ of


Seq1 it’ll be seen in ‘List’ of Seq2! 20
L

Seq2

• NumEleme
nts

• List
CS103 – Object Oriented Programming 23
Somewhere else in
Somewhere in
Memory
Seq Memory

1 20
L
• NumEleme
nts
• List

And if we delete the ‘List’ of Seq1


it’ll 20
delete the ‘List’ of Seq2 too!!! L

Seq2

• NumEleme
nts

• List
CS103 – Object Oriented Programming 24

Shallow copy vs. Deep


copy
class Sequence
{ public:
int main()
{
int *List; Sequence seq1;
int Sequence seq2 =
NumElements; seq1;
Sequence();
Sequence(Seque seq2.List[0] = 10;
nce const } cout <<
&seq); seq1.List[0];
};

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 copy vs. Deep


copy
Sequence::Sequence(Sequence const
&seq)
int main()
{
{ Sequence seq1;
NumElements = Sequence seq2 =
seq.NumElements; List = new seq1;
int[NumElements];
for (int i = 0; i < NumElements; seq2.List[0] = 10;
i++) seq1.List[0] = 30;
{ cout <<
List[i] = seq.List[i]; seq2.List[0]; cout
} } << seq1.List[0];
}
Output:
10
30
This is deep copy.
The pointer variables are separately allocated memory for every object
being created. Then element by element copy is performed to copy data
CS103 – Object Oriented Programming 26

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

In the example the data members are


declared public just for elaborating a
concept. In practice, you should NEVER do
this!
CS103 – Object Oriented Programming 29

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?

 When a function is called for an object, the address of the object is


passed to the function as argument
 This address is dereferenced by the function and hence it acts on
correct object
 The variable containing the “self-address” is called this pointer
 Whenever a function is called, this pointer is passed as argument
to that function
• A function with n parameters is actually called with n+1 parameters
CS103 – Object Oriented Programming 37

this
pointer
 For example, the function

void Rectangle::SetWidth (int w)

is internally represented as

void Rectangle::SetWidth(int w, Rectangle* const


this)
CS103 – Object Oriented Programming 38

this
pointer
 This is your code

Rectangle::SetWidth(int w) {
Width = w;
}

but the code generated by the compiler will be:

Rectangle::SetWidth(int w, Rectangle* const


this) {
this->Width = w;
CS103 – Object Oriented Programming 39

this pointer: Can it be used


explicitly?
 Yes. Two scenarios are common:
1. When the name of input parameter is the same as data member
void Rectangle::SetWidth(int Width)
{
Width = Width;
}
• The compiler will assign the current value of Width to itself!
• We need to use this pointer to specify what we mean…
void Rectangle::SetWidth(int Width)
{
this->Width = Width;
}
CS103 – Object Oriented Programming 40

this pointer: Can it be used


explicitly?
 Yes. Two scenarios are common:
2. When we need to return the reference of the object in a function
Rectangle& Rectangle::SetWidth(int Width)
{
this->Width =
Width; return
*this;
}
Rectangle& Rectangle::SetHeight(int
Height)
{
this->Height = Height;
return *this;
}
CS103 – Object Oriented Programming 41

this pointer: Can it be used


explicitly?
 Yes. Two scenarios are common:
2. When we need to return the reference of the object in a function
• Example:
int main()
{
Rectangle r1;
r1.SetWidth(10).SetHeight(1
0); return 0;
}

• When a reference to a local object is returned,


the returned reference
can be used to chain function calls on a single

You might also like