11) Write a program to demonstrate use of copy constructor

Anonymous School

            (Minds of Hacker)

--------------------------------


Write a program to demonstrate use of copy constructor


C++ Practical



#include<iostream> 
using namespace std
  
class Point 
private: 
    int xy
public: 
    Point(int x1int y1) { x = x1y = y1; } 
  
    // Copy constructor 
    Point(const Point &p2) {x = p2.xy = p2.y; } 
  
    int getX()            {  return x; } 
    int getY()            {  return y; } 
}; 
  
int main() 
    Point p1(1015); // Normal constructor is called here 
    Point p2 = p1; // Copy constructor is called here 
  
    // Let us access values assigned by constructors 
    cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY(); 
    cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY(); 
  
    return 0


Output:



p1.x = 10, p1.y = 15 p2.x = 10, p2.y = 15




 *******************Don't Make Learning Hard*****************



Follow us on :-

1) YouTube Channel :- Click Here

2) Blog                     :- Click Here


NextGen Digital... Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...