/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: c.cpp
* 作者:李慧慧
* 完成日期: 2013年 4 月12 日
* 版本号: 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 w,int l):width(w),length(l){}
int volume( ){return height*width*length;};
private:
static int height; //静态的数据成员
int width;
int length;
};
int Box::height =2;
int main()
{
Box b(3,4);
cout<<"volume is "<<b.volume()<<endl;
return 0;
}
运行结果:
不能用参数初始化表对静态数据成员初始化
静态数据成员只能在类体外进行初始化