#include <iostream>
#define PI 3.14
using namespace std;
class Shape
{
protected:
double round;
double area;
public:
Shape():round(0),area(0){}
Shape(double r,double a):round(r),area(a){}
Shape(const Shape &shape)
{
this->round = shape.round;
this->area = shape.area;
}
~Shape(){}
Shape& operator=(const Shape &shape)
{
this->round = shape.round;
this->area = shape.area;
return *this;
}
};
class Circle:public Shape
{
private:
double radius;
public:
Circle(double r):radius(r)
{
round = 2*PI*r;
area = r*r*PI;
}
Circle(Circle *c):Shape(c->round,c->area),radius(c->radius){}
~Circle(){}
Circle&operator = (const Circle &c)
{
this->radius = c.radius;
this->round = c.round;
this->area =c.area;
return *this;
}
double get_round()
{
return round;
}
double get_area()
{
return area;
}
};
class Rect:public Shape
{
private:
double length;
double width;
public:
Rect(double l,double w):length(l),width(w)
{
round = (l+w)*2;
area = l*w;
}
Rect(Rect *c):Shape(c->round,c->area),length(c->length),width(c->width){}
~Rect(){}
Rect& operator=(const Rect &c)
{
this->length =c.length;
this->width =c.width;
this->round = c.round;
this->area = c.area;
return *this;
}
double get_round()
{
return round;
}
double get_area()
{
return area;
}
};
int main()
{
Circle c1(6);
cout<<"c1周长="<<c1.get_round()<<endl;
cout<<"c1面积="<<c1.get_area()<<endl;
Circle c2(c1);
cout<<"c2周长="<<c2.get_round()<<endl;
cout<<"c2面积="<<c2.get_area()<<endl;
Rect r1(4,6);
cout<<"r1周长="<<r1.get_round()<<endl;
cout<<"r1面积="<<r1.get_area()<<endl;
Rect r2(r1);
cout<<"r2周长="<<r2.get_round()<<endl;
cout<<"r2面积="<<r2.get_area()<<endl;
return 0;
}
day 0912
最新推荐文章于 2024-09-27 11:26:03 发布
本文介绍了C++中的类Shape,以及其派生类Circle和Rect,展示了如何定义构造函数、运算符重载和成员函数,计算并显示图形的周长和面积。
摘要由CSDN通过智能技术生成