pta_【CPP0028】以圆类Circle及立体图形类Solid为基础设计圆柱类Cylinder
#include <iostream>
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 Plane, public Point
{
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;
}
virtual double length() const
{
return 2 * PI * radius;
}
virtual double area() const
{
return PI * radius * radius;
}
virtual void show() const
{
cout << "Circle("
<< "Point(" << "X=" << getX() << ",Y=" << getY() << "),"
<< "Radius=" << radius
<< ")";
}
};
const double Circle::PI = 3.14159;
class Cylinder : public Solid, public Circle
{
private:
double height;
public:
Cylinder(double x = 0, double y = 0, double radius = 0, double height = 0)
: Circle(x, y, radius), height(height)
{
cout << "Cylinder Constructor run" << endl;
}
Cylinder(const Cylinder& c) : Circle(c), height(c.height)
{
cout << "Cylinder CopyConstructor run" << endl;
}
~Cylinder()
{
cout << "Cylinder Destructor run" << endl;
}
void setH(double height)
{
this->height = height;
}
double getH() const
{
return height;
}
virtual double volume() const
{
return height * Circle::area();
}
virtual double s_Area() const
{
return 2 * Circle::area() + height * Circle::length();
}
virtual void show() const
{
cout << "Cylinder("
<< "Circle("
<< "Point(" << "X=" << getX() << ",Y=" << getY() << "),"
<< "Radius=" << radius << "),"
<< "Height=" << height
<< ")";
}
};
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;
Cylinder cy1(1,2,3,4),cy2(cy1);
show(&cy1);
cout<<endl;
area(cy1);
length(&cy1);
s_Area(cy1);
volumn(&cy1);
cy2.setH(h);
show(&cy2);
cout<<endl;
area(cy2);
length(&cy2);
s_Area(cy2);
volumn(&cy2);
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)