编程定义一个Box(盒子)类,在该类定义中包括数据成员:length(长)、width(宽) 和height(高)。成员函数:构造函数设置盒子长、宽和高三个数据;复制函数实现对象的复制;volume函数计算并输出盒子的体积。在main函数中,要求创建Box对象,实现对象的复制,并求盒子的体积。
#include<iostream>
using namespace std;
class BOX
{
private:
double length, width, height;
public:
BOX(double l, double w, double h)
{
length = l;
width = w;
height = h;
}
BOX(const BOX& t) {
length = t.length;
width = t.width;
height = t.height;
}
double getlength() { return length; }
double getwidth() { return width; }
double getheight() { return height; }
};
double volume(BOX box)
{
return box.getlength() * box.getwidth() * box.getheight();
}
int main()
{
BOX box1(1, 3, 5);
cout << "box1的体积:" << volume(box1) << endl;
return 0;
}
#include<iostream>
using namespace std;
class Box {
private:
int length, width, highth;
public:
Box(int l,int w,int h):length(l),width(w),highth(h){}
Box(const Box& B) {
length = B.length;
width = B.width;
highth = B.highth;
}
int Volume() {
return length * width * highth ;
}
};
int main() {
Box box1(10, 20, 30);
Box box2(box1);
cout << "box1 volume: " << box1.Volume() << endl;
cout << "box2 volume: " << box2.Volume() << endl;
return 0;
}