Lecture 6 (Copy Constructor)
Lecture 6 (Copy Constructor)
LECTURE 5
REFERENCE: ROBERT LAFORE, CHAPTER 6
The Default Copy
Constructor
Copy constructor is used to initialize an object
with another (existing) object of the same type
(class)
A default copy constructor is built automatically
into the class if the programmer does not specify
any.
The default behavior is member-wise copy
Example
int main() {
Distance d1(11, 6.25); //overloaded ctor
Distance d2(d1); //default copy ctor
Distance d3=d1; //default copy ctor
cout<<“\n d1 = “; d1.showDist();
cout<<“\n d2 = “; d2.showDist();
cout<<“\n d3 = “; d3.showDist();
return 0;
}
Returning objects
from functions
class Distance {
int feet; float inches;
public:
Distance(): feet(0), inches(0.0) {}
Distance(int ft, float in): feet(ft), inches(in) {}
void setDist() {
cout<<“\nEnter feet: “; cin>>feet;
cout<<“Enter inches: “; cin>>inches;}
void showDist() { cout<<feet<<“\’-”<<inches<<‘\”’;}
Distance addDist(Distance);
};
Returning objects
from functions
Distance Distance::addDist(Distance d2)
{
Distance temp;
temp.inches=inches + d2.inches;
if (temp.inches>=12.0)
{
temp.inches -= 12;
temp.feet=1;
}
temp.feet += feet + d2.feet;
return temp;
}
void main()
{
Distance d1, d3;
Distance d2(11, 6.25);
d1.getDist();
d3=d1.add(d2);
d1.showDist();
d2.showDist(); d3.showDist();
}
Pointers
void main
{
int *p;
int val=9;
p=val;
cout<<p;
}
?? Error in the code?
Example 3
void main{
int a = 5, b = 10;
int *p1, *p2;
p1 = &a;
p2 = &b;
*p1 = 10;
p1 = p2;
*p1 = 20;
cout<<a<<“ “<<b;}
?? What will be the output?
10 20
Pointer Arithmetic
Some mathematical operations may be
performed on pointers.
void main ()
{
int *ptr = NULL;
}
Contd.
If all unused pointers are given the null value you can avoid
the accidental misuse of an uninitialized pointer.
Many times, uninitialized variables hold some junk values
and it becomes difficult to debug the program.
if(ptr){ // succeeds if p is not null }
if(!ptr) {// succeeds if p is null}
Pointer Rules
Summary
A pointer stores a reference to its pointee. The pointee, in
turn, stores something useful.
The dereference operation on a pointer accesses its pointee.
A pointer may only be dereferenced after it has been assigned
to refer to a pointee. Most pointer bugs involve violating this
one rule.
Allocating a pointer does not automatically assign it to refer
to a pointee. Assigning the pointer to refer to a specific
pointee is a separate operation which is easy to forget.
Assignment between two pointers makes them refer to the
same pointee which introduces sharing.
Shallow and deep copy
void main()
{
int a=9;
int b=a;
a=10;
cout<<a<<b;
}
?? Does change in value of a leads to
change in value of b
Class MyData Void main()
{ {
int *a;
MyData obj1;
public: int j=9;
MyData():a(NULL) obj1.setValue(j);
{ } cout<<obj1.getValue();
MyData obj2=obj1;
MyData(int r) cout<<obj2.getValue();
{ a=new int; *a=r;} obj2.setValue(20);
cout<<obj1.getValue();
void setValue(int m)
{a=new int; *a=m;}
}
int getValue()
{return *a;} Output?
void main()
{
int a=9;
int*p =&a;
int*r=p;
*r=6;
cout<<a;
}
COPYING POINTER
void main()
{
int*p=new int;
int *r;
*p=8;
r=p;
*p=9;
cout<<*p<<*r;
}
Deep Copy
void main()
{
int*p=new int;
int *r=new int;
*p=8;
*r=*p;
*p=10;
}
Copy Constructor
Deep Copying
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
Class_name(class_name & obj)
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;
} }
CLASSES WITH POINTER
DATA MEMBER
Constructor to ensure Memory is
allocated
Setter to ensure Value initialized
to memory
Destructor to ensure de allocation
Copy constructor should be
provided to ensure deep copy
Why?
Reading Assignment