编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle

更新时间:2023-06-19 20:59:06 阅读: 评论:0

小学三角形面积公式编写⼀个抽象类Shape,在此基础上派⽣出类Rectangle和Circle
版权声明:本⽂为博主原创⽂章,未经博主允许不得转载。 blog.csdn/baishuiniyaonulia/article/details/80233301
请编写⼀个抽象类Shape,在此基础上派⽣出类Rectangle和Circle,⼆者都有计算对象⾯积的函数getArea()、计算对象周长的函数getPerim()
输出:#include <iostream>
#include <cmath>
using namespace std;
class Shape
{
public:
Shape(){}
~Shape(){}
public:
virtual double getArea() const = 0;
virtual double getPerim() const = 0;
骨头英语怎么读private:
};
class Rectangle : public Shape
戴德{
public:
Rectangle(double _length, double _width) : length(_length), width(_width){}        ~Rectangle(){}
public:
double getArea() const {return length * width;}
double getPerim() const {return2 * (length + width);}
private:
double length;
double width;wifi安全助手
};
class Circle : public Shape
无心法师张若昀{
public:
Circle(double _radius) : radius(_radius){}十瀑峡
王语心~Circle(){}
public:
double getArea() const {return radius * radius * M_PI;}
double getPerim() const {return2 * M_PI * radius;}
private:
double radius;
};
int main()
{
Rectangle * rec = new Rectangle(2, 3);
皑如山上雪
Circle * cir = new Circle(3);
cout << rec->getArea() << endl;
cout << rec->getPerim() << endl;
cout << cir->getArea() << endl;
cout << cir->getPerim() << endl;
return0;
}
6
10 28.2743 18.8496

本文发布于:2023-06-19 20:59:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1046010.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:博主   编写   对象   公式
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图