C++ Copy Constructor with Example

What is copy constructor and how to use it in C++?


A copy constructor in C++ programming language is used to reproduce an identical copy of an original existing object.It is used to initialize one object from another of the same type.

Example :

using namespace std;
class copycon {
int copy_a,copy_b; // Variable Declaration
copycon(int x,int y) {//Constructor with Argument
copy_b=y; // Assign Values In Constructor
void Display() {
cout<<"\nValues :"<< copy_a <<"\t"<< copy_b;
int main() {
copycon obj(10,20);
copycon obj2=obj; //Copy Constructor
cout<<"\nI am Constructor";
obj.Display(); // Constructor invoked.
cout<<"\nI am copy Constructor";
return 0;

Result :

I am Constructor
Values:10 20
I am Copy Constructor
Values:10 20

C++ Tutorial

Ask Questions

Ask Question