#include < iostream >
using namespace std;
class Point
{
protected:
double x;
double y;
public:
Point ( double m, double n );
void Show1( );
~Point( ); //需要释放建立对象时动态分配的内存
};
class Circle : public Point
{protected:
double r;
public:
Circle ( double m,double n,double c);
void Show2( );
~Circle( );
};
class Cylinder : public Circle
{protected:
double h;
public:
Cylinder ( double m,double n,double c,double d);
void Show3( );
double get_s();
double get_v();
~Cylinder( );
friend ostream & operator << (ostream&,Cylinder &c);
};
Point::Point ( double m, double n )
{
x=m;
y=n;
}
void Point::Show1( )
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
Point::~Point( )
{
cout << "The destructor of part was called " << endl;
}
Circle::Circle ( double m,double n,double c):Point ( m, n )
{
r=c;
}
void Circle::Show2( )
{
cout<<"("<<x<<","<<y<<")"<<"半径:"<<r<<endl;
}
Circle::~Circle( )
{
cout << "The destructor of part was called " << endl;
}
Cylinder::Cylinder ( double m,double n,double c,double d):Circle ( m, n, c)
{
h=d;
}
void Cylinder::Show3( )
{
cout<<"("<<x<<","<<y<<")"<<"半径:"<<r<<"高:"<<h<<endl;
}
double Cylinder::get_s()
{
double s=0;
double i=3.14;
s=r*r*i;
cout << "面积为:" << s << endl;
return s;
}
double Cylinder::get_v()
{
double v=0;
double j=3.14;
v=r*r*j*h;
cout << "体积为:" << v <<endl;
return v;
}
Cylinder::~Cylinder( )
{
cout << "The destructor of part was called " << endl;
}
ostream & operator << (ostream&output,Cylinder &c)
{
output<<"("<<c.x<<","<<c.y<<")"<<endl;
output<<"半径"<<c.r<<endl;
output<<"高"<<c.h<<endl;
return output;
}
int main( )
{
double a,b,c,d;
cout << "请分别输入坐标对应的x,y及其圆柱的半径和高"<<endl;
cin >> a >> b >> c >> d ;
Cylinder cylinder( a,b,c,d );
cylinder.Show3( );
cylinder.get_s();
cylinder.get_v();
cout<<cylinder;
system ( "pause" );
return 0;
}
运行结果:
请分别输入坐标对应的x,y及其圆柱的半径和高
0 0 1 1
(0,0)半径:1高:1
面积为:3.14
体积为:3.14
(0,0)
半径1
高1
请按任意键继续. . .