问题及代码:
/*
*Copyright(c)2016,烟台大学计算机与控制工程学院
*All right reserved.
*文件名称:77.cpp
*作 者:董凯琦
*完成日期:2016年3月30日
*版 本 号:v1.0
*
*问题描述:求3个长方柱的体积。数据成员包括长、宽、高、体积
要求设计成员函数实现下面功能
*输入描述:三个长方珠的长、宽、高
*程序输出:长方柱的体积和表面积
*/
#include<iostream>
#include<cmath>
using namespace std;
class bulk
{
public:
void setbulk(double l,double w,double h);
int volume();
int areas();
private:
double length,width,heigth;
};
int bulk::volume()
{
return length*width*heigth;
}
int bulk::areas()
{
return 2*(length*width+length*heigth+width*heigth);
}
void bulk::setbulk(double l,double w,double h)
{
length=l;
width=w;
heigth=h;
}
int main()
{
bulk a,b,c;
double l,w,h;
cin>>l>>w>>h;
a.setbulk(l,w,h);
cout<<"长方柱的体积为"<<a.volume()<<endl;
cout<<"长方柱的表面积为"<<a.areas()<<endl;
cin>>l>>w>>h;
b.setbulk(l,w,h);
cout<<"长方柱的体积为"<<b.volume()<<endl;
cout<<"长方柱的表面积为"<<b.areas()<<endl;
cin>>l>>w>>h;
c.setbulk(l,w,h);
cout<<"长方柱的体积为"<<c.volume()<<endl;
cout<<"长方柱的表面积为"<<c.areas()<<endl;
return 0;
}
运行结果:
知识点总结:
从这个程序中,掌握了结构函数的运用及处理。
学习心得:
持续努力!