Advertisement
TheWhiteFang

MTQ2 yeo

Dec 18th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. //yeo punya
  2. //MTQ2
  3. #include <iostream>
  4. #include <string>
  5. #define PI 3.141592654
  6. using namespace std;
  7.  
  8. class Polygon{
  9.     string type;
  10.     double sphere, cylinder;
  11. public:
  12.     static double m_TotalAreaSphere, m_TotalAreaCyld;
  13.     Polygon(string t, double r, double h){
  14.         type = t;
  15.         if (t == "S"){
  16.             sphere = 4 * 3.14159 * r * r;
  17.         }
  18.         else if (t == "C"){
  19.             cylinder = (2 * PI * r * r) + (2 * PI * r * h);
  20.         }
  21.         else{
  22.             sphere = 0;
  23.             cylinder = 0;
  24.         }
  25.     }
  26.     void Print(){
  27.         cout << "Area of ";
  28.         if (type == "S"){
  29.             m_TotalAreaSphere += sphere;
  30.             cout << "Sphere (cm2): " << sphere << endl << endl;
  31.         }
  32.         else{
  33.             m_TotalAreaCyld += cylinder;
  34.             cout << "Cylinder (cm2) " << cylinder << endl << endl;
  35.         }
  36.     }
  37. };
  38.     double Polygon::m_TotalAreaSphere = 0.0;
  39.     double Polygon::m_TotalAreaCyld = 0.0;
  40.  
  41. int main()
  42. {
  43.     string type;
  44.     double radius, height;
  45.     for(int i = 0; i < 4; i++){
  46.         cout << "Specify polygon type (S for sphere or C for cylinder): ";
  47.         cin >> type;
  48.         cout << "Specify radius and height: ";
  49.         cin >> radius >> height;
  50.         Polygon polyobj(type, radius, height);
  51.         polyobj.Print();
  52.     }
  53.     cout << "Total area for Sphere (cm2): " << Polygon::m_TotalAreaSphere << endl;
  54.     cout << "Total area for Cylinder (cm2): " << Polygon::m_TotalAreaCyld << endl << endl;
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement