#include<iostream>
using namespace std;
class Cuboid
{
public:
Cuboid();
Cuboid(double a, double b, double c):length(a),width(b),heigth(c) {}
void set_cuboid();
double volume();
double areas();
void display();
private:
double length;
double width;
double heigth;
bool is_cuboid(double, double, double);
};
Cuboid::Cuboid()
{
length = 4;
width = 5;
heigth = 6;
}
int main()
{
Cuboid c[5] =
{
Cuboid(10, 12, 20),
Cuboid(20, 25, 30),
Cuboid(30, 14, 28),
Cuboid()
};
for(int i = 0; i < 4; ++i)
{
c[i].volume();
c[i].areas();
c[i].display();
}
c[4].set_cuboid();
c[4].volume();
c[4].areas();
c[4].display();
return 0;
}
void Cuboid::set_cuboid()
{
cout << "请输入长方体的长、宽、高:" << endl;
while(1)
{
cin >> length >> width >> heigth;
if (! is_cuboid(length, width, heigth))
{
cout << "数据非法,请重新输入:" << endl;
}
else
{
break;
}
}
}
bool Cuboid::is_cuboid(double l, double w, double h)
{
if(l <= 0 || w <= 0 || h <= 0)
{
return false;
}
else
{
return true;
}
}
double Cuboid::volume()
{
return (length * width * heigth);
}
double Cuboid::areas()
{
return (2 * (length * width + length * heigth + width * heigth));
}
void Cuboid::display()
{
cout << "长是" << length << ", 宽是" << width << ", 高是" << heigth << " 的长方体体积是" << volume() << ",面积是" << areas() <<endl;
}
第五周 任务3
最新推荐文章于 2022-02-25 21:57:21 发布