/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: c.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()
{ const C c(5);
cout<<c.getX()<<endl;
return 0;
}
修改后的代码(1)
#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()<<endl;
return 0;
}
修改后的代码(2)
#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()<<endl;
return 0;}
运行结果:
原代码中const c是个常对象,无法访问其成员函数
修改后的代码1是去掉const,把c定义为普通对象
代码2是将把他的成员函数定义为const函数