嵌入式Linux C++练习3——继承和派生

使用类的继承和派生实现对矩形,正方形,三角形,圆形的定义,并实现对长方体,正方体,球体和圆锥的表面积、体积的计算。

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

const float pi = 3.14;

class Line //线
{
    
    
public:
    Line(float len) : m_len(len) {
    
    }
    virtual float area() = 0;   //面积
    virtual float volume() = 0; //体积

protected:
    float m_len;
};



class Tri : public Line //三角形
{
    
    
public:
    Tri(float len, float height) : Line(len)
    {
    
    
        m_height = height;
    }

    float area()
    {
    
    
        return m_height * m_len / 2;
    }

protected:
    float m_height;
};

class Cir : public Line //圆形
{
    
    
public:
    Cir(float len) : Line(len)
    {
    
    
        m_radius = len;
    }

    float area()
    {
    
    
        return m_radius * m_radius * pi;
    }

protected:
    float m_radius;
};

class Rec : public Line //长方形
//在全部实现基类纯虚函数前,仍然是个抽象类
{
    
    
public:
    Rec(float len, float width) : Line(len)
    {
    
    
        m_width = width;
    }

    float area()
    {
    
    
        return m_width * m_len;
    }

protected:
    float m_width;
};



class Cuboid : public Rec //长方体
{
    
    
public:
    Cuboid(float len, float width, float height) : Rec(len, width)
    {
    
    
        m_height = height;
    }

    float area()
    {
    
    
        return 2 * (m_len * m_width + m_len * m_height + m_width * m_height);
    }

    float volume()
    {
    
    
        return m_width * m_height * m_len;
    }

protected:
    float m_height;
};

class Cube : public Cuboid //正方体
{
    
    
public:
    Cube(float len) : Cuboid(len, len, len) {
    
    }
};

class Cone : public Tri //圆锥
{
    
    
public:
    Cone(float radius, float height) : Tri(radius, height)
    {
    
    
        m_radius = radius;
        m_height = height;
    }

    float area()
    {
    
    
        return (pi * sqrt(m_radius * m_radius + m_height * m_height) * m_radius + pi * m_radius * m_radius);
    }

    float volume()
    {
    
    
        return pi * m_radius * m_radius * m_height / 3;
    }

protected:
    float m_radius;
    float m_height;
};

class Sph : public Line //球体
{
    
    
public:
    Sph(float len) : Line(len)
    {
    
    
        m_radius = len;
    }

    float area()
    {
    
    
        return 4 * pi * m_radius * m_radius;
    }

    float volume()
    {
    
    
        return 4 / 3 * pi * m_radius * m_radius * m_radius;
    }

protected:
    float m_radius;
};

int main()
{
    
    
    Line *p = new Cuboid(1, 2, 3);
    cout << "长方体的表面积是" << p->area() << endl;
    cout << "长方体的体积是" << p->volume() << endl;
    delete p;

    p = new Cube(3);
    cout << "正方体的表面积是" << p->area() << endl;
    cout << "正方体的体积是" << p->volume() << endl;
    delete p;

    p = new Cone(3, 4);
    cout << "圆锥的表面积是" << p->area() << endl;
    cout << "圆锥的体积是" << p->volume() << endl;
    delete p;

    p = new Sph(3);
    cout << "球体的表面积是" << p->area() << endl;
    cout << "球体的体积是" << p->volume() << endl;
    delete p;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45792897/article/details/119424973