类的继承与派生(Shape例子派生Rectangle和Circle,Rectangle派生出Square)

定义一个基类Shape,在此基础上派生出Rectangle和Circle。二者都有getArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square。
下面是源代码:

#include<cstdio>
#include<iostream>
using namespace std;

class Shape
{
public:
    Shape(){}
    ~Shape(){}
    virtual float getArea()
    {
        return -1;
    }
};

class Circle:public Shape
{
public:
    Circle(float radius):itsRadius(radius){}
    ~Circle();
    float getArea(){return 3.14*itsRadius*itsRadius;}
private:
    float itsRadius;
};

class Rectangle:public Shape
{
public:
    Rectangle(float len,float width):itsLength(len),itsWidth(width){};
    ~Rectangle();
    virtual float getArea()
    {
        return itsLength*itsWidth;
    }
    virtual float getLength()
    {
        return itsLength;
    }
    virtual float getWidth()
    {
        return itsWidth;
    }
private:
    float itsLength;
    float itsWidth;
};

class Square:public Rectangle
{
public:
    Square(float len);
    ~Square(){};
};

Square::Square(float len):Rectangle(len,len)
{

}
int main()
{
    Shape*sp;
    sp=new Circle(5);
    cout<<"The area of the Circle is"<<sp->getArea()<<endl;
    delete sp;
    sp=new Rectangle(4,6);
    cout<<"The area of the Rectangle is"<<sp->getArea()<<endl;
    delete sp;
    sp=new Square(5);
    cout<<"The area of the Square is"<<sp->getArea()<<endl;
    delete sp;
    return 0;
}
  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于C++继承派生的问题,让我来解答一下。 首先,我们要定义一个基Shape: ```cpp class Shape { public: virtual double getArea() = 0; // 获取面积的纯虚函数 virtual double getPerimeter() = 0; // 获取周长的纯虚函数 }; ``` 在这个基中,我们定义了两个纯虚函数`getArea()`和`getPerimeter()`,分别用于获取形状的面积和周长。由于不同形状的计算方式不同,因此这两个函数没有具体的实现,而是由派生实现。 接下来,我们可以定义Rectangle和Ellipse: ```cpp class Rectangle : public Shape { public: Rectangle(double w, double h) : width(w), height(h) {} virtual double getArea() override { return width * height; } virtual double getPerimeter() override { return 2 * (width + height); } private: double width, height; }; class Ellipse : public Shape { public: Ellipse(double r1, double r2) : radius1(r1), radius2(r2) {} virtual double getArea() override { return 3.14 * radius1 * radius2; } virtual double getPerimeter() override { return 2 * 3.14 * sqrt((radius1 * radius1 + radius2 * radius2) / 2.0); } private: double radius1, radius2; }; ``` 在这两个派生中,我们实现了基中定义的纯虚函数,分别用于计算矩形和椭圆的面积和周长。 接下来,我们可以定义SquareCircle: ```cpp class Square : public Rectangle { public: Square(double s) : Rectangle(s, s) {} }; class Circle : public Ellipse { public: Circle(double r) : Ellipse(r, r) {} }; ``` 在这两个派生中,我们分别继承Rectangle和Ellipse,并定义了Square和Circle的构造函数。由于Square和Circle是特殊的矩形和椭圆,因此它们的计算方式与Rectangle和Ellipse相同,只是参数不同。 最后,我们可以使用这些来计算面积和周长: ```cpp int main() { Rectangle rect(3, 4); cout << "Rectangle area: " << rect.getArea() << endl; cout << "Rectangle perimeter: " << rect.getPerimeter() << endl; Ellipse ellipse(3, 4); cout << "Ellipse area: " << ellipse.getArea() << endl; cout << "Ellipse perimeter: " << ellipse.getPerimeter() << endl; Square square(5); cout << "Square area: " << square.getArea() << endl; cout << "Square perimeter: " << square.getPerimeter() << endl; Circle circle(5); cout << "Circle area: " << circle.getArea() << endl; cout << "Circle perimeter: " << circle.getPerimeter() << endl; return 0; } ``` 输结果为: ``` Rectangle area: 12 Rectangle perimeter: 14 Ellipse area: 37.68 Ellipse perimeter: 20.5664 Square area: 25 Square perimeter: 20 Circle area: 78.5 Circle perimeter: 31.4006 ``` 这样,我们就完成了一个简单的C++继承派生例子

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值