C++ OOP - 计算机学习指南 #47
Replies: 1 comment
-
#include <iostream>
#include <string>
class Student {
public:
Student(const std::string &_name,int _age) : name(_name),age(_age){};
std::string getName() const {
return this->name;
}
int getAge() const {
return this->age;
}
private:
std::string name;
int age;
};
std::ostream& operator<<(std::ostream& os, const Student& stu) {
os << stu.getName() << " is " << stu.getAge() << " years old";
return os; // Return the ostream reference
}
int main(void) {
Student st("meowrain",12);
std::cout << st << std::endl;
return 0;
}运算符重载示例 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
C++ OOP - 计算机学习指南
计算机学习指南,这里有从网络上收集的计算机各种方面的知识技能,可供查阅和学习
https://cs.meowrain.cn/Blogs/meowrain/c%2B%2B/c%2B%2B%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/
Beta Was this translation helpful? Give feedback.
All reactions