問題:求5個長方柱的體積和表面積
我的程式:
/*
Copyright (c) 2013, 煙台大學計算機學院
* All rights reserved.
* 作 者:趙玲玲
* 完成日期:2014 年 3 月 26日
* 版 本 号:v1.0
* 輸入描述: 無
* 問題描述: 長方體類
* 程式輸出:
* 問題分析:
* 算法設計:略
*/
#include <iostream>
using namespace std;
class Bulk //長方體
{
public:
Bulk(double l=1,double w=1,double h=1); //初始化
int Super(); //求表面積
int Volume(); //求體積
void get_value(); //輸入資料
void output(int); //輸出
private:
double length; //長
double width; //寬
double heigth; //高
};
Bulk::Bulk(double l,double w,double h)
{
length=l;
width=w;
heigth=h;
}
Bulk b[5]= {Bulk(2.3,4.5,6.7),Bulk(1.5,3.4),Bulk(10.5)};
int main()
{
b[4].get_value();
//下面分别輸出這5個長方柱的體積和表面積
int i;
for(i=0; i<5; i++)
{
b[i].output(i);
}
return 0;
}
//輸入資料
void Bulk::get_value()
{
double l,w,h;
cout<<"請輸入長方體的長寬高:";
cin>>l>>w>>h;
b[4]=Bulk(l,w,h); //注意是等于
}
void Bulk::output(int i) //輸出
{
cout<<"第"<<i+1<<"個長方體的表面積為:"<<Super()<<'\t'<<"體積為:"<<Volume()<<endl;
}
//求表面積
int Bulk::Super()
{
return ((length*width)+(heigth*length)+(width*heigth)*2);
}
//求體積
int Bulk::Volume()
{
int i;
for(i=0;i<5;i++)
{
return(length*width*heigth);
}
}
我的結果:

我的體會:預設函數調用時直接用等于号