Advertisement
StefiIOE

Vehicle Polymorphism

May 17th, 2020
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Vehicle
  5. {
  6.     protected:
  7.     float mass;
  8.     int width;
  9.     int height;
  10.     public:
  11.     Vehicle(float mass=0.0 , int width = 0 , int height = 0 )
  12.     {
  13.     this->mass=mass;
  14.         this->width=width;
  15.         this->height=height;
  16.     }
  17.  
  18.     Vehicle(const Vehicle &v)
  19.     {
  20.         this->mass=v.mass;
  21.         this->width=v.width;
  22.         this->height=v.height;
  23.    
  24.     }
  25.     float getMass(){return this->mass;}
  26.     int getWidth(){return this->width;}
  27.     int getHeight(){return this->height;}
  28.     virtual int dailyPrice()=0;
  29.    
  30. };
  31.  
  32. class Automobile : public Vehicle
  33. {
  34.     private:
  35.     int n ;
  36.     public:
  37.     Automobile(float mass=0.0 , int width = 0 , int height = 0 , int n = 0 ) : Vehicle(mass,width, height)
  38.     {
  39.     this-> n = n ;
  40.     }
  41.     Automobile (const Automobile &a) : Vehicle(a)
  42.     {
  43.     this->n=a.n;
  44.     }
  45.     int dailyPrice()
  46.     {
  47.         if(n<5){return 100;}
  48.         else return 130;
  49.     }
  50.    
  51. };
  52. class Bus : public Vehicle
  53. {
  54.     private:
  55.     int n ;
  56.     public:
  57.     Bus(float mass=0.0 , int width = 0 , int height = 0 , int n = 0 ) : Vehicle(mass,width, height)
  58.     {
  59.     this-> n = n ;
  60.     }
  61.     Bus (const Bus &b) : Vehicle(b)
  62.     {
  63.     this->n=b.n;
  64.     }
  65.     int dailyPrice()
  66.     {
  67.     return n*5;
  68.     }
  69.  
  70.    
  71. };
  72. class Truck :public Vehicle
  73. {
  74.  
  75.     private:
  76.     float max;
  77.     public:
  78.         Truck(float mass=0 , int width = 0 , int height = 0 , float max= 0 ) : Vehicle(mass,width, height)
  79.     {
  80.     this-> max= max ;
  81.     }
  82.     Truck (const Truck &t) : Vehicle(t)
  83.     {
  84.     this->max=t.max;
  85.     }
  86.     int dailyPrice()
  87.     {
  88.     return (mass + max) * 0.02;
  89.     }
  90.  
  91.    
  92.        
  93. };
  94. class Parking
  95. {
  96.     protected:
  97.     Vehicle **vehicle;
  98.     int n ;
  99.     public:
  100.     Parking (){
  101.     vehicle= new Vehicle*[100];
  102.         n=0;
  103.     }
  104.     Parking(Vehicle ** vehicle , int n)
  105.     {
  106.     this->n=n;
  107.         this->vehicle=new Vehicle*[n];
  108.         for(int i = 0 ; i < n ; i ++)
  109.         {
  110.         vehicle[i]=vehicle[i];
  111.         }
  112.     }
  113.    
  114.     Parking operator+=(Vehicle *v)
  115.     {
  116.        
  117.         vehicle[n]=v;
  118.         n++;
  119.         return *this;
  120.     }
  121.    
  122.     float totallMass()
  123.     {
  124.         float sum=0.0;
  125.         for(int i = 0 ; i < n ; i++)
  126.         {
  127.         sum+=vehicle[i]->getMass();
  128.         }
  129.         return sum;
  130.     }
  131.     int vehiclesWiderThan(int width)
  132.     {
  133.         int count=0;
  134.         for(int i = 0 ; i < n ; i ++)
  135.         {
  136.             if(vehicle[i]->getWidth()>width)
  137.             {
  138.             count++;
  139.             }
  140.         }
  141.         return count;
  142.    
  143.     }
  144.     void print()
  145.     {
  146.     int count = 0, count1=0, count2=0;
  147.         for(int i = 0 ; i< n ; i ++)
  148.         {
  149.             Automobile *a=dynamic_cast<Automobile*>(vehicle[i]);
  150.             {
  151.                 if (a!=0){count++;}
  152.             }
  153.             Bus *b=dynamic_cast<Bus*>(vehicle[i]);
  154.             {
  155.                 if(b!=0){count1++;}
  156.             }
  157.             Truck *t=dynamic_cast<Truck*>(vehicle[i]);
  158.             {
  159.                 if(t!=0){count2++;}
  160.             }
  161.         }
  162.         cout<<"Automobiles: "<<count<<endl<<"Busses: "<<count1<<endl<<"Trucks: "<<count2<<endl;
  163.        
  164.     }
  165.     int greaterMaxWeightThan(Vehicle &v)
  166.     {
  167.     int count=0;
  168.         for(int i = 0 ; i < n ; i++)
  169.         {
  170.        Truck *t=dynamic_cast<Truck*>(vehicle[i]);
  171.             if(t!=0)
  172.             {
  173.                 if(v.getMass()<vehicle[i]->getMass())
  174.                 {
  175.                 count++;
  176.                 }
  177.             }
  178.         }
  179.         return count;
  180.     }
  181.     int dailyProfit()
  182.     {
  183.         int profit = 0 ;
  184.         for(int i = 0; i < n ; i ++)
  185.         {
  186.         profit+=vehicle[i]->dailyPrice();
  187.         }
  188.         return profit;
  189.     }
  190.        ~Parking()
  191.     {
  192.         /*for(int i = 0 ; i < n ; i ++)
  193.         {
  194.         delete vehicle[i];
  195.         }
  196.         delete []vehicle;
  197.         */
  198.     }
  199.  
  200.    
  201. };
  202. int main() {
  203.     Parking p;
  204.     int n;
  205.     cin >> n;
  206.     int width, height, broj;
  207.     float mass, max_weight;
  208.     for (int i = 0; i < n; i++) {
  209.         int type;
  210.         cin >> type;
  211.         if (type == 1) {
  212.             cin >> mass >> width >> height >> broj;
  213.             Automobile *a = new Automobile(mass, width, height, broj);
  214.             p += a;
  215.         }
  216.         if (type == 2) {
  217.             cin >> mass >> width >> height >> broj;
  218.             p += new Bus(mass, width, height, broj);
  219.         }
  220.         if (type == 3) {
  221.             cin >> mass >> width >> height >> max_weight;
  222.             p += new Truck(mass, width, height, max_weight);
  223.         }
  224.     }
  225.  
  226.     p.print();
  227.     cout << "\nDaily profit: " << p.dailyProfit() << endl;
  228.     cout << "Total mass: " << p.totallMass() << endl;
  229.     cout << "Number wider then 5 is: " << p.vehiclesWiderThan(5) << endl;
  230.     Automobile a(1200, 4, 3, 5);
  231.     cout << "Number of trucks with max weight larger then the automobile is: " << p.greaterMaxWeightThan(a) << endl;
  232.     return 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement