0% found this document useful (0 votes)
12 views16 pages

LECTURE-4 (Copy Constructor)

Uploaded by

Shafla Rehman
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)
12 views16 pages

LECTURE-4 (Copy Constructor)

Uploaded by

Shafla Rehman
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/ 16

DATA STRUCTURES

LECTURE 04
BOOK READINGS

• Data Structures and Algorithms in C++


by Michael T. Goodrich, Roberto Tamassia, David M. Mount
Second Edition : Chapter 2 (2.3)
A GLIMPSE FROM PREVIOUS LECTURE

• StaticIntPointer Task
• Error Solution (DynamicIntPointer Task)
• Avoid Memory Leakage

• Destructor

• What are dangling pointers?


CONSTRUCTORS

• ClassName obj1; // Default Constructor

• ClassName obj2(Parameter1, Parameter2, …., Parameter


N) ; // Parameterized constructor

• ClassName obj3 = obj2; // Copy constructor


COPY CONSTRUCTOR

Copy Constructor is a type of constructor which is used to create


a copy of an already existing object of a class type.
EXAMPLE

Class circle void main()


{ {
float rad;
circle obj; // obj.circle()
public: circle obj2(6.6); // obj2.circle(6.6)
circle obj3=obj2; // obj2.circle(obj2)
circle() { rad=0;}
circle(floar r){rad=r} }

~circle()
{}
};
SYNTAX OF COPY CONSTRUCTOR

Classname(const classname & objectname)


{
....
}
EXAMPLE

Class circle void main()


{ {
float rad;
circle obj; // obj.circle()
public: circle obj2(6.6); // obj2.circle(6.6)
circle obj3=obj2; // obj2.circle(obj2)
circle() { rad=0;}
circle(floar r){rad=r} }
circle (const circle & obj)
{
rad=obj.rad;
}
~circle()
{}
};
COPYING POINTERS
class dataType void main()
{ {
int * ptr;
dataType obj;
public: Obj.set(5);
dataType() { ptr=NULL;} datatype obj2=obj;
void set (int val) cout<<obj2.get();
{
if(ptr==NULL) }
ptr=new int;
*ptr=val;
}
~circle()
{ delete ptr; ptr=NULL; }

Int get()
{ return *ptr;}
DEEP VS. SHALLOW
DEEP COPY VS SHALLOW COPY

Shallow Copy Deep Copy


Void main() Void main()
{ {
int *p,*q; int *p,*q;
p=new int; p=new int;
*p=5; *p=5;
q=p; // shallow copy q=new int;
*q=9; *q=*p; // deep copy
cout<<*p; cout<<*p;
} }
WRITE COPY CONSTRUCTOR?
class dataType void main()
{ {
int * ptr;
dataType obj;
public: Obj.set(5);
dataType() { ptr=NULL;} datatype obj2=obj;
void set (int val) cout<<obj2.get();
{
if(ptr==NULL) }
ptr=new int;
*ptr=val;
}
~circle()
{ delete ptr; ptr=NULL; }

Int get()
{ return *ptr;}
COPY CONSTRUCTOR CALL

• Called whenever
• an object is created by initializing it with already existing object of same
class
• An object is passed as an argument to a function
• An object is returned from a function
EXAMPLE
class dataType dataType Greater(dataType obj)
{ {
int * ptr; if(*ptr> *(obj.ptr))
return (*this);
public:
dataType() { ptr=NULL;} return obj;
void set (int val) }
{ };
if(ptr==NULL) void main()
ptr=new int; {
*ptr=val;
} dataType obj;
~circle() Obj.set(5);
{ delete ptr; ptr=NULL; } datatype obj2=obj;
cout<<obj2.get();
Int get() dataType obj4=obj.Greater(obj2);
{ return *ptr;}
}
CLASS DISCUSSION

• Can we make copy constructor private?

• Why argument to a copy constructor must be passed as a


reference?

• Why argument to a copy constructor should be const?


REFERENCES

• https://www.geeksforgeeks.org/copy-constructor-in-cpp/

You might also like