以点类Point及平面图形类Plane为基类公有派生圆类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用于显示点的坐标信息,显示格式为:Point(X=<X坐标>,Y=<Y坐标>)
Plane类结构说明:
抽象类Plane类的成员函数包括:
①纯虚函数virtual double length()const用于计算平面图形的周长
②纯虚函数virtual double area()const用于计算平面图形的面积
Circle类结构说明:
公有派生圆类Circle以点类Point、平面图形类Plane为基类,Circle类的结构说明如下:
Circle类的数据成员包括:
①圆心坐标继承自Point类
②保护静态数据常量PI(double型),其值为3.14159
③私有数据成员:半径radius(double型)。
Circle类成员函数包括:
①有参构造函数Circle(double, double, double)和拷贝构造函数Circle(const Circle &),其中有参构造函数参数包括圆心坐标和半径,圆心调用Point类构造函数进行构造,各参数默认值为0,输出信息“Circle Constructor run”,拷贝构造函数输出信息“Circle CopyConstructor run”
②析构函数,析构函数输出信息“Circle Destructor run”
③公有函数成员void setR(double)和double getR()const分别返回和设置radius
④重载void show()const用于显示圆的信息,显示格式为:
Circle(Point(<圆心X坐标>,<圆心Y坐标>),Radius=<半径>)
⑤重载double area()const用于计算圆的面积
⑥重载double length()const用于计算圆的周长
裁判测试程序样例:
#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(); /*析构函数*/ virtual void show()const; /*显示*/ void setX(double xv); /*设置X坐标*/ void setY(double yv); /*设置Y坐标*/ double getX() const; /*获取X坐标*/ double getY() const; /*获取Y坐标*/ }; 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::show() const{/*显示*/ cout<<"Point(X="<<x<<",Y="<<y<<")"; } 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; } class Plane{/*平面图形基类*/ public: virtual double length()const=0;/*周长*/ virtual double area()const=0; /*面积*/ }; /*请在这里填写答案*/ void show(Point *p){/*点基类的显示函数*/ p->show(); } void length(Plane *p){/*平面图形的周长函数*/ cout<<"Length="<<p->length()<<endl; } void area(Plane &p){/*平面图形的面积函数*/ cout<<"Area="<<p.area()<<endl; } //主函数 int main(void){ double x,y,r; Circle c1,c2(c1); show(&c1); cout<<endl; area(c1); length(&c1); cin>>x>>y>>r; c2.setX(x); c2.setY(y); c2.setR(r); show(&c2); cout<<endl; area(c2); length(&c2); }
输入样例:
1.0 2.0 3.0
输出样例:
Point Constructor run Circle Constructor run Point CopyConstructor run Circle CopyConstructor run Circle(Point(X=0,Y=0),Radius=0) Area=0 Length=0 Circle(Point(X=1,Y=2),Radius=3) Area=28.2743 Length=18.8495 Circle Destructor run Point Destructor run Circle Destructor run Point Destructor run
补充代码:
class Circle:public Point,public Plane
{
private:
double radius;
public:
static double PI;
Circle(double x1=0,double x2=0,double r1=0);
Circle(const Circle &P);
~Circle();
void setR(double r2);
double getR() const;
void show() const;
double area() const;
double length() const;
};
double Circle::PI=3.14159;
Circle::Circle(double x1,double x2,double r1):Point(x1,x2)
{
radius=r1;
cout<<"Circle Constructor run"<<endl;
}
Circle::Circle(const Circle &p):Point(p)
{
radius=p.radius;
cout<<"Circle CopyConstructor run"<<endl;
}
Circle::~Circle()
{
cout<<"Circle Destructor run"<<endl;
}
void Circle::setR(double r2)
{
radius=r2;
}
double Circle::getR() const
{
return radius;
}
void Circle::show() const
{
cout<<"Circle(Point(X="<<getX()<<",Y="<<getY()<<"),Radius="<<radius<<")";
}
double Circle::area() const
{
return PI*radius*radius;
}
double Circle::length() const
{
return 2*PI*radius;
}
标签:const,Point,double,void,圆类,Plane,run,Circle
From: https://www.cnblogs.com/zljzy/p/17402925.html