/* (程序頭部註釋開始)
* 程序的版權和版本聲明部分
* Copyright (c) 2011, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱:
* 作 者: 王琳
* 完成日期: 2012年 3 月21日
* 版 本 號:5-3
* 對任務及求解方法的描述部分
* 輸入描述:前4個長方柱(即數組的前4個元素)要在定義數組時初始化,其中前3個直接給出參數初始化,
第4個用默認構造函數初始化;第5個長方柱定義時不初始化,而是由鍵盤輸入長、寬、高賦值;
* 問題描述:需要定義長方柱類,5個長方柱採用一個對象數組表示;
* 程序輸出:輸出這5個長方柱的體積和表面積;
* 程序頭部的註釋結束
*/
源程序:
#include <iostream>
using namespace std;
class cuboid
{
public:
cuboid();
cuboid(double,double,double);
void set_value();
double get_V();
double get_S();
void display();
private:
double length;
double width;
double height;
};
void cuboid::set_value()
{
cout<<"請輸入長方體的長,寬,高:";
cin>>length;
cin>>width;
cin>>height;
}
cuboid::cuboid()
{
length=6,width=6,height=8;
}
cuboid::cuboid(double len,double wid,double hei):length(len),width(wid),height(hei){};
double cuboid::get_V()
{
double V;
V=length*width*height;
return V;
}
double cuboid::get_S()
{
double S;
S=2*(length*width+length*height+width*height);
return S;
}
void cuboid::display()
{
cout<<"長方體的體積爲:"<<get_V()<<endl;
cout<<"長方體的表面積爲:"<<get_S()<<endl;
}
int main()
{
cuboid c[5]={cuboid(1.5,2.5,3.5),cuboid(1.6,2.6,3.6),cuboid(3.6,4.6,34.1),cuboid()};
c[4].set_value();
for(int i=0;i<5;++i)
{
cout<<"關於c["<<i<<"]";
c[i].display();
}
system("pause");
return 0;
}
運行結果:
。。。。。
感想:今晚電腦出問題了,圖片一直傳不上去啊,以後電腦好了再補上唄!相當無語啊。。。