zl程序教程

您现在的位置是:首页 >  其它

当前栏目

使用默认参数的构造函数

参数 默认 构造函数 使用
2023-09-14 09:12:06 时间
 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 
 6 class Box
 7 {
 8     public:
 9         Box(int h=10,int w=10,int len=10);
10         int volume();
11     private:
12         int height;
13         int width;
14         int length;
15 };
16 
17 Box::Box(int h,int w,int len)
18 {
19     height=h;
20     width=w;
21     length=len;
22 }
23 
24 int Box::volume()
25 {
26     return(height*width*length);
27 }
28 
29 int main(int argc, char** argv) {
30     Box box1;
31     cout<<"The volume of box1 is"<<box1.volume()<<endl;
32     Box box2(15);
33     cout<<"The volume of box2 is"<<box2.volume()<<endl;
34     Box box3(15,30);
35     cout<<"The volume of box3 is"<<box3.volume()<<endl;
36     Box box4(15,30,20);
37     cout<<"The volume of box4 is"<<box4.volume()<<endl;
38     return 0;
39 }