/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 编写基于对象的程序,求5个长方柱的体积和表面积。
* 作 者: 雷恒鑫
* 完成日期: 2012 年 03 月 19 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
运行结果:
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 编写基于对象的程序,求5个长方柱的体积和表面积。
* 作 者: 雷恒鑫
* 完成日期: 2012 年 03 月 19 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
#include <iostream>
using namespace std;
int n=0;
class square
{
public:
//square();
square(int l=1,int w=2,int h=3);
void cin_date();
void volume( );
void show_volume( );
void areas( );
void show_areas();
private:
int length;
int width;
int heigth;
};
int main( )
{
square a[5]={square (1,2,3),square (2,3,4),square (3,4,5),square ()};
++n;
//a[0].(1,2,3);
a[0].volume( );
a[0].areas( );
++n;
//a[1].(2,3,4);
a[1].volume( );
a[1].areas( );
++n;
//a[2].(2,3,5);
a[2].volume( );
a[2].areas( );
++n;
a[3].volume( );
a[3].areas( );
++n;
a[4].cin_date() ;
a[4].volume( );
a[4].areas( );
system("PAUSE");
return 0;
}
/*square::square()
{
length=1;
width=2;
heigth=3;
}*/
square::square(int l,int w,int h)
{
length=1;
width=w;
heigth=h;
}
void square::cin_date( )
{
char c1,c2;
int length1,width1,heigth1;
cout<<"请输入"<<"第"<<n<<"个长方柱的长宽高(格式:长:宽:高 )"<<endl;
while(1)
{
cin>>length1>>c1>>width1>>c2>>heigth1;
length=length1;
width=width1;
heigth=heigth1;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入"<<endl;
else
break;
}
return;
}
void square::volume( )
{
double volume;
volume=length*width*heigth;
cout<<"第"<<n<<"个长方柱的体积为:"<<volume<<endl;
return;
}
void square::areas( )
{
double areas;
areas=2*(length*width+length*heigth+width*heigth);
cout<<"第"<<n<<"个长方柱的表面积为:"<<areas<<endl;
return;
}
运行结果:
经验积累:
1.我学会了使用对象数组。