C++输入输出重载

#include <iostream>

using namespace std;

class Complex2
{
public:

    Complex2(float x = 0, float y = 0)
        :_x(x), _y(y){

        

    }
    void dis()
    {
        cout << "(" << _x << "," << _y << ")" << endl;
    }
    friend  ostream & operator<<(ostream  &os, const Complex2 &c);
    friend  istream & operator>>(istream  &is, Complex2 &c);

private:
    float _x;
    float _y;
};

ostream &operator<<(ostream  &os, const Complex2 &c)
{
    os << "(" << c._x << "," << c._y << ")";
    return os;
}

istream & operator>>(istream  &is, Complex2 &c)
{
    is >> c._x >> c._y;
    return is;
}
int main()
{
    Complex2 c(2, 3);
    cout << c << endl;
    cin >> c;
    cout << c << endl;
    cin.get();
    return 1;
}
原文地址:https://www.cnblogs.com/Stephen-Jixing/p/10120494.html