作业帮 > 综合 > 作业

求大神用c++设计一个长方体类,包括长,宽,高等私有数据成员,用友元函数的方法求长方体的体积.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/04/27 15:33:26
求大神用c++设计一个长方体类,包括长,宽,高等私有数据成员,用友元函数的方法求长方体的体积.
用c++设计一个长方体类,包括长,宽,高等私有数据成员,用友元函数的方法求长方体的体积
class Cuboid {
friend int Volume (const Cuboid&);
int length;
int width;
int height;
};
int Volume (const Cuboid& c)
{
return c.length * c.width * c.height;
}
再问: 请问这是一个完整的程序吗?
再答: 不,上面的程序只是根据你的需求写的基本的程序片段。
完整的程序是这样的:
#include
class Cuboid {
friend int Volume (const Cuboid&);
public:
Cuboid(int l, int w, int h) : length(l), width(w), height(h) {}
private:
int length;
int width;
int height;
};
int Volume (const Cuboid& c)
{
return c.length * c.width * c.height;
}
int main()
{
using namespace std;
Cuboid a(1, 2, 3);
cout