用拷贝构造函数进行对象构造。
个人感觉第二空应该是:Box box4(10,10,10);不知道对不对。
第一空没思路,求大神指点
#include
using namespace std;
class Box
{
public:
Box(int h,int w,int len); //构造函数声明
( 1 ) //拷贝构造函数声明
int volume(); //求立方体体积函数
private:
int height;
int width;
int length;
};
Box::Box(int h,int w,int len)
{
height=h;
width=w;
length=len;
}
Box::Box(Box &a)
{
height=a.height+5;
width=a.width+5;
length=a.length+5;
}
int Box::volume()
{
return(height*width*length);
}
void main()
{
Box box1(5,5,5);
cout<<"The volume of box1 is "<<box1.volume()<<endl;
( 2 ) //用box1对象拷贝构造box2对象
cout<<"The volume of box4 is "<<box2.volume()<<endl;
}