/*
02.* 程序的版权和版本声明部分
03.* Copyright (c)2012, 烟台大学计算机学院学生
04.* All rightsreserved.
05.* 文件名称: object.cpp
06.* 作者:刘明亮
07.* 完成日期: 2013年 4 月5日
08.* 版本号: v1.0
09.* 输入描述:下面的程序存在编译错误。有两种方法可以修改,请给出这两种修改方案,
在报告中说明你倾向于用哪一种?为什么?处理此类问题的原则是什么?
10.* 问题描述:
11.* 程序输出:
12.*/
#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();
return 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的问题。
一定要看好数据成员,数据函数,普通成员函数是不是const型。