您的位置:首页 > 其它

第五周项目零:阅读程序(4):const

2015-04-01 17:47 169 查看
代码:

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
    Student() {}
    Student( const string& nm, int sc = 0 ): name(nm), score(sc) {}
    //(1)下面的const干神马?定义nm为常数据成员
    void set_student( const string& nm, int sc = 0 )
    {
        name = nm;
        score = sc;
    }

    //(2)下面的const分别干神马?声明常成员函数
    const string& get_name() const
    {
        return name;
    }

    int get_score() const
    {
        return score;
    }
private:
    string name;
    int score;
};

//(3)下面的const干神马?将对象student定义为const
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;
}



运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: