第五周项目零:阅读程序(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; }
运行结果:
相关文章推荐
- 第五周 阅读程序 4 const 的用法
- 第五周项目零:阅读程序(2):指针类
- 第五周 程序阅读——const
- 第五周项目零:阅读程序(3):指针类
- 第五周项目零:阅读程序(5):static
- 第五周项目零:阅读程序(6):static .
- 第五周项目程序阅读一
- 第五周项目零:阅读程序(1):指针类
- 第5周项目--程序阅读——指针、const、static
- 第十五周项目—阅读程序(6)
- 第十四周项目 阅读程序 2
- 第16周项目——阅读程序4
- 第十周第十一周项目1阅读程序(8)
- 第十四周项目:程序阅读(二)
- 第十六周上机实践项目1(3):阅读程序
- 第五周程序阅读(3)
- 第十二周上机实践项目-阅读程序-1-(3)
- fw: 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
- 第十四周项目6-阅读程序2(对a[i++]=的理解)
- 第十二周项目一——阅读程序(2)