OJ第三批——Problem K:C++ 長方體繼承自矩形

問題及代碼:

Problem K: C++ 長方體繼承自矩形

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 617  Solved: 481
[Submit][Status][Web Board]

Description

如下的代碼中,定義了Rectangle矩形類,在此基礎上定義Bulk立方體派生類,請在(1)-(6)處填上需要的代碼,使程序輸出指定長、寬、高的立方體的體積。

#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; //高,長、寬繼承自基類
};

//只提交begin到end部分的代碼

//*********** begin *************

//Rectangle類的成員函數
Rectangle::Rectangle():______(1)______ {}

Rectangle::Rectangle(int l,int w):______(2)______ {}

int Rectangle::getArea()
{
return ______(3)______;
}

//Bulk類的成員函數
Bulk::Bulk():______(4)______

Bulk::Bulk(int l, int w,int h):______(5)______

int Bulk::getVolume()
{
______(6)______
};

//*********** end ***************

int main()
{
int x,y,z;
cin>>x>>y>>z;
Bulk b(x,y,z);
cout<<b.getVolume()<<endl;
return 0;
}

Input

長方體的長、寬、高

Output

長文體的體積

Sample Input

3 4 5

Sample Output

60

HINT

 

#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; //高,長、寬繼承自基類
};

//只提交begin到end部分的代碼

//*********** begin *************

//Rectangle類的成員函數
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():height(0) {}

Bulk::Bulk(int l, int w,int h):Rectangle(l,w),height(h) {}

int Bulk::getVolume()
{
    return getArea()*height;
};

//*********** end ***************

int main()
{
    int x,y,z;
    cin>>x>>y>>z;
    Bulk b(x,y,z);
    cout<<b.getVolume()<<endl;
    return 0;
}



 

運行結果:

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