Вы находитесь на странице: 1из 2

/*Binary operator overloading

without friend function*/


# include <iostream.h>
# include <conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Real part:";
cin>>a;
cout<<"imagenary part:";
cin>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};

void main()
{
clrscr();
complex obj1,obj2,result,result1;
cout<<"\nBinary operator overloading without friend function";
cout<<"\n***************************************************\n";
cout<<"Enter the value of first complex number\n";
obj1.getvalue();
cout<<"Enter the value of second complex number\n";
obj2.getvalue();
result=obj1+obj2;
result1=obj1-obj2;
cout<<"\nGiven values:";
cout<<"\n************\n";
obj1.display();
obj2.display();
cout<<"\nResult:";
cout<<"\n******\n";
cout<<"Addition of two complex numbers:\n";
cout<<"*******************************\n";
result.display();
cout<<"subtraction of two complex numbers:\n";
cout<<"**********************************\n";
result1.display();
getch();
}

Вам также может понравиться