0% found this document useful (0 votes)
9 views18 pages

12.special Member Functions

The document discusses special member functions in C++, including default constructors, copy constructors, overloaded assignment operators, and destructors. It explains the purpose and usage of copy constructors, when they are called, and the importance of user-defined copy constructors and assignment operators when dealing with pointers. Additionally, it covers destructors, their role in memory management, and provides examples of their implementation.

Uploaded by

komal komal
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)
9 views18 pages

12.special Member Functions

The document discusses special member functions in C++, including default constructors, copy constructors, overloaded assignment operators, and destructors. It explains the purpose and usage of copy constructors, when they are called, and the importance of user-defined copy constructors and assignment operators when dealing with pointers. Additionally, it covers destructors, their role in memory management, and provides examples of their implementation.

Uploaded by

komal komal
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/ 18

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.

 A copy constructor has the following


prototype:
ClassName (const ClassName &old_obj);

Why argument to a copy constructor must be


passed as a reference and constant?
A copy constructor is called when an object is passed by value.
Copy constructor itself is a function. So if we pass an argument
2
by value in a copy constructor, a call to copy constructor would
be made to call copy constructor which becomes a
non-terminating chain of calls
Copy constructor
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }

// Copy constructor
Point(const Point &p1) {x = p1.x; y =
p1.y; }

int getX() { return x; }


int getY() { return y; }
};
3
Copy constructor
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here

// Let us access values assigned by constructors


cout << "p1.x = " << p1.getX() << ", p1.y = " <<
p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " <<
p2.getY();

return 0;
}

4
When is copy constructor called?

In C++, a Copy Constructor may be called in the


following cases:

1. When an object of the class is returned by value.


2. When an object of the class is passed (to a
function) by value as an argument.
3. When an object is constructed based on another
object of the same class.
5
When is a user-defined copy constructor
needed?

If we don’t define our own copy constructor, the


C++ compiler creates a default copy constructor
for each class which does a member-wise copy
between objects.

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

• Which of the following two statements call copy constructor


and which one calls 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

test::test(void) test::test(test &t){


{ x=t.x;
x=0; cout<<"i am copy constructor"<<endl;
cout<<"i am constructor” }
<<endl; void test::operator=(test &t)
} {
test::test(int m){ x=t.x;
x=m; cout<<"i am assignment
cout<<"i am parameterized operator"<<endl;
constructor"<<endl; }
}

10
Copy constructor vs Assignment Operator

int main(){
{
test t1,t2;
test t3(2);
test t4=t3;

t2=t3; //what would have


happened if object was passed by
value to operator= function
t2.operator=(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

You might also like