12.special Member Functions
12.special Member Functions
• DEFAULT CONSTRUCTOR
• COPY CONSTRUCTOR
• OVERLOADED ASSIGNMENT OPERATOR
• DESTRUCTOR
Copy Constructor
A copy constructor is a member function that
initializes
an object using another object of the same class.
// Copy constructor
Point(const Point &p1) {x = p1.x; y =
p1.y; }
return 0;
}
4
When is copy constructor called?
6
Default Assignment operator
• The default behavior is that all non-static
members of the class are copied one-by-one
from the source to the target class.
• In C++, an assignment operator may be called in
the following cases:
1. When an already initialized object is assigned a new
value from another existing object
2. When an object of a class explicitly call operator=
function and pass another object of same class as
argument 7
Copy constructor vs Assignment Operator
Myclass t1,t2,t4;
Myclass t3=t1;(1)
t2=t1;(2)
t4.operator=(t1); (3)
Sumfunc(t1) (4)
In the above example (1) calls copy constructor and (2) calls
assignment operator. (3) calls assignment operator. (4) calls copy
constructor 8
Copy constructor vs Assignment Operator
class test
{
int x;
public:
test(void);
test(int);
test(test &);
void operator=(test&);
};
9
Copy constructor vs Assignment Operator
10
Copy constructor vs Assignment Operator
int main(){
{
test t1,t2;
test t3(2);
test t4=t3;
}
getch();
return 0;
11
}
When should you overload copy constructor
and assignment operator
• When you have pointers as data members of the class.
• When you copy an object of this class the default assignment
operator and copy constructor will copy the value of this member
pointer to the new object.
• This means that the new object and the old object will be pointing
at the same piece of memory so when you change it in one object
it will be changed for the other object too. If one object deletes
this memory the other will carry on trying to use it - eek.
• To resolve this you write your own version of the copy constructor
and assignment operator. Your versions allocate separate memory
to the new objects and copy across the values that the first
pointer is pointing to rather than its address.
12
Destructors
• C++ destructor is a special member function
that is executed automatically when an object is
destroyed that has been created by the
constructor.
• C++ destructors are used to de-allocate the
memory that has been allocated for the object
by the constructor.
• A destructor is called for a class object when that
object passes out of scope or is explicitly deleted
13
Destructors
• A destructor is a member function with the same name
as its class prefixed by a ~ (tilde). For example:
class X {
public:
// Constructor for class X
X();
// Destructor for class X
~X();
};
• A destructor takes no arguments and has no return type.14
destructors
class test
{
int x;
public:
test(void);
test(int);
~test(void);
};
15
Destructors
test::test(void)
{
x=0;
cout<<"i am constructor"<<x<<endl;
}
test::test(int m){
x=m;
cout<<"i am parameterized constructor"<<x<<endl;
}
test::~test(void)
{
cout<<"i am destructor"<<x<<endl;
} 16
Destructors
int main(){
{
test t1;
test t3(2);
test t4(3);
test *ptr=new test(4); //destructor is not called for dynamic memory unless you
delete explicitly
delete ptr;
}
getch();
return 0;
17
}
Destructors: order of execution
18