输入输出运算符重载

来源:互联网 发布:网络社会工作局局长 编辑:程序博客网 时间:2024/05/20 04:13
#include <iostream>


using namespace std;
class A{
public:
    int i;
    int j;
    A(int m, int n):i(m),j(n){


    }
    ~A(){


    }
};


ostream&  operator<<(ostream &os, A &a)   //输出运算符重载,返回引用,//属性是private修饰的时候,需要添加友元
{
    os <<"a.i = " << a.i << "\t" << "a.j = " << a.j << endl;
    return os;
}


istream& operator>>(istream &in,A &a)   //输出运算符重载,返回引用,//属性是private修饰的时候,需要添加友元
{
    in >> a.i >> a.j;
    return in;
}
int main()
{
    A a(12,13);
    cin >> a;
    cout << a << endl;


    return 0;
}