第八周项目(3)-长方柱类
2016-05-03 11:04
246 查看
问题描述及代码:
测试运行结果:
心得体会:
在定义时可以初始化值,没有传入参数时则为初值。
测试display函数:
运行结果:
心得体会:
求值时不一定要在数据成员里定义volume,area,可以直接输出时用公式求出。
/* *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,可以直接输出时用公式求出。
相关文章推荐
- is running beyond physical memory limits. Current usage: 2.0 GB of 2 GB physical memory used; 2.6 GB of 40 GB virtual memory used
- 【框架】PullToRefresh下拉刷新上拉加载
- 毕业设计——系统
- Swift—扩展声明-备
- 软考二进宫-必考知识点
- Linux的sort命令
- 简单的Flume和hive的结合
- 解题报告:ZOJ3457 Absence Number (打表+数学)
- CSS中position:relative;的作用
- Androdi Application作用
- kvo浅析与实例
- 每天一个linux命令(31): /etc/group文件详解
- Linux的sort命令
- 如何控制dedecms描述的长度?
- 关键字static的作用
- MySQL执行计划解读
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项
- error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项