nrk009

oops ass 5 virtual

Oct 2nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class shape
  4. {
  5.     public:
  6.     shape()
  7.     {
  8.     }
  9.     virtual void compute()=0;
  10. };
  11. class triangle: public shape
  12. {
  13.     public:
  14.     double base,height,area;
  15.     triangle()
  16.     {
  17.  
  18.     }
  19.     triangle(double b1,double h1):shape()
  20.     {
  21.        base=b1;
  22.        height=h1;
  23.     }
  24.     void compute()
  25.     {
  26.       cout<<"Base of triangle: "<<base<<"\n";
  27.       cout<<"Height of triangle: "<<height<<"\n\n";
  28.       area=0.5*base*height;
  29.       cout<<"Area of triangle: "<<area<<"\n";
  30.     }
  31. };
  32. class rectangle:public shape
  33. {
  34.     public:
  35.     double length,width,ar;
  36.     rectangle()
  37.     {
  38.  
  39.     }
  40.     rectangle(double l1,double w1):shape()
  41.     {
  42.       length=l1;
  43.       width=w1;
  44.     }
  45.     void compute()
  46.     {
  47.       cout<<"length of rectangle: "<<length<<"\n";
  48.       cout<<"width of rectangle: "<<width<<"\n\n";
  49.       ar=length*width;
  50.       cout<<"Area of rectangle: "<<ar<<"\n";
  51.     }
  52. };
  53.  
  54. int main()
  55. {
  56.    double l,w,h,b;
  57.    int i,x,p=1;
  58.    shape *s;
  59.    triangle t;
  60.    rectangle r;
  61.    while(p)
  62.    {
  63.    cout<<"\nEnter the operation you want to perform: ";
  64.    cout<<"\n1. Area of triangle";
  65.    cout<<"\n2. Area of rectangle";
  66.    cout<<"\n3. Exit";
  67.    cout<<"\n\n Enter your choice: ";
  68.    cin>>x;
  69.    switch(x)
  70.    {
  71.         case 1:
  72.         cout<<"\n\nEnter the dimension of triangle";
  73.         cout<<"\n\nEnter the height of the triangle: ";
  74.         cin>>h;
  75.         cout<<"\nEnter the base of the triangle: ";
  76.         cin>>b;
  77.         t=triangle(h,b);
  78.         s=&t;
  79.         s->compute();
  80.         break;
  81.  
  82.         case 2:
  83.         {
  84.         cout<<"\n\nEnter the dimension of Rectangle";
  85.         cout<<"\n\nEnter the length of the rectangle: ";
  86.         cin>>l;
  87.         cout<<"\nEnter the width of the rectangle: ";
  88.         cin>>w;
  89.         r=rectangle(l,w);
  90.         s=&r;
  91.         s->compute();
  92.         break;
  93.         }
  94.  
  95.         case 3:
  96.             exit(0);
  97.         break;
  98.  
  99.    }
  100.    cout<<"\n\nDo you want to continue(1/0) ";
  101.    cout<<"\nEnter your choice: ";
  102.    cin>>p;
  103.    }
  104. }
Add Comment
Please, Sign In to add comment