C++实现新的class有一些习俗,比如会提供一个 print(ostream & out)的公开的成员函数,例子如下
class Employee {
public:
void print(ostream& out) const {
out << name << salary << endl;
}
private:
string name;
double salary;
};
// Define an output operator for
// Employee
ostream& operator<<(ostream& out, const Employee& rhs) {
rhs.print(out);
return out;
}
int main() {
Employee person;
cout << person << endl;
}