#include<iostream>
using namespace std;
class Point //定义坐标点类
{
public:
Point():x(0),y(0) {};
Point(double x0, double y0);
void setPoint(double a,double b);
double get_x0();
double get_y0();
friend ostream & operator<<(ostream &,const Point &);
protected:
double x,y; //点的横坐标和纵坐标
};
Point::Point(double x0, double y0)
{
x=x0;
y=y0;
}
void Point::setPoint(double a,double b)
{
x=a;
y=b;
}
ostream & operator<<(ostream &output,const Point &p)
{
output<<"("<<p.x<<","<<p.y<<")"<<endl;
return output;
}
double Point::get_x0()
{
return x;
}
double Point::get_y0()
{
return y;
}
class Circle: public Point //利用坐标点类定义直线类, 其基类的数据成员表示直线的中点
{
public:
Circle(double x=0,double y=0,double r=0);
void setRadius(double);
double area();
double getr();
friend ostream &operator<<(ostream &,Circle &);
protected:
double radius;
};
Circle::Circle(double x,double y,double r)
:Point(x,y),radius(r) {}
void Circle::setRadius(double r)
{
radius=r;
}
double Circle::getr()
{
return radius;
}
double Circle::area()
{
return 3.14*radius*radius;
}
ostream &operator<<(ostream &output,Circle &c)
{
output<<"圆的圆心是"<<'('<<c.x<<','<<c.y<<"),r="<<c.radius<<" area="<<c.area();
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(double x=0,double y=0,double r=0,double h=0);
double area();
double volume();
double geth();
void setHeight(double);
friend ostream &operator<<(ostream &,Cylinder &);
private:
double height;
};
Cylinder::Cylinder(double x,double y,double r,double h)
:Circle(x,y,r),height(h) {}
void Cylinder::setHeight(double h)
{
height=h;
}
double Cylinder::area()
{
return 2*3.14*radius+height+Circle::area();
}
double Cylinder::volume()
{
return Circle::area()*height;
}
ostream &operator<<(ostream &output,Cylinder &cy)
{
output<<"Center=("<<cy.x<<","<<cy.y<<"), r="<<cy.radius<<", h="<<cy.height
<<"\narea="<<cy.area( )<<", volume="<<cy.volume( )<<endl;
return output;
}
double Cylinder::geth()
{
return height;
}
int main()
{
Cylinder cy1(1,1,5.2,10);
cout<<"\ncylinder:\nx="<<cy1.get_x0( )<<", y="<<cy1.get_y0( )<<", r="
<<cy1.getr( )<<", h="<<cy1.geth( )<<"\narea="<<cy1.area()
<<",volume="<<cy1.volume()<<endl;
}