home *** CD-ROM | disk | FTP | other *** search
- // Program demonstrates a small hierarchy of classes
-
- #include <iostream.h>
- #include <math.h>
-
- const double pi = 4 * atan(1);
-
- inline double sqr(double x)
- { return x * x; }
-
- class cCircle
- {
- protected:
- double radius;
-
- public:
- cCircle(double radiusVal = 0) : radius(radiusVal) {}
- void setRadius(double radiusVal)
- { radius = radiusVal; }
- double getRadius() const
- { return radius; }
- double area() const
- { return pi * sqr(radius); }
- void showData();
- };
-
- class cCylinder : public cCircle
- {
- protected:
- double height;
-
- public:
- cCylinder(double heightVal = 0, double radiusVal = 0)
- : height(heightVal), cCircle(radiusVal) {}
- void setHeight(double heightVal)
- { height = heightVal; }
- double getHeight() const
- { return height; }
- double area() const
- { return 2 * cCircle::area() +
- 2 * pi * radius * height; }
- void showData();
- };
-
- void cCircle::showData()
- {
- cout << "Circle radius = " << getRadius() << "\n"
- << "Circle area = " << area() << "\n\n";
- }
-
- void cCylinder::showData()
- {
- cout << "Cylinder radius = " << getRadius() << "\n"
- << "Cylinder height = " << getHeight() << "\n"
- << "Cylinder area = " << area() << "\n\n";
- }
-
- main()
- {
- cCircle Circle(1);
- cCylinder Cylinder(10, 1);
-
- Circle.showData();
- Cylinder.showData();
- return 0;
- }