C++中类重载输出符,请看示例代码,
#include <iostream>
using namespace std;
class Student {
public:
Student(string name, int grade, int cclass, int age) : _name(name), _grade(grade), _class(cclass), _age(age) {}
friend ostream& operator<< (ostream& os, const Student& student) {
os << student._name << " " << student._grade << " " << student._class << " " << student._age;
return os;
}
private:
string _name;
int _grade;
int _class;
int _age;
};
int main() {
Student stu("Mick", 6, 1, 12);
cout << stu << endl;
return 0;
}
输出为,
Mick 6 1 12