-->
当前位置:首页 > 题库 > 正文内容

程序填空题:复数的加法及输出

Luz4年前 (2021-09-05)题库2834
下述程序从控制台读取一个复数b的实部和虚部,然后将这个复数与复数a及实数3.2相加,得到复数c并输出。请参考注释将程序补充完整。



#include <iostream>
#include <iomanip>
using namespace std;

class Complex {
double dReal;
double dImage;
public:
//构造函数


//operator+操作符函数


//友元函数声明以帮助operator<<()函数访问Complex类的私有成员

};

ostream& operator<<(ostream& o, const Complex& c){
o << fixed << setprecision(1) << c.dReal << " + " << c.dImage << "i";
return o;
}

int main() {
double dReal, dImage;
cin >> dReal >> dImage;

Complex a(1,1);
Complex b(dReal,dImage);
Complex c = a + b + 3.2;
cout << c << endl;
return 0;
}








答案:
第1空: Complex(double real, double image=0){
dReal = real; dImage = image;
}






第2空: Complex operator+(const Complex& r) const{
return Complex(dReal+r.dReal,dImage+r.dImage);
}




第3空: friend ostream& operator<<(ostream& o, const Complex& c);




发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。