Home »
C+ programs »
C++ constructor, destructor programs
C++ program to demonstrate example of Copy Constructor
Learn about the Copy Constructor in C++, how to implement Copy Constructor in C++ program?
[Last updated : March 02, 2023]
In this program we will learn to create Copy Constructor program in c++ programming language.
Copy Constructor Program in C++
// C++ program to demonstrate example of
// Copy Constructor.
#include <iostream>
using namespace std;
//Class declaration.
class Demo {
//Private block to declare data member( X,Y ) of
//integer type.
private:
int X;
int Y;
//Public block of member function to
//access data members.
public:
//Declaration of parameterized constructor to
//initialize data members.
Demo(int a, int b);
//Declaration of copy constructor to
//initialize data members.
Demo(const Demo& d);
//To display output on screen.
void Display();
}; //End of class
//Definition of parameterized constructor.
Demo::Demo(int a, int b)
{
X = a;
Y = b;
}
//Definition of copy constructor.
Demo::Demo(const Demo& d)
{
X = d.X;
Y = d.Y;
}
//Definition of Display () member function.
void Demo::Display()
{
cout << endl
<< "X: " << X;
cout << endl
<< "Y: " << Y << endl;
}
int main()
{
//Ctor automatically call when object is created.
Demo d1(10, 20);
//Display value of data member.
cout << endl
<< "D1 Object: " << endl;
cout << "Value after initialization : ";
d1.Display();
//Intialize object with other object using copy constructor
Demo d2 = Demo(d1); //also write like this :Demo d2(d1);
//Display value of data member.
cout << endl
<< "D2 Object: " << endl;
cout << "Value after initialization : ";
d2.Display();
return 0;
}
Output
D1 Object:
Value after initialization:
X: 10
Y: 20
D2 Object:
Value after initialization:
X: 10
Y: 20