StefiIOE

Playground Multiple inheritance

May 17th, 2020
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Toy {
  7. public:
  8.     virtual float getVolume() = 0;
  9.     virtual float getWeight() = 0;
  10. };
  11.  
  12. class Shape  {
  13. protected:
  14.     char color[100];
  15.     int density;
  16. public:
  17.     Shape(char *color="",int density = 0)
  18.     {
  19.         strcpy(this->color,color);
  20.         this->density=density;
  21.    
  22.     }
  23.     Shape(const Shape &s)
  24.     {
  25.         strcpy(this->color,s.color);
  26.         this->density=s.density;
  27.     }
  28. };
  29. class Cubes :public Shape ,public Toy
  30. {
  31.     private:
  32.     int height;
  33.     int width;
  34.     int depth;
  35.     public:
  36.     Cubes(char *color="",int density =0,int height =0 ,int width =0 ,int depth =0): Shape(color , density)
  37.     {
  38.         this->height=height;
  39.         this->width=width;
  40.         this->depth=depth;
  41.     }
  42.     Cubes(const Cubes &c) : Shape(c)
  43.     {
  44.         this->height=c.height;
  45.         this->width=c.width;
  46.         this->depth=c.depth;
  47.     }
  48.     float getVolume()
  49.     {
  50.     return height * width * depth;
  51.     }
  52.     float getWeight()
  53.     {
  54.        
  55.       return getVolume() * density;
  56.     }
  57.  
  58.    
  59. };
  60. class Balls :public Shape ,public Toy
  61. {
  62.  
  63.     private:
  64.     int radius;
  65.     public:
  66.     Balls(char *color="",int density = 0, int radius = 0 ): Shape(color , density)
  67.     {
  68.     this->radius=radius;
  69.     }
  70.     Balls(const Balls &b) :Shape(b)
  71.     {
  72.      this->radius=b.radius;
  73.     }
  74.     float getWeight()
  75.     {
  76.     return getVolume() * density;
  77.     }
  78.     float getVolume()
  79.     {
  80.     return 3.14*4/3 * radius * radius * radius;
  81.     }
  82.  
  83.    
  84. };
  85.  
  86.  
  87.  
  88.  
  89. int main(){
  90.    
  91. int n ;  
  92.     cin>>n;
  93.     char color[100];
  94.         int density;
  95.         int height;
  96.         int width;
  97.         int depth;
  98.         int radius;
  99.        
  100.     Toy **toys=new Toy*[n];
  101.     for(int i = 0 ; i < n ; i ++)
  102.     {
  103.         int s;
  104.         cin>>s;
  105.         if(s == 1)
  106.         {
  107.         cin>>color>>density>>radius;
  108.             toys[i]=new Balls(color,density,radius);
  109.         }
  110.         if(s == 2)
  111.         {
  112.         cin>>color>>density>>height>>width>>depth;
  113.                   //  cout<<color<<endl<<density<<endl<<height<<endl<<width<<endl<<depth; do tuka raboti
  114.  
  115.             toys[i]=new Cubes(color,density,height,width,depth);
  116.         }
  117.    
  118.     }
  119.    
  120.     cin>>color>>density>>height>>width>>depth;
  121.      //cout<<color<<endl<<density<<endl<<height<<endl<<width<<endl<<depth<<endl;
  122.     Cubes Petra(color,density,height,width,depth);
  123.          //cout<<color<<endl<<density<<endl<<height<<endl<<width<<endl<<depth<<endl;
  124.     float sum=0.0;
  125.     for(int i = 0 ; i < n ; i ++)
  126.     {
  127.         sum+=toys[i]->getWeight();
  128.     }
  129.     if(sum>Petra.getWeight())
  130.    
  131.     cout<<"YES"<<endl;
  132.         else
  133.         {
  134.         cout<<"NO"<<endl;
  135.         }
  136.         //cout<<"test"<<endl;            
  137.         //cout<<sum;
  138.    
  139.    
  140.    
  141.  
  142.  
  143.    
  144.     float maxVolume=0;
  145.     for(int i=0;i<n;i++)
  146.        
  147.     {
  148.         if(toys[i]->getVolume() >maxVolume)
  149.         {
  150.             maxVolume=toys[i]->getVolume();
  151.         }
  152.     }
  153.     float difference=0;
  154.    
  155.     difference=maxVolume - Petra.getVolume();
  156.    
  157.     if(difference<0)
  158.     {
  159.     difference=0-difference;
  160.         }
  161.     cout<<"The difference is: "<<difference<<endl;
  162.  
  163.  
  164.    
  165.  
  166.    
  167.  
  168.     return 0;
  169. }
Add Comment
Please, Sign In to add comment