Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

The Copy Constructor

#include <iostream>
using namespace std;
class Point {
// fields are accessible only to methods belonging to point
double x;
double y;
// constructor accepting two arguments, called by a
// statement such as: Point p(1, 2);
Point(double a_x, double a_y) : x(a_x), y(a_y) {}
// copy constructor
Point(const Point& p) {
x = p.x;
y = p.y;
int main() {
Point p1(1, 2); // new object using a constructor
Point p2(p1); // new object using the copy constructor
return 0;
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content