1.#include <iostream.h>
class Point
{
public:
Point(float x=0,float y=0);
void setPoint(float,float);
float getX() const
{return x;}
float getY() const
{return y;}
friend ostream & operator<<(ostream &output,Point &);
protected:
float x,y;
};
Point::Point(float a,float b)
{
x=a;y=b;
}
void Point::setPoint(float a,float b)
{
x=a;y=b;
}
ostream & operator<<(ostream &output,Point &p)
{
output<<"["<<p.x<<","<<p.y<<"]"<<endl;
return output;
}
class Circle:public Point
{
public:
Circle(float x,float y,float r):Point(x,y),radius(r){}
void setRadius(float r)
{radius=r;}
float getRadius() const
{return radius;}
double area() const
{
return 3.1415*radius*radius;
}
friend ostream & operator<<(ostream &output,Circle &);
protected:
float radius;
};
ostream & operator<<(ostream &output,Circle &c)
{
output<<"Center =["<<c.x<<","<<c.y<<"],r= "<<c.radius<<",area="<<c.area()<<endl;
return output;
}
class Cylinder:public Circle
{
public:
Cylinder(float x,float y,float r,float h):Circle(x,y,r),height(h){}
void setHeight(float h)
{height=h;}
float getHeight() const
{return height;}
double area() const
{
return 2*Circle::area()+2*3.1415*radius*height;
}
double volume() const
{
return 3.1415*radius*radius*height;
}
friend ostream & operator<<(ostream &output,Cylinder &);
protected:
float 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;
}
int main()
{
Cylinder cy1(3.5,6.4,5.2,10);
cout<<"original cylinder:\nx="<<cy1.getX()<<",y="<<cy1.getY()<<",r="<<cy1.getRadius()<<",h="<<cy1.getHeight()<<"\narea="<<cy1.area()<<",volume="<<cy1.volume()<<endl;
cy1.setHeight(15);
cy1.setRadius(7.5);
cy1.setPoint(5,5);
cout<<"\nnew cylinder:\n"<<cy1;
Point &pRef=cy1;
cout<<"\npRef as a point:"<<pRef;
Circle &cRef=cy1;
cout<<"\ncRef as a circle:"<<cRef;
return 0;
}