home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CIRCULAR.ZIP / CIRCULAR.CPP
Encoding:
C/C++ Source or Header  |  1999-04-05  |  3.9 KB  |  172 lines

  1. /****************************************************************************
  2. This program was made by Andrew Wong and is strictly freeware.
  3. Feel free to distribute this to your friends.
  4. Please forward any questions or comments to mwong@acay.com.au
  5. Disclaimer: I take no responsibility for the use of this product.
  6. *****************************************************************************/
  7.  
  8.  
  9. #ifndef IOSTREAM_H
  10. #define IOSTREAM_H
  11. #include<iostream.h>
  12. #endif
  13.  
  14. const double pi = 3.14159265359;    //global constant
  15.  
  16. class CircularShape
  17. {
  18.  
  19.     public:
  20.     
  21.     CircularShape() {}
  22.     CircularShape(double radius): itsradius(radius) {}
  23.     virtual ~CircularShape() {}
  24.  
  25.     double getradius() const {return itsradius;}
  26.  
  27.     void setheight(double height) {itsheight = height;}
  28.     void setradius(double radius) {itsradius = radius;}
  29.  
  30.     virtual    double gethemisphere() const {return 0;}
  31.     virtual double getsurfaceArea() const
  32.     {
  33.         return (pi * (itsradius * itsradius));
  34.     }
  35.  
  36.     protected:
  37.  
  38.     double itsradius;
  39.     double itsheight;
  40. };
  41.  
  42.  
  43. class Circle : public CircularShape
  44. {
  45.     public:
  46.  
  47.     double getcircumference() const
  48.     {
  49.         return (2 * pi * itsradius); //formula for circumference
  50.     }
  51. };
  52.  
  53.  
  54. class Sphere : public CircularShape
  55. {
  56.     public:
  57.  
  58.     double getsurfaceArea() const
  59.     {
  60.         return (4 * pi * (itsradius * itsradius)); //formula for SA
  61.     }
  62.  
  63.     double gethemisphere() const
  64.     {
  65.         return (3 * pi * (itsradius * itsradius)); //formula for SA
  66.     }
  67. };
  68.  
  69.  
  70. class Cylinder : public Circle
  71. {
  72.     public:
  73.  
  74.     double getsurfaceArea() const
  75.     {       //formula for SA
  76.         return ((pi * (itsradius * itsradius)) + (getcircumference() * itsheight));
  77.     }
  78.  
  79. };
  80.  
  81.  
  82. class Cone : public Circle
  83. {
  84.     public:
  85.  
  86.     double getsurfaceArea() const
  87.     {       //formula for SA of cone
  88.         return (pi * itsradius * itsheight);
  89.     }
  90.  
  91. };
  92.  
  93.  
  94. int main()
  95. {
  96.     short int choice = 1;
  97.     short int number = 1;
  98.     double radius;
  99.     double height;
  100.     CircularShape *pshape = 0;
  101.  
  102.     cout << "This program was made by Andrew Wong and is strictly freeware\n";
  103.     cout << "Feel free to distribute this to your friends\n";
  104.     cout << "Please forward any questions or comments to mwong@acay.com.au\n";
  105.     cout << "Disclaimer: I take no responsibility for the use of this product\n\n";
  106.     cout << "This program finds out the surface area of all circular shapes\n";
  107.     cout << "Never enter anything besides numbers!!\n";
  108.     cout << "If you do, the program will crash!!!\n";
  109.  
  110.     while(choice != 0)
  111.     {
  112.         cout << "\nWhat circular shape do you want to find the surface area of \n";
  113.         cout << "(0)quit, (1)circle, (2)cone, (3)cylinder, (4)sphere  ";
  114.         cin >> choice;
  115.  
  116.         switch(choice)
  117.         {
  118.             case 1:    pshape = new Circle;    break;
  119.             case 2:    pshape = new Cone;    break;
  120.             case 3:    pshape = new Cylinder;    break;
  121.             case 4:    pshape = new Sphere;       break;
  122.             default: cout << "\ngoodbye\n";
  123.                  pshape = 0;            break;
  124.         }
  125.  
  126.         if((choice < 1) || (choice > 4)) //is choice a valid number?
  127.         {
  128.             break;
  129.         }
  130.  
  131.         cout << "\nPlese enter the radius\n";
  132.         cin >> radius;
  133.         pshape->setradius(radius);
  134.  
  135.         if((choice == 2) || (choice == 3)) //does the shape require a height?
  136.         {
  137.             cout << "\nPlease enter the hieght\n";
  138.             cin >> height;
  139.             pshape->setheight(height);
  140.         }
  141.  
  142.         if(choice == 4)
  143.         {
  144.             cout << "\nDo you want the SA of the (1)whole sphere or a (2)hemisphere?  \n";
  145.             cin >> number;
  146.  
  147.             while((number < 1) || (number >2))
  148.             {
  149.                 cout << "Sorry, that is not a valid choice\n";
  150.                 cout << "Please eneter a new choice ";
  151.                 cin >> number;
  152.             }
  153.  
  154.             if(number == 1)
  155.             {       cout << "\nThe surface are of the sphere is:\t";
  156.                 cout << pshape->getsurfaceArea();
  157.             }
  158.             else
  159.             {          cout << "\nThe surface are of the hemisphere is:\t";
  160.                 cout << pshape->gethemisphere();
  161.             }
  162.         }
  163.         else
  164.         {    cout <<"\nThe surface are of your shape is:\t";
  165.             cout << pshape->getsurfaceArea();
  166.         }
  167.     delete pshape;
  168.     pshape = 0;
  169.     }
  170. return 0;
  171. }
  172.