#include<iostream>
using namespace std;
class complex
{
private:
double a;
double b;
public:
complex();
complex(double ax,double bx);
complex operator+(const complex & com);
complex operator-(const complex & com);
complex operator*(const complex & com);
friend complex operator~ (const complex & com);
friend ostream & operator<<(ostream &os,const complex &com);
friend istream & operator >>(istream &is,complex &com);
};
complex::complex(double ax,double bx)
{
a=ax;
b=bx;
}
complex complex::operator+(const complex & com)
{
return complex(a+com.a,b+com.b);
}
complex complex::operator-(const complex & com)
{
return complex(a-com.a,b-com.b);
}
complex complex::operator*(const complex & com)
{
return complex(a*com.a-b*com.b,a*com.b+b*com.a);
}
complex operator~ (const complex & com)
{
return complex(com.a,-com.b);
}
ostream &operator<<(ostream &os,const complex &com)
{
os<<com.a<<"+"<<com.b<<"i";
return os;
}
istream & operator >>(istream &is,complex &com)
{
is >> com.a>>com.b;
return(is);
}
int main()
{
complex c1(3,4);
complex c2(1,2);
cout<<"c1+c2:"<<c1+c2<<endl;
cout<<"c1-c2:"<<c1-c2<<endl;
cout<<"c1*c2:"<<c1*c2<<endl;
cout<<"~c1:"<<~c1<<endl;
system("pause");
return 0;
}
友元实现复数运算
最新推荐文章于 2022-04-19 17:13:31 发布