【任务】下面的程序存在编译错误。有两种方法可以修改,请给出这两种修改方案,在报告中说明你倾向于用哪一种?为什么?处理此类问题的原则是什么?
class C
{
private:
int x;
public:
C(int x){this->x = x;}
int getX(){return x;}
};
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() const {return x;}
};
void main()
{
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");
}
赶!赶!赶!好累!