c++day5

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

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

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

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

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

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

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

#include 

using namespace std;

class Shape
{
protected:
    float perimeter;  //周长
    float area;//面积

public:
    //无参构造函数
    Shape()
    {
        cout<<"Shape:无参构造"<perimeter = other.perimeter;
            this->area = other.area;
        }
        cout<<"Shape: 拷贝赋值函数"<radius = other.radius;
        }
        cout<<"Circle: 拷贝赋值函数"<perimeter=2*3.14*radius;
        return perimeter;
    }
    //获取面积函数
    using Shape::area;
    float area_get()
    {
        this->area=3.14*radius*radius;
        return area;
    }
};
class Rect:public Shape
{
private:
    int length;
    int hight;
public:
    //无参构造函数
    Rect()
    {
        cout<<"Rect:无参构造"<length = other.length;
            this->hight = other.hight;
        }
        cout<<"Rect: 拷贝赋值函数"<perimeter=2*(length+hight);
        return this->perimeter;
    }
    //获取面积函数
    float area_get()
    {
        this->area=length*hight;
        return this->area;
    }
};
int main()
{
    Circle c1(5);
    c1.perimeter_get();//获取圆形周长
    c1.area_get();//获取圆形面积
    c1.show();//打印周长面积
    Rect r1(4,5);
    r1.perimeter_get();//获取矩形周长
    r1.area_get();//获取矩形面积
    r1.show();//打印周长面积
    return 0;
}

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