c++输出(<<)操作符重载

代码通过VS2008编译运行

注意:输出和输入操作符,必须声明为类的友元函数

#include <iostream>
#include<fstream>
#include <string>
class CheakoutRecord
{
public:
    int set_book(const double& id,const std::string& topic);
    friend std::ostream& operator<<(std::ostream& ,const CheakoutRecord& );

private:
    double book_id;
    std::string title;

};
int CheakoutRecord::set_book(const double& id,const std::string& topic)
{
    book_id = id;
    title = topic;
    return 0;
}
std::ostream& operator<<(std::ostream& out,const CheakoutRecord& record)
{
    out<<record.book_id<<"\t"<<record.title;
    return out;
}
int main()
{
    CheakoutRecord record;
    record.set_book(123455,"c++primer");
    std::cout<<record<<std::endl;
    system("pause");
    return 0;
}


 

你可能感兴趣的:(c++输出(<<)操作符重载)