0% found this document useful (0 votes)
5 views

Copy Constructor & Destructor

Uploaded by

Ankur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Copy Constructor & Destructor

Uploaded by

Ankur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Copy Constructor & Destructor

Unit 2
Copy Constructor

• A copy constructor is a member function that initializes


an object using another object of the same class. In simple
terms, a constructor which creates an object by
initializing it with an object of the same class, which has
been created previously is known as a copy constructor.

• Copy constructor is used to initialize the members of a


newly created object by copying the members of an
already existing object.
Copy constructor
• Copy constructor takes a reference to an
object of the same class as an argument.

Sample(Sample &t)
{
id=t.id;
}
Copy constructor
• The process of initializing members of an object through a
copy constructor is known as copy initialization.

• It is also called member-wise initialization because the copy


constructor initializes one object with the existing object,
both belonging to the same class on a member by member
copy basis.

• The copy constructor can be defined explicitly by the


programmer. If the programmer does not define the copy
constructor, the compiler does it for us.
Example
#include<iostream>
#include<string.h>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student(int,char[ ],double);
student(student &t) //copy constructor
{
rno=t.rno;
strcpy(name,t.name);
fee=t.fee;
}
void display();

};
student::student(int no,char n[ ],double f)
{
rno=no;
strcpy(name,n);
fee=f;
}

void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}

int main()
{
student s(1001,"Manjeet",10000);
s.display();

student manjeet(s); //copy constructor called


manjeet.display();

return 0;
}
Characteristics of Copy Constructor

• The copy constructor is used to initialize the members of a newly


created object by copying the members of an already existing object.
• Copy constructor takes a reference to an object of the same class as
an argument.
• The process of initializing members of an object through a copy
constructor is known as copy initialization.
• It is also called member-wise initialization because the copy
constructor initializes one object with the existing object, both
belonging to the same class on a member-by-member copy basis.
• The copy constructor can be defined explicitly by the programmer.
If the programmer does not define the copy constructor, the
compiler does it for us.
Example 2
#include <iostream>
using namespace std;

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; }
};
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;
}
Output: p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
Dynamic Constructor
#include <iostream>
using namespace std;
class geeks {
int* p;
public:
// default constructor
geeks()
{ // allocating memory at run time
p = new int;
*p = 0; }
// parameterized constructor
geeks(int x)
{
p = new int;
*p = x;
}
void display()
{
cout << *p << endl; }
// Deallocate the dynamically allocated memory
~geeks()
{
delete p;
}
};

int main()
{

// default constructor would be called


geeks obj1 = geeks();
obj1.display();

// parameterized constructor would be called


geeks obj2 = geeks(7);
obj2.display();
}
Destructor

• Destructor is an instance member function that is


invoked automatically whenever an object is going to
be destroyed. Meaning, a destructor is the last function
that is going to be called before an object is destroyed.
• A destructor is also a special member function like a
constructor. Destructor destroys the class objects
created by the constructor.
• Destructor has the same name as their class name
preceded by a tilde (~) symbol.
• It is not possible to define more than one destructor.
Destructor

• The destructor is only one way to destroy the object created by


the constructor. Hence destructor can-not be overloaded.
• Destructor neither requires any argument nor returns any
value.
• It is automatically called when an object goes out of scope.
• Destructor release memory space occupied by the objects
created by the constructor.
• In destructor, objects are destroyed in the reverse of an object
creation.
Destructor

• The thing is to be noted here if the object is created by using


new or the constructor uses new to allocate memory that
resides in the heap memory or the free store, the destructor
should use delete to free the memory.
Syntax
• The syntax for defining the destructor within the class:
~ <class-name>()
{
// some instructions
}
• The syntax for defining the destructor outside the class:
<class-name> :: ~<class-name>()
{
// some instructions
}
Example
#include <iostream>
using namespace std;

class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed"; }

// User-Defined Destructor
~Test() { cout << "\nDestructor executed"; }
};
main()
{
Test t;

return 0;
}
Example: Multiple Objects
#include <iostream>
using namespace std;
class Test {
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed"; }
// User-Defined Destructor
~Test() { cout << "\n Destructor executed"; }
};
main()
{
// Create multiple objects of the Test class
Test t, t1, t2, t3;
return 0;
}
Output
• Constructor executed
• Constructor executed
• Constructor executed
• Constructor executed
• Destructor executed
• Destructor executed
• Destructor executed
• Destructor executed
Properties of Destructor

The following are the main properties of Destructor:


• The destructor function is automatically invoked when the
objects are destroyed.
• It cannot be declared static or const.
• The destructor does not have arguments.
• It has no return type not even void.
• An object of a class with a Destructor cannot become a
member of the union.
• A destructor should be declared in the public section of the
class.
• The programmer cannot access the address of the destructor.

You might also like