建立一个复数类 Complex,要求 1. 含两个私有数据成员:real, imaginary: float 2. 具有三种构造函数:a.不带参数构造: 0+0i b.以实部和虚部构造 c.拷贝构造函数 3.公有成员函数包括如下功能: 输出这个复数 与另一复数的加法函数,要求函数原型为 complex add(complex c); 判断与另一复数是否相等 要求函数原型为bool isEqual(complex c); 在main函数中,输入两个复数,判断两个复数是否相等(实部和虚部要分别相等),然后两者相加之和赋值给一个新的复数,并输出
时间: 2023-04-12 16:00:57 浏览: 173
好的,我将扮演一个由 CSDN 开发的 C知道,我会尽力回答你的问题。关于你的第一个问题,建立一个复数类 Complex,以下是完整代码:
```cpp
#include <iostream>
using namespace std;
class Complex {
private:
float real;
float imaginary;
public:
Complex() {
real = 0;
imaginary = 0;
}
Complex(float r, float i) {
real = r;
imaginary = i;
}
Complex(const Complex& c) {
real = c.real;
imaginary = c.imaginary;
}
void print() {
cout << real << " + " << imaginary << "i" << endl;
}
Complex add(Complex c) {
Complex result;
result.real = real + c.real;
result.imaginary = imaginary + c.imaginary;
return result;
}
bool isEqual(Complex c) {
return (real == c.real && imaginary == c.imaginary);
}
};
int main() {
Complex c1(1, 2);
Complex c2(3, 4);
if (c1.isEqual(c2)) {
cout << "c1 and c2 are equal." << endl;
} else {
cout << "c1 and c2 are not equal." << endl;
}
Complex c3 = c1.add(c2);
cout << "c1 + c2 = ";
c3.print();
return 0;
}
```
希望这个代码对你有所帮助!
阅读全文