定义名为Box类,包含长宽高,包含输出体积成员函数,表面积成员函数。
#include
using namespace std;
class box
{
double length, width, height,area;
public:
static int n;
static double total;
box(double l,double w ,double h);
~box();
void volume(box& obj);
};
int box::n = 0;
double box::total = 0;
box::box(double l, double w, double h):length(l),width(w),height(h)
{
n++;
total += l * w * h;
}
//需要写一个析构函数
box::~box()
{
n--;
total -= length * width * height;
}
void box::volume(box& obj)
{
obj.area = (obj.length * obj.width * obj.height);
cout <<obj.area<<endl;
}
int main()
{
box obj(1,2 ,3 );
cout << "obj的area为: ";
obj.volume(obj);
box obj1(2, 3, 4);
cout << "obj1的area为: ";
obj1.volume(obj1);
box obj2(3, 4, 5);
cout << "obj2的area为: ";
obj2.volume(obj2);
cout << "一共有 " << box::n << " 个box类的对象 " << "总体积为: " << box::total;
}