Advertisement
Guest User

C++ Lab

a guest
Dec 7th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.05 KB | None | 0 0
  1. /*
  2. ##This is a program for calculating "Area" of Circle, Square, Rectangle and Triangle.
  3. ##This program uses:
  4.     #Condition
  5.     #Loop
  6.     #Array
  7.     #Inheritance
  8.     #Polymorphism
  9.     #Exception
  10. */
  11.  
  12. #include <iostream>
  13. #include <cstdlib> //Use for "Press any key to continue..."
  14.  
  15. using namespace std;
  16.  
  17. //Super Class
  18. class AreaCalculation{
  19. protected:
  20.     float base, height, width, total_area;
  21. public:
  22.     AreaCalculation(float base){
  23.         this->base = base;
  24.     }
  25.     AreaCalculation(float high, float wide){
  26.         height=high;
  27.         width=wide;
  28.     }
  29.     virtual void calculation(){
  30.     cout<<"Calculation going on"<<endl;
  31.     }
  32. };
  33.  
  34. //Sub Classes
  35. class Circle: public AreaCalculation{
  36. public:
  37.     Circle(float base):AreaCalculation(base){}
  38.     void calculation()
  39.     {
  40.         total_area= 3.1416*base*base;
  41.         cout<<"Area of the circle is: "<<total_area<<"square meter<s>"<<endl;
  42.     }
  43. };
  44.  
  45. class Rectangle : public AreaCalculation{
  46. public:
  47.     Rectangle(float high, float wide): AreaCalculation(high, wide){}
  48.     void calculation()
  49.     {
  50.         total_area = height*width;
  51.         cout<<"Area of the rectangle is: "<<total_area<<"square meter<s>"<<endl;
  52.     }
  53. };
  54.  
  55. class Triangle : public AreaCalculation{
  56. public:
  57.     Triangle(float high, float wide): AreaCalculation(high,wide){}
  58.     void calculation()
  59.     {
  60.         total_area = 0.5*height*width;
  61.         cout<<"Area of triangle is: "<<total_area<<"square meter<s>"<<endl;
  62.     }
  63. };
  64.  
  65. class Square: public AreaCalculation{
  66. public:
  67.     Square(float base): AreaCalculation(base){}
  68.     void calculation()
  69.     {
  70.         total_area = base*base;
  71.         cout<<"Area of the square is: "<<total_area<<"square meter<s>"<<endl;
  72.     }
  73. };
  74.  
  75. int main()
  76. {
  77.  
  78.         //For restarting the program if user wants.
  79.         char in;
  80.         do{
  81.         int opt;
  82.         cout<< "\n\n================================================================="<<endl;
  83.         cout<< "        Find the Areas of Different Geometrical Figures"<<endl;
  84.         cout<< "        ========= Select Your Desired Option =========="<<endl;
  85.         cout<< "================================================================="<<endl;
  86.  
  87.  
  88.         cout<<"\n\n1. Area of Circle\n2. Area of Rectangle\n3. Area of Triangle\n4. Area of Square\nChoose your number :  ";
  89.         cin>>opt;
  90.         cout<<"" <<endl;
  91.         AreaCalculation *ac;//object of super class
  92.         int no;
  93.         float hi, wi;
  94.         try
  95.         {
  96.                 if(opt == 1){
  97.                 try{ //If there is any problem found
  98.                 cout<<"How many circle<s>: ";
  99.                 cin>>no;
  100.                 cout<<"" <<endl;
  101.                 float ba[no];
  102.                 for(int i=0; i<no; i++)//including array for more than 1 conversion
  103.                 {
  104.                     cout<<"Enter the radius of circle "<<i+1<<" :";
  105.                     cin>>ba[i];
  106.                 }
  107.  
  108.                 for(int i=0; i<no; i++)
  109.                 {
  110.                     if(ba>0)
  111.                     {
  112.                         Circle c1(ba[i]);//object of sub class
  113.                         ac = &c1; // referencing object of sub class to super class
  114.                         ac->calculation();
  115.                     }
  116.                 else
  117.                 {
  118.                     throw "Radius can not be less than or equal to 0(zero).";
  119.                 }
  120.  
  121.                 }
  122.  
  123.                 }catch(const char *msg){
  124.                     cout<<"A problem occurred. "<<msg<<endl;
  125.                 }
  126.                 }
  127.                 else if(opt == 2){
  128.                 try{
  129.                 cout<<"How many rectangle<s>: ";
  130.                 cin>>no;
  131.                 float hi[no], wi[no];
  132.                 for(int i=0; i<no;i++)
  133.                 {
  134.                     cout<<"Enter the length of the rectangle "<<i+1<<" : ";
  135.                     cin>>hi[i];
  136.                     cout<<"Enter the width of the rectangle "<<i+1<<" : ";
  137.                     cin>>wi[i];
  138.                 }
  139.  
  140.                 for(int i=0; i<no;i++)
  141.                 {
  142.                     if(hi>0 && wi>0)
  143.                     {
  144.                         Rectangle rec(hi[i],wi[i]);
  145.                         ac = &rec;
  146.                         ac->calculation();
  147.                     }
  148.                     else
  149.                     {
  150.                         throw "Length and width can not be less than or equal to 0(zero).";
  151.                     }
  152.                 }
  153.  
  154.                 }catch(const char *msg){
  155.                 cout<<"A problem occurred. "<<msg<<endl;
  156.                 }
  157.                 }
  158.                 else if(opt == 3)
  159.                 {
  160.                 try
  161.                 {
  162.                 cout<<"Enter the height of the triangle: ";
  163.                 cin>>hi;
  164.                 cout<<"Enter the base of the triangle: ";
  165.                 cin>>wi;
  166.                 if(hi>0)
  167.                 {
  168.                     try
  169.                     {
  170.                         if(wi>0)
  171.                         {
  172.  
  173.                             Triangle tri(hi,wi);
  174.                             ac = &tri;
  175.                             ac->calculation();
  176.                         }
  177.                         else{
  178.                             throw "Base cannot be less than or equal to 0(zero).";
  179.                         }
  180.  
  181.  
  182.                 }catch (const char *msg){
  183.                         cout<<"\nA problem occurred. "<<msg<<endl;
  184.                     }
  185.                 }
  186.                 else
  187.                 {
  188.                 throw "Height can not be less than or equal to 0(zero).";
  189.                 }
  190.                 }catch(const char *msg){
  191.                     cout<<"\nA problem occurred. "<<msg<<endl;
  192.                 }
  193.                 }
  194.                 else if(opt == 4){
  195.                 try{
  196.                 cout<<"Enter how many square<s>: ";
  197.                 cin>>no;
  198.                 float ba[no];
  199.                 for(int i=0; i<no; i++)//including array for more than 1 conversion
  200.                 {
  201.                     cout<<"Enter the base of square no. "<<i+1<<" :";
  202.                     cin>>ba[i];
  203.                 }
  204.  
  205.                 for(int i=0; i<no; i++)
  206.                 {
  207.                     if(ba>0)
  208.                     {
  209.                         Square sq(ba[i]);
  210.                         ac = &sq; // referencing object of sub class to super class
  211.                         ac->calculation();
  212.                     }
  213.                 else
  214.                 {
  215.                     throw "Base can not be less than or equal to 0(zero).";
  216.                 }
  217.  
  218.                 }
  219.  
  220.                 }catch(const char *msg){
  221.                     cout<<"A problem occurred. "<<msg<<endl;
  222.                 }
  223.                 }
  224.             else{
  225.                 throw "Invalid Option. Please try again.";
  226.                 }
  227.             }catch(const char *msg)
  228.             {
  229.                 cout<<msg<<endl<<"Thank you.\n"<<endl;
  230.             }
  231.  
  232.             system("PAUSE");
  233.  
  234.  
  235.  
  236.             cout<<"Do you want to convert any other geometric figure?(Y/N): ";
  237.             cin>>in;
  238.  
  239.         }while(in=='y'||in=='Y');
  240.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement