/* */ Click to Join Live Class with Shankar sir Call 9798158723

What is copy constructor in C++ language ?


A copy constructor is a special type of constructor used to create a new object as a copy of an existing object (of the same type). The compiler provides each class a default copy constructor and users can define it also. It takes a single argument which is an object of the same class.

Q.) Example of copy constructor .
    


#include < iostream>
using namespace std;

class point {
private:
	double x, y;

public:
	// Non-default Constructor &
	// default Constructor
	point(double px, double py) { x = px, y = py; }
};

int main(void)
{

	// Define an array of size
	// 10 & of type point
	// This line will cause error
	point a[10];

	// Remove above line and program
	// will compile without error
	point b = point(5, 6);
}

Output
Error: point (double px, double py): expects 2 arguments, 0 provided