Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. /* g++ -Wall toltrafic.cpp -o toltrafic */
  2. /* a traffic software */
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6.  
  7. class Vehicle
  8. {
  9.     public:
  10.     int v_type;
  11.  
  12.     Vehicle()
  13.     {
  14.     //std::cout <<"vehicle object created"<<std::endl;
  15.        
  16.     }
  17.  
  18.     ~Vehicle()
  19.     {
  20.     std::cout <<"vehicle object destroyed"<<std::endl;
  21.        
  22.     }
  23. };
  24.  
  25. class Lorry : public Vehicle
  26. {
  27. public:
  28.     public:
  29.     static const int type;
  30.     Lorry()
  31.     {
  32.     std::cout <<"lorry object created"<<std::endl;
  33.     v_type = type;
  34.     }
  35.  
  36.     ~Lorry()
  37.     {
  38.     std::cout <<"lorry object destroyed"<<std::endl;
  39.     }
  40. };
  41.  
  42. class Car : public Vehicle
  43. {
  44.     public:
  45.     static const int type;
  46.     Car()
  47.     {
  48.     std::cout <<"car object created"<<std::endl;
  49.     v_type = type; 
  50.     }
  51.    
  52.     ~Car()
  53.     {
  54.     std::cout <<"car object destroyed"<<std::endl;
  55.        
  56.     }
  57. };
  58.  
  59. const int Car::type = 2;
  60. const int Lorry::type = 1;
  61.  
  62. void ferryCounter(ferry)
  63. {
  64.     int counter = 0;
  65.     for(int i=0; i<10; i++)
  66.         {
  67.         if(ferry[i] != 0)
  68.             {
  69.             counter++;
  70.             }
  71.         }
  72.     std::cout << counter << " vehicles currently on the ferry. There is space for "<< 10-counter << " more vehicles" << std::endl;
  73. }
  74.  
  75. void carCounter(ferry)
  76. {
  77.     int counter = 0;
  78.  
  79.     for(int i=0; i<10; i++)
  80.         {
  81.         if(ferry[i]->v_type == 1)
  82.             {
  83.             counter++;
  84.             }
  85.         }
  86.     std::cout << counter << " cars currently on the ferry" << std::endl;
  87.    
  88. }
  89.  
  90. void lorryCounter(ferry)
  91. {
  92.     int counter = 0;
  93.     for(int i=0; i<10; i++)
  94.         {
  95.         if(ferry[i]->v_type == 2)
  96.             {
  97.             counter++;
  98.             }
  99.         }
  100.     std::cout << counter << " lorries currently on the ferry" << std::endl;
  101. }
  102.    
  103. void ferryList()
  104. {
  105. for(int i=0; i<10; i++)
  106.     {
  107.     if(ferry[i] != 0)
  108.         {
  109.         if(ferry[i]->v_type == 1)
  110.             {
  111.             std::cout << "slot " << i+1 <<": lorry" << std::endl;
  112.             }
  113.         if(ferry[i]->v_type == 2)
  114.             {
  115.             std::cout << "slot " << i+1 <<": car" << std::endl;
  116.             }
  117.         }
  118.     else
  119.         {
  120.         std::cout << "slot " << i+1 << ": empty" << std::endl;
  121.         }
  122.     }
  123. }
  124.    
  125. int main()
  126. {
  127.     // queue will be an array of vehicle objects
  128.     Vehicle* ferry[10] = {0,0,0,0,0,0,0,0,0,0};
  129.     ///Car* c = new Car;
  130.     ferry[0] = new Car;
  131.     ferry[1] = new Lorry;
  132.     //std::cout << ferry[0]->v_type << std::endl;
  133.     //std::cout << ( ferry[0]->v_type == Car::type ) << std::endl;
  134.    
  135.  
  136.     // here i need a method on each object to print the array
  137.     // a function could trawl the array calling the print method on each
  138.     // if the array is empty print nothing
  139.     //std::cout <<"Vehicles in queue: "<<Queued<<std::endl;
  140.  
  141.     // number of cars will be a method that queues the array Queued for car objects
  142.     //std::cout <<"Number of cars: "<<NumCars<<std::endl;
  143.     // query for lorry objects in the array and print it
  144.     //std::cout <<"Number of lorries: "<<NumLorries<<std::endl;
  145.     return 0;
  146.  
  147.     std::cout <<"----------------------"<<std::endl;
  148.     std::cout <<"Press 1 to add car to the queue."<<std::endl;
  149.     std::cout <<"Press 2 to add a lorry to the queue."<<std::endl;
  150.     std::cout <<"Press 3 to get the next car."<<std::endl;
  151.     std::cout <<"Press 4 to show the traffic queue."<<std::endl;
  152.     std::cout <<"Press 5 to quit."<<std::endl;
  153.     int key;
  154.     std::cin >>key;
  155.     return EXIT_SUCCESS;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement