Copy constructor and assignment operators

#include<iostream>
using namespace std;
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 “;
radius=c.radius;
x=c.x;
y=c.y;
return *this;
}
circle (circle &c) //copy constructor must be passed by reference
{
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<<endl;
}
};
int main()
{
circle c1(10,2.5,3.6);
circle c2,c4;
c4=c2=c1; //assignment operation
circle c3=c1; //copy opertaion
c1.showdata();
c2.showdata();
c3.showdata();
c4.showdata();
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s