标签:vat 返回 color png 定义 类型 报错 结果 期望
void operator<<(ostream &out, cplxaddself &c1) { out << "陈培昌" << endl; out << c1.a << "+" << c1.b<<"i" << endl; }
#include <iostream> #include<string.h> using namespace std; class cplxaddself { public: friend void operator<<(ostream &out, cplxaddself &c1); cplxaddself::cplxaddself(int a,int b) { this->a = a; this->b = b; } void printmyself() { cout <<"复数:"<< this->a << "+" << this->b << "i" << endl; } private: int a; int b; };
void main() { cplxaddself c1(4, 6),c3(0,0); cplxaddself c2 = c1; cout << c2; system("pause"); }
输出结果:
皆大欢喜,.......不过,要是以为事情结束那就太简单了
如果我们稍加修改就报错了,其实真正实现的理想方案是满足链式编程
void main() { cplxaddself c1(4, 6),c3(0,0); cplxaddself c2 = c1; cout << c2<<endl;//报错 system("pause"); }
所以位移运算需要重载为
ostream& operator<<(ostream &out,cplxaddself &cplx) { out << "queen of rain" << endl; out << cplx.a << "+" << cplx.b << "i" << endl; return out; }
void main() { cplxaddself c1(4, 6),c3(0,0); cplxaddself c2 = c1; c1++; cout <<"before:"<< c2<<" after: "<<c1; system("pause"); }
输出结果:
标签:vat 返回 color png 定义 类型 报错 结果 期望
原文地址:https://www.cnblogs.com/saintdingspage/p/12044425.html