VC++类和对象的定义与使用
来源:百度知道 编辑:UC知道 时间:2024/06/17 05:56:15
5.现在需要求4个长方体的体积,请编写一个基于对象的程序。数据成员包括长、宽、高。要求用成员函数实现以下功能:
⑴由键盘分别输入每个长方体的长、宽、高;
⑵计算每个长方体的体积;
⑶输出每个长方体的体积。
⑴由键盘分别输入每个长方体的长、宽、高;
⑵计算每个长方体的体积;
⑶输出每个长方体的体积。
这是求一个的体积,求四个的话自己稍微改一下,如果全部都是别人写出来的,那自己又能学到什么呢?
#include <iostream>
using namespace std;
class parallelepiped
{
public:
parallelepiped(double l,double w,double h);
void Recieve();
virtual double Volume();
void Print();
private:
double length_;
double width_;
double height_;
};
parallelepiped::parallelepiped(double l,double w,double h)
{
length_ = l;
width_ = w;
height_ = h;
}
void parallelepiped::Recieve()
{
cout << "Input lenght,width and height:";
cin >> length_ >> width_ >> height_;
}
double parallelepiped::Volume()
{
return length_ * width_ * height_;
}
void parallelepiped::Print()
{
cout << "Volume : " << Volume() << endl;
}
int main()
{
parallelepiped Input(0,0,0)