C++中类重载输出符

C++中类重载输出符,请看示例代码,

#include 

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

你可能感兴趣的:(C++学习,c++,开发语言)