zoukankan      html  css  js  c++  java
  • c++重载输入输出运算符

    1 最好打断点看看哦

    2例子

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 class Complex2
     6 {
     7 public:
     8 
     9     Complex2(float x = 0, float y = 0)
    10         :_x(x), _y(y){
    11 
    12         
    13 
    14     }
    15     void dis()
    16     {
    17         cout << "(" << _x << "," << _y << ")" << endl;
    18     }
    19     friend  ostream & operator<<(ostream  &os, const Complex2 &c);
    20     friend  istream & operator>>(istream  &is, Complex2 &c);
    21 
    22 private:
    23     float _x;
    24     float _y;
    25 };
    26 
    27 ostream &operator<<(ostream  &os, const Complex2 &c)
    28 {
    29     os << "(" << c._x << "," << c._y << ")";
    30     return os;
    31 }
    32 
    33 istream & operator>>(istream  &is, Complex2 &c)
    34 {
    35     is >> c._x >> c._y;
    36     return is;
    37 }
    38 int main()
    39 {
    40     Complex2 c(2, 3);
    41     cout << c << endl;
    42     cin >> c;
    43     cout << c << endl;
    44     cin.get();
    45     return 1;
    46 }
  • 相关阅读:
    竞赛题解
    学习笔记
    竞赛题解
    学习笔记
    竞赛题解
    竞赛题解
    竞赛题解
    「链接」原博客链接
    「杂录」THUWC 2020 游记
    「杂录」CSP-S 2019 爆炸记&题解
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/7298069.html
Copyright © 2011-2022 走看看