Problem Statement - Copy Constructor Definition and Example
What is a copy constructor? Give a suitable example in C++ to illustrate with its definition within a class and a declaration of an object with the help of it.
Solution
TC++ #5943
A copy constructor is an overloaded constructor in which an object of the same class is passed as reference parameter.
class Point
{
int x;
public:
Point(){x=0;}
Point(const Point &p) // Copy constructor
{x = p.x;}
:
};
void main()
{
Point p1;
Point p2(p1); //Copy constructor is called here
//OR
Point p3=p1; //Copy constructor is called here
}