您的位置:首页 > 其它

第八周项目(3)-长方柱类

2016-05-03 11:04 246 查看
问题描述及代码:

/*
*copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:hellow.cpp
*作者:田甜
*完成日期:2016年5月3日
*版本号:v1.0
*
*问题描述:编写基于对象的程序,求5个长方柱的体积和表面积。长方柱类Bulk的数据成员包括长(length)、宽(width)、高(heigth)等。
*输入描述:长、宽、高。
*程序输出://
*/

#include <iostream>

using namespace std;
class Bulk
{
public:
Bulk(double x=1.0,double y=1.0,double z=1.0):lengh(x),width(y),hight(z){};
void get_value();
void display();
void show(int n);
private:
double lengh,width,hight;
};

void Bulk::get_value()
{
cout<<"please input lengh,width,hight."<<endl;
cin>>lengh>>width>>hight;
}
void Bulk::display()
{
cout<<"The volume is "<<lengh*width*hight<<endl;
cout<<"The surface area is"<<2*(lengh*width+width*hight+lengh*hight)<<endl;
cout<<endl;
}

void Bulk::show(int n)
{
cout<<"BULK"<<n<<":"<<lengh<<" "<<width<<" "<<hight<<endl;
}
int main()
{
int i;
Bulk bulk[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
bulk[4].get_value();
for(i=0;i<5;i++)
bulk[i].show(i);
return 0;
}


测试运行结果:



心得体会:

在定义时可以初始化值,没有传入参数时则为初值。

测试display函数:

#include <iostream>

using namespace std;
class Bulk
{
public:
Bulk(double x=1.0,double y=1.0,double z=1.0):lengh(x),width(y),hight(z){};
void get_value();
void display();
void show(int n);
private:
double lengh,width,hight;
};

void Bulk::get_value()
{
cout<<"please input lengh,width,hight."<<endl;
cin>>lengh>>width>>hight;
}
void Bulk::display()
{
cout<<"The volume is "<<lengh*width*hight<<endl;
cout<<"The surface area is"<<2*(lengh*width+width*hight+lengh*hight)<<endl;
cout<<endl;
}

void Bulk::show(int n)
{
cout<<"BULK"<<n<<":"<<lengh<<" "<<width<<" "<<hight<<endl;
}
int main()
{
int i;
Bulk bulk[5]={Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
bulk[4].get_value();
for(i=0;i<5;i++)
bulk[i].display();
return 0;
}


运行结果:



心得体会:

求值时不一定要在数据成员里定义volume,area,可以直接输出时用公式求出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: