Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* g++ -Wall toltrafic.cpp -o toltrafic */
- /* a traffic software */
- #include <iostream>
- #include <cstdlib>
- class Vehicle
- {
- public:
- int v_type;
- Vehicle()
- {
- //std::cout <<"vehicle object created"<<std::endl;
- }
- ~Vehicle()
- {
- std::cout <<"vehicle object destroyed"<<std::endl;
- }
- };
- class Lorry : public Vehicle
- {
- public:
- public:
- static const int type;
- Lorry()
- {
- std::cout <<"lorry object created"<<std::endl;
- v_type = type;
- }
- ~Lorry()
- {
- std::cout <<"lorry object destroyed"<<std::endl;
- }
- };
- class Car : public Vehicle
- {
- public:
- static const int type;
- Car()
- {
- std::cout <<"car object created"<<std::endl;
- v_type = type;
- }
- ~Car()
- {
- std::cout <<"car object destroyed"<<std::endl;
- }
- };
- const int Car::type = 2;
- const int Lorry::type = 1;
- void ferryCounter(ferry)
- {
- int counter = 0;
- for(int i=0; i<10; i++)
- {
- if(ferry[i] != 0)
- {
- counter++;
- }
- }
- std::cout << counter << " vehicles currently on the ferry. There is space for "<< 10-counter << " more vehicles" << std::endl;
- }
- void carCounter(ferry)
- {
- int counter = 0;
- for(int i=0; i<10; i++)
- {
- if(ferry[i]->v_type == 1)
- {
- counter++;
- }
- }
- std::cout << counter << " cars currently on the ferry" << std::endl;
- }
- void lorryCounter(ferry)
- {
- int counter = 0;
- for(int i=0; i<10; i++)
- {
- if(ferry[i]->v_type == 2)
- {
- counter++;
- }
- }
- std::cout << counter << " lorries currently on the ferry" << std::endl;
- }
- void ferryList()
- {
- for(int i=0; i<10; i++)
- {
- if(ferry[i] != 0)
- {
- if(ferry[i]->v_type == 1)
- {
- std::cout << "slot " << i+1 <<": lorry" << std::endl;
- }
- if(ferry[i]->v_type == 2)
- {
- std::cout << "slot " << i+1 <<": car" << std::endl;
- }
- }
- else
- {
- std::cout << "slot " << i+1 << ": empty" << std::endl;
- }
- }
- }
- int main()
- {
- // queue will be an array of vehicle objects
- Vehicle* ferry[10] = {0,0,0,0,0,0,0,0,0,0};
- ///Car* c = new Car;
- ferry[0] = new Car;
- ferry[1] = new Lorry;
- //std::cout << ferry[0]->v_type << std::endl;
- //std::cout << ( ferry[0]->v_type == Car::type ) << std::endl;
- // here i need a method on each object to print the array
- // a function could trawl the array calling the print method on each
- // if the array is empty print nothing
- //std::cout <<"Vehicles in queue: "<<Queued<<std::endl;
- // number of cars will be a method that queues the array Queued for car objects
- //std::cout <<"Number of cars: "<<NumCars<<std::endl;
- // query for lorry objects in the array and print it
- //std::cout <<"Number of lorries: "<<NumLorries<<std::endl;
- return 0;
- std::cout <<"----------------------"<<std::endl;
- std::cout <<"Press 1 to add car to the queue."<<std::endl;
- std::cout <<"Press 2 to add a lorry to the queue."<<std::endl;
- std::cout <<"Press 3 to get the next car."<<std::endl;
- std::cout <<"Press 4 to show the traffic queue."<<std::endl;
- std::cout <<"Press 5 to quit."<<std::endl;
- int key;
- std::cin >>key;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement