A copy constructor is a constructor that accepts an object of the same class as its parameter and copies its data members to the object on the left part of the assignment. It is useful when we need to construct a new object of the same class.
Example:
class A{
int x; int y;
public int color;
public A() : x(0) , y(0) {} //default (no argument) constructor
public A( const A& ) ;
};
A::A( const A & p )
{
this->x = p.x;
this->y = p.y;
this->color = p.color;
}
main()
{
A Myobj;
Myobj.color = 345;
A Anotherobj = A( Myobj ); // now Anotherobj has color = 345
}