#include<iostream>
#include<string>
using namespace std;
class Shape {
public:
Shape() {}
virtual ~Shape(){}
virtual float getArea() const { return 0; }
};
//长方形
class Rectangle :public Shape {
public:
Rectangle(float Length, float Width) :length(Length), width(Width) {}
float getArea() const { return length*width; }
float getLength() const { return length; }
float getWidth() const { return width; }
void setLength(float Length) { length = Length; }
void setWidth(float Width) { width = Width; }
private:
float length;
float width;
};
//圆形
class Circle :public Shape {
public:
Circle(float Radius) :radius(Radius){}
/*Circle() {}*/
float getArea() const { return radius*radius*3.14; }
float getRadius() const { return radius; }
void setRadius(float Radius) { radius = Radius; }
private:
float radius;
};
//正方形
class Sqare :public Rectangle {
public:
Sqare(float Side) :Rectangle(Side, Side){}
void setSide(float Side) //设置边长
{
s
继承与派生练习:定义一个基类Shape,在次基础上派生出Rectangle和Circle。二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square.
最新推荐文章于 2022-05-26 15:09:14 发布