#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student() {}
Student( const string& nm, int sc = 0 ): name(nm), score(sc){}
//(1)下面的const干神马 <u>指定字符串指针为常变量,保证该对象成员不被改变</u>
void set_student( const string& nm, int sc = 0 )
{
name = nm;
score = sc;
}
//(2)下面的const分别干神马?<u>将数据成员声明为常成员函数</u>
const string& get_name() const
{
return name;
}
int get_score() const
{
return score;
}
private:
string name;
int score;
};
//(3)下面的const干神马?<u>把引用声明为常引用,作用是不能在函数中改变实参的值</u>
void output_student(const Student& student )
{
cout << student.get_name() << "\t";
cout << student.get_score() << endl;
}
int main()
{
Student stu( "Wang", 85 );
output_student( stu );
return 0;
}