以圆类Circle为基础设计球类Sphere
以点类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
###Circle类结构说明:
公有派生圆类Circle以点类Point为基类,相较于点类Point,Circle类的结构说明如下:
Circle类的数据成员包括:
①私有数据成员:X坐标x继承自Point类,Y坐标y继承自Point类。
②私有数据成员:半径radius(double型)
③静态数据常量PI(double型),其值为3.14159
Circle类成员函数包括:
①定义有参构造函数Circle(double, double, double)和拷贝构造函数Circle(Circle &),其中有参构造函数参数包括圆心坐标和半径,圆心调用Point类构造函数进行构造,各参数默认值为0,输出信息“Circle Constructor run”,拷贝构造函数输出信息“Circle CopyConstructor run”
②定义析构函数,析构函数输出信息“Circle Destructor run”
③定义公有函数成员:void setRadius(double)和double getRadius() const分别返回和设置radius
④定义公有成员函数double circumference() const用于计算圆的周长
⑤重载double area() const用于计算圆的面积
⑥重载void show() const用于显示圆的信息,显示格式为:
(圆心X坐标,圆心Y坐标),Radius=半径
Sphere类结构说明:
公有派生球类Sphere以圆类Circle为基类,Sphere类的结构说明如下:
Sphere类的数据成员包括:
①私有数据成员:X坐标x继承自Circle类,Y坐标y继承自Circle类,半径radius继承自Circle类。
②静态数据常量PI继承自Circle类。
Sphere类成员函数包括:
①定义有参构造函数Sphere(double, double, double)和拷贝构造函数Sphere(Sphere &),其中有参构造函数参数包括圆心坐标和半径,圆心调用Point类构造函数进行构造,各参数默认值为0,输出信息“Sphere Constructor run”,拷贝构造函数输出信息“Sphere CopyConstructor run”
②定义析构函数,析构函数输出信息“Sphere Destructor run”
③定义公有成员函数double volume() const用于计算球的体积
④重载double area() const用于计算球的表面积
裁判测试程序样例:
#include <iostream>
using namespace std;
class Point{
protected:
double x;
double y;
public:
Point(double xv=0,double yv=0);//有参构造
Point(const Point &p); //拷贝构造
~Point(); //析构函数
void show() const; //显示Point信息
double area()const; //面积=0
void setX(double xv); //设置X坐标
void setY(double yv); //设置Y坐标
double getX() const; //获取X坐标
double getY() const; //获取Y坐标
};
//有参构造
Point::Point(double xv,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;
}
//显示Point
void Point::show() const{
cout<<"("<<x<<","<<y<<")";
}
//设置X坐标
void Point::setX(double xv){
x=xv;
}
//设置Y坐标
void Point::setY(double yv){
y=yv;
}
//面积函数
double Point::area() const{
return 0;
}
//获取X坐标
double Point::getX() const{
return x;
}
//获取Y坐标
double Point::getY() const{
return y;
}
/*请在这里填写答案*/
//主函数
int main(void){
double r;
cin>>r;
Sphere s1(1,2,4),s2(s1);
s1.show();
cout<<endl<<"Area="<<s1.area()<<endl<<"Volume="<<s1.volume()<<endl;
s2.setRadius(r);
s2.Circle::show();
cout<<endl<<"Area="<<s2.Circle::area()<<endl<<"Circumference="<<s2.Circle::circumference()<<endl;
return 0;
}
输入样例:
6.0
输出样例:
Point Constructor run Circle Constructor run Sphere Constructor run Point CopyConstructor run Circle CopyConstructor run Sphere CopyConstructor run (1,2),Radius=4 Area=201.062 Volume=268.082 (1,2),Radius=6 Area=113.097 Circumference=37.6991 Sphere Destructor run Circle Destructor run Point Destructor run Sphere Destructor run Circle Destructor run Point Destructor run
class Circle:public Point
{
protected:
double radius;
public:
static double PI;
Circle(double a=0,double b=0,double c=0);
Circle(Circle &a);
~Circle();
void setRadius(double a);
double getRadius()const;
double circumference()const;
double area()const;
void show()const;
};
double Circle:: PI=3.14159;
Circle::Circle(double a,double b,double c):Point(a,b)
{
radius=c;
cout<<"Circle Constructor run"<<endl;
}
Circle::Circle(Circle &a):Point(a)
{
radius=a.radius;
cout<<"Circle CopyConstructor run"<<endl;
}
Circle::~Circle()
{
cout<<"Circle Destructor run"<<endl;
}
void Circle::setRadius(double a)
{
radius=a;
}
double Circle::getRadius()const
{
return radius;
}
double Circle::circumference()const
{
return 2*PI*radius;
}
double Circle::area()const
{
return PI*radius*radius;
}
void Circle::show()const
{
cout<<"("<<x<<","<<y<<")"<<",Radius="<<radius;
}
class Sphere:public Circle
{
public:
Sphere(double a=0,double b=0,double c=0):Circle(a,b,c)
{
cout<<"Sphere Constructor run"<<endl;
}
Sphere(Sphere &p):Circle(p)
{
cout<<"Sphere CopyConstructor run"<<endl;
}
~Sphere()
{
cout<<"Sphere Destructor run"<<endl;
}
double volume()const
{
return (4*PI*radius*radius*radius)/3;
}
double area()const
{
return 4*PI*radius*radius;
}
};