本题自定义一个复数类,类名Complex,包含实部real和虚部imag,定义如下。
本题3个空,请填写完整。
#include<iostream>
using namespace std;
class Complex
{
public:
Complex() { Set(0, 0); } //无参构造函数声明及定义
Complex(double r, double i); //有参构造函数声明
void Set(double r, double i);
void Output();
double GetReal() { return real; }
double GetImag() { return imag; }
private:
double real, imag;
};
Complex::Complex(double r, double i)
{
Set(r, i);
}
void Complex::Set(double r, double i)
{
real = r;
imag = i;
}
void Complex::Output()
{
cout << real << "+" << imag << "i" << endl;
}
int main()
{
double m, n;
cin >> m >> n;
Complex p1;
p1.Output();
p1.Set(m, n);
p1.Output();
Complex p2(1, 2);
cout << p2.GetReal() << "+" << p2.GetImag() << "i" << endl;
return 0;
}
输入样例:
3 5
输出样例:
0+0i
3+5i
1+2i