yangzhixiaohuli 发表于 2023-12-24 15:43:06

定义长方体类,并生成1个长为12、宽为10、高为5的长方体对象。

定义长方体类,并生成1个长为12、宽为10、高为5的长方体对象。

#include <iostream>
using namespace std;
class Box
{
public:
    Box(int,int,int);
private:
    int length;
    int width;
    int height;
};


{
    length=len;
    width=w;
    height=h;
    cout<<"The Box is created. Parm:length="<<length<<", width="<<width<<", height="<<height<<"."<<endl;
}
int main( )
{
   


    return 0;

这题怎么做

FishC 发表于 2023-12-24 15:43:22

你好!要定义一个长方体类,需要包括类的声明和类的定义两部分。

首先,我们来定义类的声明部分,包括类名和成员函数的声明:

++
class Box
{
public:
    Box(int len, int wid, int hei); // 构造函数声明

private:
    int length;
    int width;
    int height;
};

在类的定义部分,我们可以实现构造函数和其他成员函数:

++
Box::Box(int len, int wid, int hei)
{
    length = len;
    width = wid;
    height = hei;
}

接下来,我们可以生成一个长为12、宽为10、高为5的长方体对象:

++
int main()
{
    Box box(12, 10, 5);

    return 0;
}

这样,我们就创建了一个长方体对象box,并将其长度、宽度和高度分别设置为12、10和5。

希望我的回答对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 定义长方体类,并生成1个长为12、宽为10、高为5的长方体对象。