18_1.cpp
来自「本文档是(作者:钱能)《C++程序设计教程》课后习题答案。 选题编辑:张朝阳 」· C++ 代码 · 共 34 行
CPP
34 行
#include <iostream.h>
class Complex{
public:
Complex(double r=0, double v=0):real(r),virt(v){}
friend Complex operator+(Complex a, Complex b);
friend ostream& operator<<(ostream& out, Complex& a);
private:
double real;
double virt;
};
ostream& operator<<(ostream& out, Complex& a){ return out <<a.real <<"+" <<a.virt <<"i\n"; }
Complex operator+(Complex a, Complex b)
{
return Complex(a.real+b.real, a.virt+b.virt);
}
void main()
{
Complex a(2,5), b(7,8), c(0,0);
c = a + b;
cout <<c;
c = 4.1 + a;
cout <<c;
c = b + 5.6;
cout <<c;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?