类的继承与多态编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面

用C++编写用类的继承与多态编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数GetArea(),周长GetP额里面... 用C++编写 用类的继承与多态 编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数GetArea(),周长GetP额里面()。主函数从键盘输入长和宽,算矩形的面积。编写好后 发到549274480@qq.com非常感谢 展开
 我来答
mia☆jiancha☆ce6608
2011-10-19 · TA获得超过1526个赞
知道小有建树答主
回答量:713
采纳率:100%
帮助的人:507万
展开全部
#include <iostream>

using namespace std;

class Shape
{
public:
virtual int GetArea() = 0;
virtual int GetP() = 0;
};

class Rectangle : public Shape
{
public:
Rectangle(){}
Rectangle(int width, int height);
virtual int GetArea()
{
return m_width * m_height;
}
virtual int GetP()
{
return (m_width + m_height) * 2;
}

private:
int m_width;
int m_height;
};

Rectangle::Rectangle(int width, int height)
: m_width(width), m_height(height)
{

}

int main ()
{
int w,h;
cout << "input rectangle's width and height:" << endl;
cin >> w >> h;
Rectangle rect(w,h);
cout << "rectangle's area: " << rect.GetArea() << endl;
cout << "rectangle's perimeter: " << rect.GetP() << endl;
return 0;
}
这里是矩形的,圆差不多
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
古月米丽
2011-10-19
知道答主
回答量:41
采纳率:0%
帮助的人:10.3万
展开全部
对象面积的函数GetArea(),周长GetP额里面()
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
焕芳家Q
2011-10-30
知道答主
回答量:28
采纳率:0%
帮助的人:10.5万
展开全部
我们书上好像有呢!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式