小学三角形面积公式编写⼀个抽象类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