C++设计一个长方体类,有长、宽、高三个属性,用成员函数计算;计算并输出长方体的体积和表面积。

2025-01-05 14:38:04
推荐回答(3个)
回答1:

#include using namespace std;class Retangle
{
public:
Retangle(void);
double Volume(void);
double Area(void);
void SetValue(double length, double width, double height);

private:
double length;
double width;
double height;
};Retangle :: Retangle(void)
{
length = 0.0;
width = 0.0;
height = 0.0;
}double Retangle :: Volume(void)
{
return (length * width * height);
}double Retangle :: Area(void)
{
return (2 * (length * width + length * height + width * height));
}void Retangle :: SetValue(double length, double width, double height)
{
this -> length = length;
this -> width = width;
this -> height = height;
}int main(int argc, char *argv[])
{
double length;
double width;
double height;
Retangle retangle;

cout << "Please input lenght, width and height:" << endl;
cin >> length >> width >> height;

retangle.SetValue(length, width, height);

cout << "Volume: " << retangle.Volume() << endl;
cout << "Area: " << retangle.Area() << endl;

return (0);
}

回答2:

#include
using namespace std;
class CFT{
public:
    CFT(double a,double b,double c){
        ma=a;
        mb=b;
        mc=c;
    }
    void output(){
        cout<<"V="<        cout<<"S="<<(ma*mb+ma*mc+mb*mc)*2<        return;
    }
private:
    double ma,mb,mc;
};

回答3:

class Cuboid
{private: double length; double width; double height;public: Cuboid()
{
}
Cuboid(double len,double wid, double hei): length(len),width(wid),height(hei)
{
} double getVolume()
{
return length*width*height;
}
double getArea()
{
return 2*(length*width + length*height + width*height);
}
};
使用方法:Cuboid test(1,2,3);cout << "体积:" << test.getVolume() <