/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: object.cpp
* 作者:杨晨
* 完成日期: 2013年4 月 5 日
* 版本号: v1.0
* 输入描述:无
* 问题描述:无
* 程序输出:无
*/
#include<iostream>
#include<stdlib.h>
using namespace std;
class C
{private:
int x;
public:
C(int x){this->x = x;}
int getX(){return x;}
};
int main()
{ C c(5);
cout<<c.getX();
return 0;
}
#include<iostream>
#include<stdlib.h>
using namespace std;
class C
{private:
int x;
public:
C(int x){this->x = x;}
int getX()const {return x;}
};
int main()
{ const C c(5);
cout<<c.getX();
return 0;
}
输出结果:
心得体会:
合理运用const,可以更好的保护数据不被修改和变动,我更倾向于第二种改正方法,第二种方法保证了数据成员的封闭性。