【任务4】(改自教材P262第6题)仿照你阅读过的程序,编写基于对象的程序,求3个长方柱的体积。数据成员包括长(length)、宽(width)、高(heigth)、体积,要求用成员函数实现下面的功能:
(1)由键盘输入3个长方柱的长、宽、高;
(2)计算长方柱的体积(volume)和表面积(areas);
(3)输出这3个长方柱的体积和表面积;
#include<iostream>
using namespace std;
class cft
{
public:
void set_shu();
void show_volume();
void show_areas();
private:
int length;
int width;
int heigth;
int volume;
int areas;
};
void cft :: set_shu()
{
cout << "请输入长方体的长:";
cin >> length;
cout << "请输入长方体的宽:";
cin >> width;
cout << "请输入长方体的高:";
cin >> heigth;
cout << endl;
}
void cft :: show_volume()
{
volume = length * width * heigth;
cout << "该长方体的体积是:" << volume << endl;
}
void cft :: show_areas()
{
areas = (length * width + length * heigth + width * heigth) * 2;
cout << "该长方体的表面积是:" << areas << endl;
cout << endl;
}
int main()
{
cft t1, t2, t3;
t1.set_shu();
t1.show_volume();
t1.show_areas();
t2.set_shu();
t2.show_volume();
t2.show_areas();
t3.set_shu();
t3.show_volume();
t3.show_areas();
return 0;
}

还没有评论,来说两句吧...