2-5-r 阅读程序

来源:互联网 发布:mysql 组合主键关联 编辑:程序博客网 时间:2024/05/13 23:36

问题及代码:

#include <iostream>#include <string>using namespace std;class Student{public:    Student() {}    Student( const string& nm, int sc = 0 ): name(nm), score(sc){}    //(1)下面的const干神马?__这个const作用为常引用,目的是为了在函数中使用引用变量的时候 不能改变其值___________    void set_student( const string& nm, int sc = 0 )    {        name = nm;        score = sc;}    //(2)下面的const分别干神马?_第一个函数里的前面的const作用为不能改变函数里面的变量值;后面的const是为了让常对象调用函数。    //第二个函数的const也是为了让常对象所调用。__________    const string& get_name() const    {        return name;}    int get_score() const    {        return score;    }private:    string name;    int score;};//(3)下面的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;}


 

 

学习小结:

理论应该是清楚了。

但是感觉用起来感觉不知道如何用,可能是还没有做到那种变量很复杂的项目,感觉目前应该用不到const。

 

0 0
原创粉丝点击