LECTURE-4 (Copy Constructor)
LECTURE-4 (Copy Constructor)
LECTURE 04
BOOK READINGS
• StaticIntPointer Task
• Error Solution (DynamicIntPointer Task)
• Avoid Memory Leakage
• Destructor
~circle()
{}
};
SYNTAX OF COPY CONSTRUCTOR
Int get()
{ return *ptr;}
DEEP VS. SHALLOW
DEEP COPY VS SHALLOW COPY
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
• https://www.geeksforgeeks.org/copy-constructor-in-cpp/