c++设计一个立方体类box,它能计算并输出立方体的体积,表面积以及所有边的边长和。

c++设计一个立方体类box,它能计算并输出立方体的体积,表面积以及所有边的边长和。

你可以参考一下

#include<iostream>
using namespace std;
class Box
{
    int length;
    int width;
    int height;
public:
    Box(int, int, int);
    int area(); //计算面积的函数
    int volume(); //计算体积的函数
    int sum();//计算边长和的函数
    void inputdate();//输入数据函数

};
Box::Box(int h=1, int w=1, int len=1)
{
    height = h;
    width = w;
    length = len;
}
int Box::area()
{
    return (height * width + height * length + width * length) * 2;
}
int Box::volume()
{
    return (height * width * length);
}
int Box::sum()
{
    return (height + width + length) * 4;
}
void Box::inputdate()
{
    cout << "请输入立方体的长宽高" << endl;
    cin >> length >> width >> height;
}

int main(int h,int w,int l)
{
    Box box1;
    box1.inputdate();
    cout <<"立方体的表面积是" << box1.area() << endl;
    cout << "立方体的体积是" << box1.volume() << endl;
    cout << "立方体的边长和是" << box1.sum() << endl;
    return 0;
}

写好了,你可以参考一下

#include <iostream>
using namespace std;
/*
    c++设计一个立方体类box,它能计算并输出立方体的体积,表面积以及所有边的边长和。
*/

class box {
private:
    //长宽高
    double x, y, z;
public:
    //无参构造方法
    box() {
        this->x = 1;
        this->y = 1;
        this->z = 1;
    }

    //有参构造方法
    box(double x, double y, double z) {
        this->x = x;
        this->y = y;
        this->z = z;
    }

    //计算体积
    void calVolume() {
        double v = x * y * z;
        cout << "立方体的体积为:" << v << endl;
    }

    //计算表面积
    void calArea() {
        double s = 2 * (x * y + y * z + z * x);
        cout << "立方体的表面积为:" << s << endl;
    }

    //计算边长和
    void calLength() {
        double l = 2 * (x + y + z);
        cout << "立方体所有边的和为:" << l << endl;
    }
};

int main()
{
    box b1;
    b1.calVolume();
    b1.calArea();
    b1.calLength();

    box b2(3, 4, 5);
    b2.calVolume();
    b2.calArea();
    b2.calLength();
    
    return 0;
}

有帮助的话记得点个采纳支持一下博主哦