source file name: copycst.CPP
#include<iostream.h>
#include<conio.h>
class Point
{
private:
int x, y;
public:
Point(int x1, int y1) { x = x1; y = y1; }
// Copy constructor
Point(const Point &p1) {x = p1.x; y = p1.y; }
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(25, 10); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
Point p3(15,20);
clrscr();
// Let us access values assigned by constructors
cout << "\n point 1 values x = " << p1.getX() << ", y = " << p1.getY();
cout << "\npoint 2 values x = " << p2.getX() << ", y = " << p2.getY();
cout << "\npoint 3 values x = " << p3.getX() << ", y = " << p3.getY();
return 0;
}
output:
0 Comments
Post a Comment