第六周实验报告(1)

来源:互联网 发布:淘宝打包员有年轻人吗 编辑:程序博客网 时间:2024/04/30 00:51

* 程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:   改错                       
* 作    者:        李瑞                  
* 完成日期:  2012 年3月  25日
* 版 本 号:        v1.0

* 对任务及求解方法的描述部分
* 输入描述:由程序产生随机数作为初始值
* 问题描述:……
* 程序输出:……
* 程序头部的注释结束

#include<iostream>using namespace std;class C{private:int x;public:C(int x){this->x = x;}int getX(){return x;}};void main(){    C c(5);      //将原程序中 const  去掉     cout << c.getX();    system("pause");}//以下是第二种解法#include<iostream>using namespace std;class C{private:int x;public:C(int x){this->x = x;}int getX()const{return x;}  //常对象 只能访问 常函数};void main(){        const C c(5);        cout << c.getX();        system("pause");}

 

运行结果:


上机感言:
如果一个对象被定义为常对象,则不能调用该对象的 非const型 的成员函数。无论哪种方法,效果都是一样的。

原创粉丝点击