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;
}