C++ 默认参数构造函数

C++面向对象程序设计 谭浩强

include <iostream>

using namespace std;
class Box
{public :
Box(int h=10,int w=10,int len=10); //在声明构造函数时指定默认参数
int volume( );
private :
int height;
int width;
int length;
};
Box::Box(int h,int w,int len) //在定义函数时可以不指定默认参数
{height=h;
width=w;
length=len;
}
int Box::volume( )
{return (heightwidthlength);
}
int main( ){
Box box1; //没有给实参
cout<<″The volume of box1 is ″<<box1.volume( )<<endl;
Box box2(15); //只给定一个实参
cout<<″The volume of box2 is ″<<box2.volume( )<<endl;
Box box3(15,30); //只给定2个实参
cout<<″The volume of box3 is ″<<box3.volume( )<<endl;
Box box4(15,30,20); //给定3个实参
cout<<″The volume of box4 is ″<<box4.volume( )<<endl;
return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。