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

pta_【CPP0029】以圆类Circle及立体图形类Solid为基础设计圆锥类Cone

时间:2023-05-12 22:33:15浏览次数:44  
标签:以圆类 const cout Point Solid double void pta radius

#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

相关文章

  • Aptana3 SVN Client安装
    Aptana3SVNClient安装安装SVN Client1.开启Aptana,打开help菜单,点击“InstallNewSoftware”2.点击Add按钮,在AddSite窗口,name输入:SVN ,location输入:http://subclipse.tigris.org/update_1.6.x,点击ok按钮3.全选除了“SubclipseIntegrationforMylyn3.x(Optional)3.0.0“ ......
  • Hardhat 开发框架 - Solidity开发教程连载
    Decert.me要连载教程了,《Solidity开发教程》力求系统、深入的介绍Solidity开发,同时这是一套交互式教程,你可以实时的修改教程里的合约代码并运行。本教程来自贡献者@Tiny熊,让我们正式开始学习吧。如果你已经是Hardhat的使用者,可以直接跳到文末,参与挑战领取技能认证NF......
  • 2023 PTA天梯赛补题(L1 & L2)
    2023天梯赛L1&L2补题L1L1-089最好的文档输入输出题#include<bits/stdc++.h>usingnamespacestd;intmain(){cout<<"Goodcodeisitsownbestdocumentation.";return0;}L1-090什么是机器学习输入输出题#include<bits/stdc++.h>us......
  • iptables之forward转发
    1、网络防火墙2、iptables之FORWARD转发实例3、iptables之FORWARD过滤实例1、网络防火墙网络防火墙处于网络入口的边缘,针对网络入口进行防护,针对整个网络入口后面的局域网。作用:当外部网络主机与内部网络主机互相进行通讯时,都要经过iptables所在的主机,由iptables所在的主机进行“......
  • 以圆类Circle及立体图形类Solid为基础设计圆锥类Cone
    以点类Point及平面图形类Plane为基类公有派生圆类Circle,再以圆类Circle及立体图形类Solid为基类公有派生圆锥类Cone,main(void)函数完成对圆锥类Cone的测试。Point类结构说明: Point类的数据成员包括:①私有数据成员:X坐标x(double型),Y坐标y(double型)。Point类成员函数包括:①......
  • pta_【CPP0027】以圆类Circle及立体图形类Solid为基础设计球类Sphere
    #include<iostream>usingnamespacestd;//点类PointclassPoint{private:doublex;doubley;public:Point(doublexv=0,doubleyv=0);/*构造函数*/Point(constPoint&p);/*拷贝构造*/~Point();/*析构函数*/voidsetX(d......
  • PTA练习题
    1#include<iostream>2usingnamespacestd;3classTime4{5private:6inthh;7intmm;8intss;9public:10Time()11{12hh=0;13mm=0;14......
  • PTA练习题
    复数类Complex有两个数据成员:a和b,分别代表复数的实部和虚部,并有若干构造函数和一个重载-(减号,用于计算两个复数的距离)的成员函数。要求设计一个函数模板template<classT>doubledist(Ta,Tb)对int,float,Complex或者其他类型的数据,返回两个数据的间距。以上类名和函数模......
  • pta_【CPP0026】以点类Point及平面图形类Plane为基础设计三角形类Triangle
    #include<iostream>#include<cmath>usingnamespacestd;//点类PointclassPoint{private:doublex;doubley;public:Point(doublexv=0,doubleyv=0);/*构造函数*/Point(constPoint&p);/*拷贝构造*/~Point();/*......
  • 13.solidworks简单渲染教程
    1、打开solidworks,点击菜单栏的工具,然后选择插件,勾选PhotoView360两边的框2、右键特征工具栏,把渲染工具勾选3点击渲染工具,然后对想要更改的零件或者部位使用编辑外观和编辑布景更改外观颜色、材质、贴图以及背景 4、在绘图区域先摆好一个适合的角度,然后再添加相机......