|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
class complex
{
double real, image;
public:
complex(double r = 0, double i = 0)
{
real = r;
image = i;
}
/*complex add(const complex& c)
{
complex temp;
temp.real = this->real + c.real;
temp.image = this->image + c.image;
return temp;
}*/
void show()
{
if (image > 0)
{
if (image == 1)
{
cout << real << "+" << "i" << endl;
}
else
cout << real << "+" << image << "i" << endl;
}
else if (image < 0)
{
if (image == -1)
{
cout << real << "-" << endl;
}
else
cout << real << "-" << image << "i" << endl;
}
}
friend complex operator+(const complex& c2)
};
complex operator+(const complex& c2)
{
complex temp;
temp.real = real + c2.real;
temp.image = image + c2.image;
return temp;
}
int main()
{
/*complex c1(1, 1), c2(2, 4);
c1.show();
c2.show();
complex c3 = c1.add(c2);
c3.show();
return 0;*/
complex c1(1,1), c2(2, 4);
c1.show();
c2.show();
complex c3 = c1 + c2;
c3.show();
return 0;
} |
|