带参数的构造函数
#include<iostream>
#include<string>
#include<cstdlib>
using namespace std;
class Box
{
public:
Box(int,int,int);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int x,int y,int z)
{
height=x;
width=y;
length=z;
}
int Box::volume( )
{
return (height*width*length);
}
int main()
{
Box box1(12,25,30);
cout<<box1.volume()<<endl;
Box box2(15,30,21);
cout<<box2.volume()<<endl;
system("pause");
return 0;
}
/*
用参数初始化对数据成员初始化
定义构造函数可以改用以下形式,
Box::Box(int h,int w,int len): height(x),width(y),length(len){}
*/
posted on 2011-06-08 22:32 more think, more gains 阅读(277) 评论(0) 收藏 举报
浙公网安备 33010602011771号