• C++ 类的继承特性简单运用


    封装一个名为Shape(图形)的父类,从父类中派生两个子类,分别为Circle(圆形),Rect(矩形),父类拥有两个子类的共同特性,面积和周长,两个子类除了继承父类的共性,还需封装各自的成员属性

    类的声明

    #ifndef SHAPE_H
    #define SHAPE_H
    
    class Shape
    {
    protected:
        double per;
        double area;
    
    public:
        Shape():per(0),area(0) {}
        Shape(double per, double area);
        Shape(const Shape& obj);
    
        ~Shape() {}
    };
    
    
    class Circle : public Shape
    {
    private:
        double radius;
    
    public:
        Circle():Shape(0, 0), radius(0) {}
        Circle(double radius);
        Circle(const Circle& obj);
    
        double& Get_Per();
        double& Get_Area();
    
        ~Circle() {}
    };
    
    class Rect : public Shape
    {
    private:
        double length;
        double width;
    
    public:
        Rect():Shape(0, 0), length(0), width(0) {}
        Rect(double length, double width);
        Rect(const Rect &obj);
    
        double& Get_Per();
        double& Get_Area();
    
        ~Rect() {}
    };
    
    #endif // SHAPE_H
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    类的实现

    #include "Shape.h"
    
    Shape::Shape(double per, double area):per(0), area(0)
    {
        this->per = per;
        this->area = area;
    }
    
    Shape::Shape(const Shape& obj)
    {
        per = obj.per;
        area = obj.area;
    }
    
    
    Circle::Circle(double radius):Shape(0, 0), radius(0)
    {
        this->radius = radius;
    }
    
    Circle::Circle(const Circle& obj)
    {
        radius = obj.radius;
        per = obj.per;
        area = obj.area;
    }
    
    double& Circle::Get_Per()
    {
        per = 2*3.14*radius;
    
        return per;
    }
    
    double& Circle::Get_Area()
    {
        area = 3.14 * radius * radius;
    
        return area;
    }
    
    
    Rect::Rect(double length, double width):Shape(0, 0), length(0), width(0)
    {
        this->length = length;
        this->width = width;
    }
    
    Rect::Rect(const Rect &obj)
    {
        length = obj.length;
        width = obj.width;
        per = obj.per;
        area = obj.area;
    }
    
    double& Rect::Get_Per()
    {
        per = 2 * (length + width);
    
        return per;
    }
    
    double& Rect::Get_Area()
    {
        area = length * width;
    
        return area;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    最终的效果图

    在这里插入图片描述

  • 相关阅读:
    各种业务场景调用API代理的API接口教程
    昨天面试的时候被提问到的问题集合(答案)
    linux升级glibc-2.28
    【情态动词练习题】Can / Could you
    链上物理资产「规模化」或将推动产业协作互联网迎来爆发
    PaLM-E: An Embodied Multimodal Language Model
    vue考核点示例(仅供参考)
    Es6 箭头函数
    (附源码)ssm本科教学合格评估管理系统 毕业设计 180916
    Matlab / Arcgis处理nc数据
  • 原文地址:https://blog.csdn.net/m0_72847002/article/details/132839165