//功能:定义盒子Box类,要求具有以下成员:可设置盒子形状;可计算盒子体积;可计算盒子的表面积,要求有默认构造函数、复制构造函数和析构函数。 |
#include <iostream> |
using namespace std; |
class Box //定义盒子Box类 |
{ |
public : |
Box( double L, double W, double H) //构造函数 |
{ |
length=L; |
width=W; |
height=H; |
} |
Box(Box &abj) //拷贝构造函数 |
{ |
length=abj.length; |
width=abj.width; |
height=abj.height; |
} |
~Box(){} //析构函数 |
double volume( double length, double width, double height) //计算体积函数 |
{ |
return length*width*height; |
} |
double area( double length, double width, double height) //计算表面积函数 |
{ |
double S; |
S=length*width+length*height+width*height; |
return 2*S; |
} |
private : |
double length; //定义长宽高 |
double width; |
double height; |
}; |
int main() |
{ |
double L,W,H; |
cout << "请输入盒子的长宽高:" ; |
cin>>L>>W>>H; |
Box B(L,W,H); |
cout << "盒子的体积为:" <<B.volume(L,W,H)<<endl; |
cout << "盒子的表面积为:" << B.area(L,W,H)<<endl; |
|
return 0; |
} |