错误代码:
#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; }
正确代码1:
/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.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() const {return x;}
};
int main(){
const C c(5);
cout<<c.getX();
return 0;
}
正确代码2:
/* * Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.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(){ C c(5); cout<<c.getX(); return 0; }
输出结果: