c++day5

作业

实现一个图形类(Shape),包含受保护成员属性:周长、面积,

公共成员函数:特殊成员函数书写

定义一个圆形类(Circle),继承自图形类,包含私有属性:半径

公共成员函数:特殊成员函数、以及获取周长、获取面积函数

定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度

公共成员函数:特殊成员函数、以及获取周长、获取面积函数

在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。

#include 

using namespace std;
class Shape
{
protected:
    double zc;
    double mj;
public:
    //构造函数
    Shape(){cout<<"无参构造"<zc=other.zc;
            this->mj=other.mj;
        }
        return *this;
    }
    //展示
    void show()
    {
        cout<<"周长是:"<r=other.r;
        }
        return *this;
    }
    //周长获取
    void get_zc2()
    {
        double a=3.14*r*2;
        Shape::zc=a;

    }
    //面积获取
    void get_mj2()
    {
        double b=3.14*r*r;
        Shape::mj=b;

    }

};
class Rect:public Shape
{
private:
    int length;
    int width;
public:
    //构造函数
    Rect(int l,int w):length(l),width(w){cout<<"有参构造"<width=other.width;
            this->length=other.length;
        }
        return *this;
    }
    //周长获取
    void get_zc()
    {
        double a=(length+width)*2;
        Shape::zc=a;

    }
    //面积获取
    void get_mj()
    {
        double b=width*length;
        Shape::mj=b;

    }
};
int main()
{
    Circle c1(5);
    c1.get_mj2();
    c1.get_zc2();
    c1.show();
    Rect r1(3,4);
    r1.get_mj();
    r1.get_zc();
    r1.show();

    return 0;
}

c++day5_第1张图片

 思维导图

你可能感兴趣的:(算法)