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;
}

 

posted @ 2018-12-14 17:10  Stephen~Jixing  阅读(625)  评论(0编辑  收藏  举报