程序填空题:复数的加法及输出
下述程序从控制台读取一个复数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;
}
<br>**拼尽全力还是不会?参考B站习题讲解**<br>哔哩哔哩up主:[海洋饼干叔叔](https://space.bilibili.com/384177380) [Python课程](https://www.bilibili.com/video/BV1kt411R7uW/) [Python习题](https://www.bilibili.com/video/BV1iL411t7UZ/)[简洁的C和C++](https://www.bilibili.com/video/BV1it411d7zx/)作者每天分享一篇关于C/C++/Python的技术文章,学习编程不迷路。![image.png](~/7c4cfd2d-8e3e-40cd-826d-299d4200e600.png)
答案:
第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);
#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;
}
<br>**拼尽全力还是不会?参考B站习题讲解**<br>哔哩哔哩up主:[海洋饼干叔叔](https://space.bilibili.com/384177380) [Python课程](https://www.bilibili.com/video/BV1kt411R7uW/) [Python习题](https://www.bilibili.com/video/BV1iL411t7UZ/)[简洁的C和C++](https://www.bilibili.com/video/BV1it411d7zx/)作者每天分享一篇关于C/C++/Python的技术文章,学习编程不迷路。![image.png](~/7c4cfd2d-8e3e-40cd-826d-299d4200e600.png)
答案:
第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);