请编写一个基于对象的程序,求 2 个长方体的体积。数据成员包括:length(长),width(宽),height(高)。要求用成员函数实现以下功能:
(1)由键盘输入 2 个长方体的长、宽、高。
(2)计算长方体的体积。
(3)输出 2 个长方体的体积。#include<iostream>using namespace std;class rectangle{public: void set(); void showmenu();private: float length; float width; float height; float getVolume();};void rectangle::set() { cout << "请分别输入长方体的长宽和高:\n"; //cout << "请输入长方体的长度:" << endl; cin >> length; //cout << " 请输入长方体的宽度:" << endl; cin >> width; //cout << "请输入长方体的高度:" << endl; cin >> height; cout << "长方体的长宽高分别为:" << length << " " << width << " " << height << endl;}float rectangle::getVolume() { return length * width * height;}void rectangle::showmenu() { cout << "长方体的体积为:" << this->getVolume() << endl;}int main() { rectangle rec1, rec2; rec1.set(); rec2.set(); rec1.showmenu(); rec2.showmenu(); return 0;}