/*
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:text.cpp
* 作者:胡颖
* 完成日期:2013 年 4月 17日
* 版本号:v1.0
*
* 输入描述:无
* 问题描述:找出 程序错误改正并说明原因
* 程序输出:
* 问题分析:
* 算法设计:略
*/
//原程序
#include <iostream>
#include <string>
using namespace std;
class Box
{
public:
Box(int h,int w,int l):height(h),width(w),length(l){}
int volume( ){return height*width*length;};
private:
static int height; //静态的数据成员
int width;
int length;
};
int main()
{
Box b(2,3,4);
cout<<"volume is "<<b.volume()<<endl;
return 0;
}
错误提示:
//修改后
#include <iostream>
#include <string>
using namespace std;
class Box
{
public:
Box(int h,int w,int l):height(h),width(w),length(l){}
int volume( ){return height*width*length;};
private:
int height;
int width;
int length;
};
int main()
{
Box b(2,3,4);
cout<<"volume is "<<b.volume()<<endl;
return 0;
}
理由:该程序将height声明为静态的数据成员,而静态数据成员只能由静态函数访问,int valume()不是静态函数不能使用成员数据;因而将height设为一般类的数据就可被使用。