《C++第六周实验报告1-1》-----程序改错
2012-03-26 21:26
369 查看
/* 下面的程序存在编译错误。有两种方法可以修改,请给出这两种修改方案, 在报告中说明你倾向于用哪一种?为什么?处理此类问题的原则是什么? */ #include <iostream> using namespace std; 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(){return x;}//将getX定义为常函数int getX()const {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(){return x;} }; void main() { const C c(5);//将const去掉 cout<<c.getX(); system("pause"); } /* 如果就单纯的运行主函数中的内容的话我更倾向于第二种, 因为它保证了数据的安全性,不会更改对象中的数据; 但如果类中还有其他的非常函数的话,不能实现调用,显得不够灵活。 处理此问题就要根据实际的需要进行操作,若没有其他函数就用const修饰对象。 */
相关文章推荐
- 第六周C++实验报告(2)
- 第六周实验报告 程序填空
- 第六周C++上机报告(项目1--改错)
- 第六周C++实验报告(3)
- 第六周C++上机报告(项目2--程序填空a)
- 第六周C++实验报告(4)
- 使用C/C++实现Socket聊天程序(代码+实验报告)
- 第六周实验报告任务 1 下面程序存在编译错误。有两种修改方案,更倾向于那一种?为什么?
- 使用C/C++实现Socket聊天程序(代码+实验报告)
- 第六周C++实验报告
- C++第六周任务一【任务1】下面的程序存在编译错误。有两种方法可以修改,请给出这两种修改方案,在报告中说明你倾向于用哪一种?为什么?处理此类问题的原则是什么?
- c++程序实验报告(12-2)
- 第六周实验报告 任务2程序填空:按要求将缺少的代码填进去
- 2011年 C++软件程序实验报告(三)
- C++程序设计实验报告(四十四)---第六周任务二
- C++第三周【任务2】在实验报告改错,在改动处加注释说明理由
- C++程序设计实验报告(十六)----用循环控制语句编写程序,完成表达式的计算
- C++第六周实验报告
- 《C++第六周实验报告2-1》----按要求将缺少的代码填进去
- c++程序实验报告(15-3)