65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream.h>
//#define pi 3.1415926
double const pi = 3.1415926;
class Shape
{
public:
Shape () {}
virtual double area () const
{
return (0.0);
}
virtual double volum () const
{
return (0.0);
}
virtual void shape_name () const = 0;
};
//define a Point class
class Point:public Shape
{
public:
Point (double x=0, double y=0):point_x(x), point_y (y) {}
virtual void shap_name () const;
friend ostream & operator << (ostream & output, const Point & p);
virtual void set_point ();
virtual void get_point () const;
protected:
double point_x;
double point_y;
};
void Point::shap_name () const
{
cout << "Point:" << endl;
}
void Point::set_point ()
{
cout << "Please set the points:";
cin >> point_x >> point_y;
}
void Point::get_point () const
{
cout << "(" << point_x << ',' << point_y << ")" << endl;
}
ostream & operator << (ostream & output, const Point & p)
{
output << "(" << p.point_x << ',' << p.point_y << ")" << endl;
return output;
}
//define a Circle class
class Circle:public Point
{
public:
Circle (double x, double y, double r):Point (x, y), radius (r) {}
void set_radius ();
double get_radius () const;
virtual double area () const;
virtual void shap_name () const;
friend ostream & operator << (ostream &output, const Circle &c);
protected: //保护成员在派生类中可以被引用,而私有成员则不能!
double radius;
};
void Circle::shap_name () const
{
cout << "Circle:" << endl;
}
void Circle::set_radius ()
{
cout << "Please set the radius of the circle:" << endl;
cin >> radius;
}
double Circle::get_radius () const
{
return radius;
}
double Circle::area () const
{
return (pi*radius*radius);
}
ostream & operator << (ostream &output, const Circle &c)
{
output << '(' << c.point_x << ',' << c.point_y << ')' << "radius = " << c.radius << endl;
return output;
}
//Define a Cylinder class
class Cylinder:public Circle
{
public:
Cylinder (double x, double y, double r, double h):Circle (x, y, r), height (h) {}
void set_height ();
void get_height () const;
virtual double area () const;
virtual double volum () const;
virtual void shap_name () const;
friend ostream & operator << (ostream &output, Cylinder &cy);
private:
double height;
};
void Cylinder::shap_name () const
{
cout << "cylinder:" << endl;
}
void Cylinder::set_height ()
{
cout << "Please set the height of the cylinder:" << endl;
cin >> height;
}
void Cylinder::get_height () const
{
cout << height << endl;
}
double Cylinder::area () const
{
return (2*Circle::area () + 2*pi* Circle::get_radius () *height);
}
double Cylinder::volum () const
{
return (Circle::area () * height);
}
ostream & operator << (ostream &output, Cylinder &cy)
{
output << '(' << cy.point_x << ',' << cy.point_y << ')' << "radius = " << cy.radius
<< "volum = ";
cy.volum ();
return output;
}
void main ()
{
Point point (3.2, 4.5);
Circle circle (2.4, 1.2, 5.6);
Cylinder cylinder (3.5, 6.4, 5.2, 10.5);
point.shap_name ();
cout << point;
circle.shap_name ();
cout << circle;
cylinder.shap_name ();
cout << cylinder;
Shape *pt;
pt = &point;
point.get_point () ;
// pt->get_point (); //用指针调用函数为何不行?!Point是Shape的派生类,指针虽然指向的是Shape类,但是get_point ()声明的是虚函数,应该能用指针调用啊?!!
cout << "The area of the point is:" << endl;
pt->area ();
pt = &circle;
pt->shape_name ();
// cout << '(' << pt->point_x << ',' << pt->point_y << ')' << endl;
circle.get_point ();
cout << "radius = " << circle.get_radius ();
cout << "The area of the circle is:" << endl;
cout << circle.area () << endl;
// cout << pt->area () << endl;
pt = &cylinder;
pt->shape_name ();
cylinder.get_point ();
// cout << '(' << cylinder.point_x << ',' << cylinder.point_y << ')' << endl;
cylinder.get_radius ();
// cout << "radius = " << pt->get_radius () << endl;
cout << "height = ";
cylinder.get_height ();
// pt->get_height ();
cout << "The area of the cylinder is:" << endl;
// cylinder.volum ();
cout << pt ->volum ();
}