天天看点

求长方体体积

需要求3个长方体的体积,请编一个基于对象的程序。

数据成员包括length、width、height。要求用成员函数实现以下功能:

  1. 由键盘分别输入3个长方体的长、宽、高;
  2. 计算长方体的体积;
  3. 输出3个长方体的体积。

#include

using namespace std;

class Cube{

public:

void set();

void show();

private:

double length;

double width;

double height;

double getVolume();

};

void Cube::set(){

cout<<“请分别输入长方体的长 宽 高:”<<endl;

cin>>length>>width>>height;

}

double Cube::getVolume(){

return lengthwidthheight;

}

void Cube::show() {

cout<<“长方体体积为:”<getVolume()<<endl;

}

int main() {

Cube cube2,cube3,cube1;

cube1.set();

cube2.set();

cube3.set();

cube1.show();

cube2.show();

cube3.show();

return 0;

}

求长方体体积