|
发表于 2023-12-3 19:37:58
|
显示全部楼层
- #include<iostream>
- using namespace std;
- class complex
- {
- public:
- double real, image;
- complex(double r = 0, double i = 0)
- {
- real = r;
- image = i;
- }
- complex add(const complex& c); //定义时要写上参数
- 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;
- }
- }
- };
- complex complex::add(const complex& c) // 在结构体/类外定义成员函数时,要用::标明其从属关系
- {
- complex temp;
- temp.real = this->real + c.real;
- temp.image = this->image + c.image;
- return temp;
- }
- int main()
- {
- complex c1(1, 1), c2(2, 4);
- c1.show();
- c2.show();
- //complex c3=c1.
- return 0;
- }
复制代码 |
|