編寫基於對象的程序,求5個長方柱的體積和表面積

#include<iostream>
using namespace std;
class Round
{
	public:
		Round(int l=10,int w=12,int h=3):heigth(h),width(w),length(l){}
		
        void input();
		int volume();
		int areas();

	private:
		int length;
		int width;
		int heigth;
        
};
int main()
{
	Round a[5]={
		Round(2,4,5),
		Round(5,4,9),
		Round(8,9,3),
		Round(),
		Round()
	};
	cout<<"第1個長方體的體積是"<<a[0].volume()<<"表面積是:"<<a[0].areas()<<endl;
	cout<<"第2個長方體的體積是"<<a[1].volume()<<"表面積是:"<<a[1].areas()<<endl;
	cout<<"第3個長方體的體積是"<<a[2].volume()<<"表面積是:"<<a[2].areas()<<endl;
	cout<<"第4個長方體的體積是"<<a[3].volume()<<"表面積是:"<<a[3].areas()<<endl;
	a[4].input();
   cout<<"它的體積是:"<<a[4].volume()<<"它的表面積是:"<<a[4].areas()<<endl;
   return 0;
  
}
void Round::input()
{ 
	cout<<"請輸入長方體的長:";
	cin>>length;
   cout<<"請輸入長方體的寬:";
	cin>>width;
   cout<<"請輸入長方體的高:";
   cin>>heigth;
}
int Round::volume()
{   int d;
	d=length*width*heigth;
	return d;
}
int Round::areas()
{
	return (2*length*width+2*length*heigth+2*width*heigth);

}

 

經驗積累:1.在設計input函數時,不用再int  長,寬。高,否則不能調用volume函數。

                 2自定義函數必須有(),例如volume(  ),纔是正確的。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章