C++ 默认参数构造函数

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

include

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 ″< Box box2(15); //只给定一个实参
cout<<″The volume of box2 is ″< Box box3(15,30); //只给定2个实参
cout<<″The volume of box3 is ″< Box box4(15,30,20); //给定3个实参
cout<<″The volume of box4 is ″< return 0;
}

你可能感兴趣的:(C++ 默认参数构造函数)