Thursday, December 13, 2012

Write a program of overloading operator using binary operator inn c++


#include<iostream.h>
#include<conio.h>
class Demo
{
float x,y;
public:
Demo()
{}
Demo(float real,float img)
{
x=real;
y=img;
}
Demo operator +(Demo);
void show();
};
Demo Demo::operator +(Demo ob)
{
Demo temp;
temp.x=x+ob.x;
temp.y=y+ob.y;
return (temp);
}
void Demo::show()
{
cout<<x<<"+j"<<y<<"\n";
}
void main()
{
clrscr();
Demo ob1,ob2,ob3;
ob1=Demo(2.5,3.00);
ob2=Demo(5.55,5.25);
ob3=ob1+ob2;
cout<<"ob1=";ob1.show();
cout<<"ob2=";ob2.show();
cout<<"ob3=";ob3.show();
getch();
}

Output