实验目的:学会常对象
实验内容:编码改错
* 程序头部注释开始
* 程序的版权和版本声明部分
* 烟台大学计算机学院学生
* 文件名称:
* 作
* 完成日期:
* 版本号:
* 程序头部的注释结束(此处也删除了斜杠)
#include <iostream>
using namespace std;
class C
{
private:
int x;
public:
C(int x){this->x = x;}
int getX() const{return x;} //把成员函数改成const型
};
void main()
{
const C c(5);
cout<<c.getX();
system("pause");
}
#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);//把对象改成一般型
cout<<c.getX();
system("pause");
}
我比较喜欢第一种改法,可以让我熟悉常对象。