以点类Point为基类公有派生圆类Circle,并以圆类Circle为基类公有派生球类Sphere,main(void)函数完成对其的测试。
Point类结构说明:
Point类的数据成员包括: ①私有数据成员:X坐标x(double型),Y坐标y(double型)。 Point类成员函数包括: ①有参构造函数Point(double, double)和拷贝构造函数Point(const Point &),其中有参构造函数参数默认值为0,输出信息“Point Constructor run”,拷贝构造函数输出信息“Point CopyConstructor run” ②析构函数,析构函数输出信息“Point Destructor run” ③公有函数成员:void setX(double)和double getX() const分别返回和设置X坐标 ④公有函数成员:void setY(double)和double getY() const分别返回和设置Y坐标 ⑤公有成员函数void show() const用于显示点的坐标信息,显示格式为:(x,y) ⑥公有成员函数double area() const用于计算点的面积,点的面积为0
class Circle : public Point { protected: double radius = 0; static double PI; public: Circle (double a = 0, double b = 0, double c = 0) { x = a; y = b; radius = c; cout << "Circle Constructor run" << endl; } Circle (Circle &r) : Point(r) { x = r.x; y = r.y; radius = r.radius; cout << "Circle CopyConstructor run" << endl; } ~Circle() { cout << "Circle Destructor run" << endl; } void setRadius(double a) { radius = a; } double getRadius() const { return radius; } double circumference() const { return PI * 2 * radius; } double area() const { return radius * radius * PI; } void show() const { cout << "("<< x << "," << y << "),"<< "Radius="<< radius; } }; double Circle::PI = 3.14159; class Sphere : public Circle { public: Sphere(double a = 0, double b = 0, double c = 0) { x = a; y = b; radius = c; cout << "Sphere Constructor run" << endl; } Sphere(Sphere &r) : Circle(r) { x = r.x; y = r.y; radius = r.radius; cout << "Sphere CopyConstructor run" << endl; } ~Sphere() { cout << "Sphere Destructor run" << endl; } double volume() const { return 4.0/3*PI*radius*radius*radius; } double area() const { return 4.0*PI*radius*radius; } };
标签:const,函数,Point,double,第十八,公有,Circle From: https://www.cnblogs.com/gjsgjs/p/17413060.html