gpt4 book ai didi

c++ - 如何声明面积和体积函数

转载 作者:搜寻专家 更新时间:2023-10-31 01:24:40 25 4
gpt4 key购买 nike

<分区>

#include <iostream>
using namespace std;

class Shape {
protected:
int _w, _h;
public:
Shape(int w, int h) : _w(w), _h(h) { }
//declaration of area and volume function
};

class Rectangle : public Shape {
public:
Rectangle(int w, int h) : Shape(w, h) { }
};

class Cube : public Shape {
public:
int _b;
public:
Cube(int w, int h, int b) : Shape(w, h), _b(b) { }
int area() { return 2 * (_w * _h + _w * _b + _b * _h); }
int volume() { return _w * _h * _b; }
};

int main() {
Shape *pt;
int w, h, b, v;
cin >> w >> h >> b;
pt = new Rectangle(w, h);
cout << pt->area() << " ";
if ((v = pt->volume()) == -1)
cout << "Undefined ";
else
cout << v << " ";
pt = new Cube(w, h, b);
cout << pt->area() << " ";
if ((v = pt->volume()) == -1)
cout << "Undefined ";
else
cout << v << " ";
}

对于输入 4 5 8,输出将为 20 Undefined 184 160,在另一个测试用例中,输入为 10 20 10 并且输出是200 Undefined 1000 2000 如何声明和定义 area() 和 volume() 以满足给定的测试用例。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com