问题描述:
以点类Point为基类公有派生圆类Circle,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类。
②静态数据常量PI(double型),其值为3.14159
③私有数据成员:半径radius(double型)。
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=半径
裁判测试程序样例:
#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;
Circle c1(1,2,4),c2,c3(c1);
c1.show();
cout<<endl<<"Area="<<c1.area()<<endl<<"Circumference="<<c1.circumference()<<endl;
c2.show();
cout<<endl<<"Area="<<c2.area()<<endl<<"Circumference="<<c2.circumference()<<endl;
c3.setRadius(r);
c3.show();
cout<<endl<<"Area="<<c3.area()<<endl<<"Circumference="<<c3.circumference()<<endl;
return 0;
}
输入样例:
6.0
输出样例:
Point Constructor run
Circle Constructor run
Point Constructor run
Circle Constructor run
Point CopyConstructor run
Circle CopyConstructor run
(1,2),Radius=4
Area=50.2654
Circumference=25.1327
(0,0),Radius=0
Area=0
Circumference=0
(1,2),Radius=6
Area=113.097
Circumference=37.6991
Circle Destructor run
Point Destructor run
Circle Destructor run
Point Destructor run
Circle Destructor run
Point Destructor run
代码:
class Circle :protected Point{
private:
double radius;
public:
static double PI;
Circle(double a=0, double b=0, double c=0): Point(a,b),radius(c)
{
cout<<"Circle Constructor run"<<endl;
}
Circle(Circle &p):Point(p)
{
x=p.x;
y=p.y;
radius=p.radius;
cout<<"Circle CopyConstructor run"<<endl;
}
~Circle(){
cout<<"Circle Destructor run"<<endl;
}
void setRadius(double a)
{
radius=a;
}
double getRadius() const
{
return radius;
}
double circumference() const
{
return PI*radius*2;
}
double area() const
{
return PI*radius*radius;
}
void show() const
{
cout<<"("<<x<<","<<y<<")"<<","<<"Radius="<<radius;
}
};
double Circle::PI=3.14159;
标签:const,第一个,Point,double,void,问题,第十四天,run,Circle From: https://www.cnblogs.com/czfznb/p/17372123.html