#include <iostream>
#include<cmath>
using namespace std;
//点类Point
class Point{
private:
double x;
double y;
public:
Point(double xv=0,double yv=0);/*构造函数*/
Point(const Point &p); /*拷贝构造*/
~Point(); /*析构函数*/
void setX(double xv); /*设置X坐标*/
void setY(double yv); /*设置Y坐标*/
double getX()const; /*获取X坐标*/
double getY()const; /*获取Y坐标*/
virtual void show()const; /*显示*/
};
Point::Point(const double xv,const double yv){/*构造函数*/
x=xv;
y=yv;
cout<<"Point Constructor run"<<endl;
}
Point::Point(const Point &p){/*拷贝构造*/
x=p.x;
y=p.y;
cout<<"Point CopyConstructor run"<<endl;
}
Point::~Point(){/*析构函数*/
cout<<"Point Destructor run"<<endl;
}
void Point::setX(double xv){/*设置X坐标*/
x=xv;
}
void Point::setY(double yv){/*设置Y坐标*/
y=yv;
}
double Point::getX()const{/*获取X坐标*/
return x;
}
double Point::getY()const{/*获取Y坐标*/
return y;
}
void Point::show()const{/*显示*/
cout<<"Point(X="<<x<<",Y="<<y<<")";
}
//平面图形类Plane
class Plane{
public:
virtual double length()const=0;/*周长*/
virtual double area()const=0; /*面积*/
};
//立体图形类Solid
class Solid{
public:
virtual double volume()const=0;/*体积*/
virtual double s_Area()const=0;/*表面积*/
};
class Circle: public Point, public Plane
{
protected:
static const double PI;
double radius;
public:
Circle(double x = 0, double y = 0, double radius = 0)
:Point(x, y), radius(radius)
{
cout << "Circle Constructor run" << endl;
}
Circle(const Circle& c) :Point(c), radius(c.radius)
{
cout << "Circle CopyConstructor run" << endl;
}
~Circle()
{
cout << "Circle Destructor run" << endl;
}
void setR(double radius)
{
this->radius = radius;
}
double getR() const
{
return radius;
}
void show() const
{
cout << "Circle(";
Point::show();
cout << ",Radius=" << radius << ")";
}
double area() const
{
return PI * radius * radius;
}
double length() const
{
return 2 * PI * radius;
}
};
const double Circle::PI = 3.14159;
class Cone: public Circle, public Solid
{
private:
double height;
public:
Cone(double x = 0, double y = 0, double radius = 0, double height = 0)
:Circle(x, y, radius), height(height)
{
cout << "Cone Constructor run" << endl;
}
Cone(const Cone& c) :Circle(c), height(c.height)
{
cout << "Cone CopyConstructor run" << endl;
}
~Cone()
{
cout << "Cone Destructor run" << endl;
}
void setH(double height)
{
this->height = height;
}
void show() const
{
cout << "Cone(";
Circle::show();
cout << ",Height=" << height << ")";
}
double s_Area() const
{
return PI * radius * (radius + sqrt(radius * radius + height * height));
}
double volume() const
{
return PI * radius * radius * height / 3;
}
};
void show(Point *p){/*点基类的显示函数*/
p->show();
}
void length(Plane *p){/*平面图形的周长函数*/
cout<<"Length="<<p->length()<<endl;
}
void area(Plane &p){/*平面图形的面积函数*/
cout<<"Area="<<p.area()<<endl;
}
void volumn(Solid *s){/*立体图形的体积函数*/
cout<<"Volumn="<<s->volume()<<endl;
}
void s_Area(Solid &s){/*立体图形的表面积函数*/
cout<<"S_Area="<<s.s_Area()<<endl;
}
//主函数
int main(void){
double h;
cin>>h;
Cone co1(1,2,3,4),co2(co1);
show(&co1);
cout<<endl;
area(co1);
length(&co1);
s_Area(co1);
volumn(&co1);
co2.setH(h);
show(&co2);
cout<<endl;
area(co2);
length(&co2);
s_Area(co2);
volumn(&co2);
return 0;
}
标签:以圆类,const,cout,Point,Solid,double,void,pta,radius From: https://www.cnblogs.com/atrue/p/17396450.html