3.67
来源:互联网 发布:土木工程用什么软件 编辑:程序博客网 时间:2024/06/07 17:38
3.6 的图为:
7:
(1)把第2行改为const Student stud(101.78.5)后,表明其为常对象,只能调用它的常成员函数,而不能调用它的普通成员函数,即chang函数不能使用,把display函数申明为常函数, 则可以用。
#include <iostream> using namespace std;class Student{public: Student(int n,float s):num(n),score(s){} void change(int n,float s){num=n;score=s;} void display()const{cout<<num<<" "<<score<<endl;}private: int num; float score;};int main(){ const Student stud(101,78.5); stud.display(); return 0;}(2)若要使用chang函数, 而对象是常对象,则要用到mutable,把其声明为可变的数据,并且要使得chang函数可以调用, 需要加上const把它声明为常成员函数
#include <iostream>using namespace std;class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) const {num=n;score=s;} void display() const {cout<<num<<" "<<score<<endl;} private: mutable int num; mutable float score; };int main(){ const Student stud(101,78.5); stud.display(); stud.change(101,80.5); stud.display(); return 0;}(3)用指向对象的指针变量也是可以的。
#include <iostream>using namespace std;class Student {public: Student(int n,float s):num(n),score(s){} void change(int n,float s) {num=n;score=s;} void display() {cout<<num<<" "<<score<<endl;} private: int num; float score; };int main(){ Student stud(101,78.5); Student *p=&stud; p->display(); p->change(101,80.5); p->display(); return 0;}
(4)用指向常对象的指针变量也可以,结果一样。
(5)用指向对象的常指针变量也是一样的。
0 0
- 3.67
- 3.67
- 3.67
- csapp2e 家庭作业 3.67
- 【SICP练习】136 练习3.67
- 笔试面试算法
- HTTP 错误 500.19 - Internal Server Error 的解决方法
- Spark学习之1:Master启动流程
- android源码下载地址
- Android SDK代理服务器解决国内不能更新下载问题
- 3.67
- error LNK2001: 无法解析的外部符号 sqlite3_open
- NYOJ VF
- 使用powerdesigner创建数据库表
- 线上项目替换class文件的陷阱
- fullcalendar日历控件知识点集合
- java(JDBC连接数据库)[完整版封装]
- IIS7 HTTP/405 METHOD NOT ALLOWED_PUT AND DELETE ME
- jQuery调用指定iframe页面内的方法