C++day5

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

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

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

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

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

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

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

#include 

#define PI 3.14



using namespace std;


class Shape
{
protected:
    double cir;//周长
    double area;//面积
public:
    Shape()//显性定义无参构造
    {
        cir = 0;
        area = 0;
       cout<<"无参构造函数"<cir = other.cir;
        this->area = other.area;
        cout<<"拷贝赋值函数"<rad = other.rad;
        }
        cout<<"拷贝赋值函数"<cir = 2*PI*rad;
        return this->cir;
    }
    double get_area()
    {
        this->area = PI*rad*rad;
        return this->area;
    }
};

class Rect:public Shape
{
private:
    double len;
    double width;
public:
    Rect()//显性定义无参构造
    {
        len = 0;
        width = 0;
       cout<<"无参构造函数"<len = other.len;
            this->width = other.width;
        }
        cout<<"拷贝赋值函数"<cir = 2*(len+width);
        return this->cir;
    }
    double get_area()
    {
        this->area = len*width;
        return this->area;
    }
};

int main()
{
   Circle c1(1.1);
   cout<<"c1周长="<C++day5_第1张图片

 

你可能感兴趣的:(c++,开发语言)