![](https://csdnimg.cn/release/download_crawler_static/80933614/bg4.jpg)
文档供参考,可复制、编制,期待您的好评与关注!
4 / 17
Box(int h,int w ,int len):
height(h),width(w),length(len){}
int volume();
private:
int height;
int width;
int length;
};
Box::Box()
{ height=10;
width=10;
length=10;
}
int Box::volume()
{ return(height*width*length); }
int main()
{
Box box1;
cout<<"The volume of box1 is "
<<box1.volume()<<endl;
Box box2(15,30,21);
cout<<"The volume of box2 is "
<<box2.volume()<<endl;
return 0;
}
The volume of box1 is 1000
The volume of box2 is 9450
9.1.6 使用默认参数的构造函数
例 9.4
#include <iostream>
using namespace std;
class Box
{
public:
Box(int w=10,int h=10,int len=10);
int volume();
private:
int height;
int width;
int length;
};
Box::Box(int w,int h,int len)
{ height=h;
width=w;
length=len;
}
int Box::volume()
{ return(height*width*length); }
int main()
{
Box box1;
cout<<"box1:"<<box1.volume()<<endl;
Box box2(15);
cout<<"box2:"<<box2.volume()<<endl;
Box box3(15,30);
cout<<"box3:"<<box3.volume()<<endl;