内容:
说明:
<<操作符重载
示例代码:
// OverloadingOstreamOperator.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
class Person
{
public:
Person( const string& first_name, const string& last_name ) : first_name_( first_name ), last_name_( last_name ) {}
const string& get_first_name() const
{
return first_name_;
}
const string& get_last_name() const
{
return last_name_;
}
private:
string first_name_;
string last_name_;
};
ostream &operator << ( ostream &out, Person &p )
{
cout << "first_name=" << p.get_first_name() << "," << "last_name=" << p.get_last_name();
return out;
}
//by zhaocl
int main()
{
string first_name, last_name, event;
cin >> first_name >> last_name >> event;
auto p = Person( first_name, last_name );
cout << p << " " << event << endl;
system( "pause" );
return 0;
}
知识点:
1、cout << 操作符重载