問題及代碼:
/*。
*Copyright(c)2014,煙台大學計算機學院
*All right reserved,
*檔案名:test.cpp
*作者:liu_feng_zi_
*完成日期:2015年6月25日
*版本号:v1.0
*問題描述:長方體繼承自矩形
*輸入描述:
*程式輸出:
*/
#include <iostream>
using namespace std;
class Rectangle //矩形類
{
private:
int length; //矩形的長和寬
int width;
public:
Rectangle();
Rectangle(int l,int w); //構造函數,l、w分别代表長和寬
int getArea(); //求面積
};
class Bulk: public Rectangle //立方體類
{
public:
Bulk(); //預設構造函數
Bulk(int l, int w,int h); //帶參數的構造函數
int getVolume();
private:
int height; //高,長、寬繼承自基類
};
Rectangle::Rectangle():length(0),width(0) {}
Rectangle::Rectangle(int l,int w):length(l),width(w) {}
int Rectangle::getArea()
{
return length*width;
}
//Bulk類的成員函數
Bulk::Bulk():Rectangle(),height(0) {}
Bulk::Bulk(int l, int w,int h):Rectangle(l,w),height(h) {}
int Bulk::getVolume()
{
return (getArea()*height);
};
int main()
{
int x,y,z;
cin>>x>>y>>z;
Bulk b(x,y,z);
cout<<b.getVolume()<<endl;
return 0;
}
運作結果:
