home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / class3.cpp < prev    next >
C/C++ Source or Header  |  1993-03-31  |  1KB  |  66 lines

  1. // Program demonstrates a small hierarchy of classes
  2.  
  3. #include <iostream.h>
  4. #include <math.h>
  5.  
  6. const double pi = 4 * atan(1);
  7.  
  8. inline double sqr(double x)
  9. { return x * x; }
  10.  
  11. class cCircle
  12. {
  13.   protected:
  14.     double radius;
  15.  
  16.   public:
  17.     cCircle(double radiusVal = 0) : radius(radiusVal) {}
  18.     void setRadius(double radiusVal)
  19.       { radius = radiusVal; }
  20.     double getRadius() const
  21.       { return radius; }
  22.     double area() const
  23.       { return pi * sqr(radius); }
  24.     void showData();
  25. };
  26.  
  27. class cCylinder : public cCircle
  28. {
  29.   protected:
  30.     double height;
  31.  
  32.   public:
  33.      cCylinder(double heightVal = 0, double radiusVal = 0)
  34.        : height(heightVal), cCircle(radiusVal) {}
  35.      void setHeight(double heightVal)
  36.        { height = heightVal; }
  37.      double getHeight() const
  38.        { return height; }
  39.      double area() const
  40.        { return 2 * cCircle::area() +
  41.                 2 * pi * radius * height; }
  42.      void showData();
  43. };
  44.  
  45. void cCircle::showData()
  46. {
  47.    cout << "Circle radius        = " << getRadius() << "\n"
  48.         << "Circle area          = " << area() << "\n\n";
  49. }
  50.  
  51. void cCylinder::showData()
  52. {
  53.    cout << "Cylinder radius      = " << getRadius() << "\n"
  54.         << "Cylinder height      = " << getHeight() << "\n"
  55.         << "Cylinder area        = " << area() << "\n\n";
  56. }
  57.  
  58. main()
  59. {
  60.    cCircle Circle(1);
  61.    cCylinder Cylinder(10, 1);
  62.  
  63.    Circle.showData();
  64.    Cylinder.showData();
  65.    return 0;
  66. }