
类的继承与多态编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面
用C++编写用类的继承与多态编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数GetArea(),周长GetP额里面...
用C++编写 用类的继承与多态 编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数GetArea(),周长GetP额里面()。主函数从键盘输入长和宽,算矩形的面积。编写好后 发到549274480@qq.com非常感谢
展开
展开全部
#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;
}
这里是矩形的,圆差不多
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;
}
这里是矩形的,圆差不多
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
对象面积的函数GetArea(),周长GetP额里面()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询