Copy Constructor
A copy constructor is a member function which initializes an object using another object of the same class. Detailed article on Copy Constructor (Links to an external site.).
Whenever we define one or more non-default constructors( with parameters ) for a class, a default constructor( without parameters ) should also be explicitly defined as the compiler will not provide a default constructor in this case. However, it is not necessary but it’s considered to be the best practice to always define a default constructor.
// Illustration
#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);
}