Q2 program for Assignment operator/unary operator(prefix and postfix )/binary operator overloading .
#include<iostream>
using namespace std;
class A
{
int x,y;
public :
A()
{
x=10;
y=20;
}
void show();
A operator +(A obj)
{
A temp;
temp.x=x+obj.x;
temp.y=y+obj.y;
return temp;
}
A operator -(A obj)
{
A temp;
temp.x=x-obj.x;
temp.y=y-obj.y;
return temp;
}
//prefix expression overloaded
A operator ++()
{
x++;
++y;
return *this;
}
//postfix expression overloaded
A operator ++(int )
{
x++;
++y;
return *this;
}
A operator =(A obj)
{
this->x=obj.x;
this->y=obj.y;
return *this;
}
};
void A::show(){
cout<<x<<endl<<y<<endl;
}
int main()
{
A obj1,obj2,obj3;
obj1=obj1+obj2;//overloaded assignment operator and binary operator will be called .
obj1.show();
++obj1; //overloaded unary "prefix" operator will be called .
obj1++; //overloaded unary "post" operator will be called .
obj1.show();
obj3=obj1 ;//overloaded assignment operator will be called .
obj3.show();
return 0;
}
using namespace std;
class A
{
int x,y;
public :
A()
{
x=10;
y=20;
}
void show();
A operator +(A obj)
{
A temp;
temp.x=x+obj.x;
temp.y=y+obj.y;
return temp;
}
A operator -(A obj)
{
A temp;
temp.x=x-obj.x;
temp.y=y-obj.y;
return temp;
}
//prefix expression overloaded
A operator ++()
{
x++;
++y;
return *this;
}
//postfix expression overloaded
A operator ++(int )
{
x++;
++y;
return *this;
}
A operator =(A obj)
{
this->x=obj.x;
this->y=obj.y;
return *this;
}
};
void A::show(){
cout<<x<<endl<<y<<endl;
}
int main()
{
A obj1,obj2,obj3;
obj1=obj1+obj2;//overloaded assignment operator and binary operator will be called .
obj1.show();
++obj1; //overloaded unary "prefix" operator will be called .
obj1++; //overloaded unary "post" operator will be called .
obj1.show();
obj3=obj1 ;//overloaded assignment operator will be called .
obj3.show();
return 0;
}
Comments
Post a Comment