首页 > 其他分享 >pta_【CPP0027】以圆类Circle及立体图形类Solid为基础设计球类Sphere

pta_【CPP0027】以圆类Circle及立体图形类Solid为基础设计球类Sphere

时间:2023-05-08 19:13:38浏览次数:40  
标签:以圆类 const cout Point Solid double void Sphere radius

#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 Point, public Plane
{
public:
Circle(double x = 0, double y = 0, double r = 0) : Point(x, y), radius(r)
{
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 r)
{
radius = r;
}
double getR() const
{
return radius;
}
void show() const
{
cout << "Circle(Point(X=" << getX() << ",Y=" << getY() << "),Radius=" << radius << ")";
}
double area() const
{
return PI * radius * radius;
}
double length() const
{
return 2 * PI * radius;
}
protected:
static const double PI;
double radius;
};
class Sphere : public Circle, public Solid
{
public:
Sphere(double x = 0, double y = 0, double r = 0) : Circle(x, y, r)
{
cout << "Sphere Constructor run" << endl;
}
Sphere(const Sphere &s) : Circle(s)
{
cout << "Sphere CopyConstructor run" << endl;
}
~Sphere()
{
cout << "Sphere Destructor run" << endl;
}
void show() const
{
cout << "Sphere(";
Circle::show();
cout << ")";
}
double s_Area() const
{
return 4 * PI * radius * radius;
}
double volume() const
{
return 4.0 / 3.0 * PI * radius * radius * radius;
}
};
const double Circle::PI = 3.14159;
void show(const Sphere *s)
{
s->show();
}
void area(const Circle &c)
{
cout << "Area=" << c.area() << endl;
}
void length(const Circle *c)
{
cout << "Length=" << c->length() << endl;
}
void s_Area(const Sphere &s)
{
cout << "S_Area=" << s.s_Area() << endl;
}
void volumn(const Sphere *s)
{
cout << "Volumn=" << s->volume() << endl;
}

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 r;
cin>>r;
Sphere s1(1,2,3),s2(s1);
show(&s1);
cout<<endl;
area(s1);
length(&s1);
s_Area(s1);
volumn(&s1);
s2.setR(r);
show(&s2);
cout<<endl;
area(s2);
length(&s2);
s_Area(s2);
volumn(&s2);
return 0;
}

 

 

标签:以圆类,const,cout,Point,Solid,double,void,Sphere,radius
From: https://www.cnblogs.com/atrue/p/17382846.html

相关文章

  • OpenEuler 部署 kubesphere 遇到问题及处理
    安装时报如下错误18:46:27CSTretry:[node1]18:46:33CST[ERRO]checkremotefileexisterr:strconv.Atoi:parsing"Welcometo5.10.0-136.12.0.86.oe2203sp1.x86_64\r\n\r\nSysteminformationasoftime:\tFriMay506:46:33PMCST2023\r\n\r\nSystem......
  • 13.solidworks简单渲染教程
    1、打开solidworks,点击菜单栏的工具,然后选择插件,勾选PhotoView360两边的框2、右键特征工具栏,把渲染工具勾选3点击渲染工具,然后对想要更改的零件或者部位使用编辑外观和编辑布景更改外观颜色、材质、贴图以及背景 4、在绘图区域先摆好一个适合的角度,然后再添加相机......
  • 终于有人把openGauss3.0.0分布式原理讲透了,openGauss X ShardingSphere分布式原理和部
    本文为原理精讲,部署文章链接如下https://www.cnblogs.com/opengauss/p/17364285.html一、opengauss的背景和行业现状2022年,七大openGauss商业版发布,是基于openGauss3.0推出商业发行版目前海量数据库Vastbase表现最佳,一直是TOP1作者认为之所以海量数据库Vastbase......
  • 终于有人把openGauss3.0.0分布式原理讲透了,openGauss X ShardingSphere分布式原理和部
    本文为原理精讲,部署文章链接如下https://blog.51cto.com/u_13808894/6236819一、opengauss的背景和行业现状2022年,七大openGauss商业版发布,是基于openGauss3.0推出商业发行版目前海量数据库Vastbase表现最佳,一直是TOP1作者认为之所以海量数据库Vastbase目前无法被同......
  • Kubernetes(K8S) kubesphere 介绍
    使用Kubeadm部署Kubernetes(K8S)安装--附K8S架构图Kubernetes(K8S)kubesphere安装官网地址:https://kubesphere.com.cn/KubeSphere是个全栈的Kubernetes容器云PaaS解决方案KubeSphere是在目前主流容器调度平台Kubernetes之上构建的企业级分布式多租户容器平台,提供简单易......
  • KubeSphere 社区双周报 | 杭州站 Meetup 议题征集中 | 2023.04.14-04.27
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2023.04.14-2023.04.27。贡献者名单新晋KubeSphereCon......
  • 11.solidworks零件另存后与工程图关联,工程图与零件关联问题
    一.工程图与零件关联1.不要打开零件情况下,点击打开 2.点击对应的工程图不要打开,点击下面的参考3.点击左边的零件去找到新的对应零件即可 4.完成 二、零件与工程图关联 工程图与零件的名称完全一直就会被关联......
  • Kubernetes(K8S) kubesphere 安装
    安装KubeSphere最好的方法就是参考官方文档,而且官方文档是中文的。官网地址:https://kubesphere.com.cn/https://github.com/kubesphere/kubesphere/blob/master/README_zh.mdKubernetes(K8S)kubesphere介绍安装提前条件:使用Kubeadm部署Kubernetes(K8S)安装--附K8S架构......
  • MySQL主从复制搭配SharingSphere踩大坑记录
    报错内容:Causedby:java.util.NoSuchElementException:Novaluebound点击查看原始错误代码ErrorstartingApplicationContext.Todisplaytheconditionsreportre-runyourapplicationwith'debug'enabled.2023-04-2616:34:48.270ERROR22584---[......
  • websphere6.1开发、部署、远程调用EJB2.0
    开发工具是IBMRAD7.5.4。WAS版本6.1。EJB版本2.0。开发过程如图:新建完后,工程结构如下:再新建SessionBean下一步完成。如图:修改几个文件:packagecom.ejb;importstaticjava.lang.System.out;/***BeanimplementationclassforEnterpriseBean:HelloSession*/publicc......