#include
using namespace std;
class complex
{
private:
double real;
double imag;
public:
complex(double r=0.0,double i=0.0){real=r;imag=i;}
complex operator+ (complex c2);
complex operator- (complex c2);
complex operator+ ();
complex operator- ();
void display();
};
complex complex::operator+(complex c2)
{
return complex(real+c2.real,imag+c2.imag);
}
complex complex::operator-(complex c2)
{
return complex(real-c2.real,imag-c2.imag);
}
complex complex::operator+()
{
return complex(+real,+imag);
}
complex complex::operator-()
{
return complex(-real,-imag);
}
void complex::display(){
cout<<"("<}
int main(){
complex c1(1,2),c2(3,4),c3;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
c3=c1+c2;
cout<<"c3=c1+c2=";c3.display();
c3=c1-c2;
cout<<"c3=c1-c2=";c3.display();
c1=-c1;
cout<<"-c1=";c1.display();
return 0;
}
--------------------next---------------------
阅读(1767) | 评论(0) | 转发(0) |