• << 链式重载


     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Complex
     6 {
     7 private:
     8     int a, b;
     9 public:
    10     Complex(int a, int b) :a(a), b(b)
    11     {}
    12     friend ostream& operator<<(ostream &out, Complex &c);
    13 
    14     void operator+(Complex &c)          //放在类里
    15     {
    16           this->a = this->a + c.a;
    17           this->b = this->b + c.b;
    18           //return *this;
    19     }
    20 
    21 };
    22 
    23 ostream& operator<<(ostream &out, Complex &c)
    24 {
    25     out << "a = " << c.a << "  b = " << c.b;
    26     return out;
    27 }
    28 
    29 int main()
    30 {
    31     Complex c1(1, 2), c2(3, 4);
    32 
    33     cout << c1 << "showmethemoney" << "aaaaa" << c1 << endl;
    34     //输出c1之后,返回cout(重载函数中手工返回),接着输出两个字符串(系统本身功能),再次返回cout(系统函数所定义的返回),最后又输出c1(重载函数功能),返回cout,最后endl;
    35     system("pause");
    36     return 0;
    37 }
  • 相关阅读:
    sql基础笔记备忘
    论坛---挂掉的思考
    http response
    跑步肋骨下方疼
    Golang
    svn rollback: 恢复到上一版本
    中国时区
    crontab
    初识redis
    siege
  • 原文地址:https://www.cnblogs.com/c-slmax/p/5184128.html
Copyright © 2020-2023  润新知