您的位置:首页 > 其它

长方体继承自矩形

2015-06-25 21:59 246 查看
问题及代码:
/*。
*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;
}


运行结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: