Tuesday, 21 April 2020

Write a program in C++ to highlight the difference between overloaded assignment operator and copy constructor.

#include<iostream.h>
class circle
{
private:   
int   radius;   
float x, y;
public:   
circle( ){     }   
circle( int  rr, float  xx, float  yy )   
{       
radius = rr;       
x = xx;       
y = yy;   
}   
circle  operator =( circle & c )   
{       
cout << endl << "Assignment operator invoked";       
radiius = c.radius;       
x= c.x;       
y= c.y;       
return circle( radius, x, y );   
}   
circle( circle & c )
{       
cout << endl << "copy constructor invoked";       
radius = c.radius;       
x= c.x;       
y= c.y;   
}   
void showdata( )
{       
cout << endl << "Radius = " << radius;       
cout << endl << "X-Coordinate=" << x;       
cout << endl << "Y-Coordinate=" << y;   
}
};
void main( )
{   
circle  c1 ( 10, 2.5, 2.5 );   
circle  c2,c4;   
c4 = c2 = c1;   
circle  c3  = c1;   
c1.showdata( );   
c2.showdata( );   
c3.showdata( );   
c4.showdata( );

No comments:

Post a Comment