输入输出运算符重载

#include<iostream>
using namespace std;
class Student
{
    public:
        Student(int x,string s)
        {
            num  = x;
            name = s;
        }
    friend  ostream  &operator<<(ostream &os,const Student &stu);  //声明为友元
    private:
        int      num;
        string   name;
};

ostream  &operator<<(ostream &os,const Student &stu)
{
    os<<stu.num<<" "<<stu.name;
    return os;
}

int main()
{
    Student  stu(10,"liu");

    //调用者在使用的时候决定是否换行
    cout<<stu<<endl;

    return 0;
}

阅读更多
换一批

没有更多推荐了,返回首页