#include <iostream>
using namespace std;
class Point
{
protected:
double x,y;//分别代表横坐标和纵坐标
public:
Point(double m=0,double n=0);
void setpoint(double d,double e){x=d;y=e;}
friend ostream& operator << (ostream& output,Point&c);
double getx(){return x;}
double gety(){return y;}
};
Point::Point(double m,double n)
{
x=m;
y=n;
}
ostream& operator << (ostream& output,Point&c)
{
output<<"点为:"<<"("<<c.x<<","<<c.y<<")"<<endl;
return output;
}
class Circle:public Point
{
public:
Circle(double c1,double c2,double c3):Point(c1,c2){r=c3;}
friend ostream& operator << (ostream& output,Circle&c);
void setR(double);
double area( );
double getR(){return r;}
protected:
double r;//圆的半径
};
void Circle::setR(double m)
{
r=m;
}
double Circle::area()
{
return 3.1415926*r*r;
}
ostream& operator << (ostream& output,Circle&c)
{
output<<"圆心:"<<"("<<c.x<<","<<c.y<<")"<<"面积为:"<<c.area()<<endl;
return output;
}
class Cylinder:public Circle
{
private:
double h;//圆柱的高
public:
Cylinder(double t1,double t2,double t3,double t4):Circle(t1,t2,t3){h=t4;}
friend ostream& operator << (ostream& output,Cylinder&c);
double area( ) ;
double volume();
void setHeight(double m){h=m;}
double getHeight(){return h;}
};
double Cylinder::area( )
{
return 2*Circle::area( )+2*3.14159*r*h;
}
double Cylinder::volume()
{
return area()*h;
}
ostream& operator << (ostream& output,Cylinder&c)
{
output<<"Center=["<<c.x<<","<<c.y<<"], r="<<c.r<<", h="<<c.h
<<"\narea="<<c.area( )<<", volume="<<c.volume( )<<endl;
return output;
}
int main( )
{
Point t(5,6);
cout<<t;
Circle t1(7,3,1);
cout<<t1;
Cylinder cy1(3.5,2.4,5.2,10);
cout<<"\noriginal cylinder:\nx="<<cy1.getx( )<<", y="<<cy1.gety( )<<", r="
<<cy1.getR( )<<", h="<<cy1.getHeight( )<<"\narea="<<cy1.area()
<<",volume="<<cy1.volume()<<endl;
cy1.setHeight(5);
cy1.setR(7.5);
cy1.setpoint(5,4);
cout<<"\nnew cylinder:\n"<<cy1;
system("pause");
return 0;
}
任务三:点到圆到圆柱体的继承
最新推荐文章于 2024-04-14 14:01:23 发布