fork(2) download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Point
  5. {
  6. private:
  7. int x, y;
  8. public:
  9. Point(int x1, int y1) { x = x1; y = y1; }
  10.  
  11. // Copy constructor
  12. // Point(const Point &p2) {x = p2.x; y = p2.y; }
  13.  
  14. void set()
  15. {
  16. x=50;
  17. y = 100;
  18. }
  19. int getX() { return x; }
  20. int getY() { return y; }
  21. };
  22.  
  23. int main()
  24. {
  25. Point p1(10, 15); // Normal constructor is called here
  26. Point& p2( p1);
  27. // p2 = p1;
  28. cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
  29. cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
  30. p2.set();
  31. cout << "\np1.x = " << p1.getX() << ", p1.y = " << p1.getY();
  32. cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
  33. // Copy constructor is called here
  34.  
  35. // Let us access values assigned by constructors
  36.  
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
p1.x = 10, p1.y = 15
p2.x = 10, p2.y = 15
p1.x = 50, p1.y = 100
p2.x = 50, p2.y = 100