5 Replies - 1378 Views - Last Post: 29 August 2011 - 07:17 AM Rate Topic: -----

#1 a'ha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-August 11

Question on inheritance, polymorphism and composition

Posted 29 August 2011 - 06:04 AM

i have to write a c++ code implementing inheritance, polymorphism and composition. The question is as follows:


Problem Statement
You are hired to develop a system that keeps track of all drivers and vehicles in a company. The company has many drivers and many different types of vehicles: bike, car, bus, truck, etc. A vehicle may have a driver assigned to it or no driver assigned to it. A driver is assigned to handle maximum one or no vehicle at a time. Sample attributes are listed below:
• Vehicle: Vehicle number, type, capacity, driver assigned
• Driver: driver id, name, vehicle assigned.

The system must be able to perform the following functions:
1. Create/Delete drivers and vehicles
2. Assign/Unassign a driver to/from a vehicle
3. List all drivers (all attributes must be shown)
4. List all vehicles (all attributes must be shown)
5. List all instances of a specified type of vehicle (all attributes of the instance must be shown)

The attached code below is what i have done so far which came along with errors :
#include <iostream>
#include <vector>
#include <string>
using namespace std;



class Driver{
private:

    int ID;
    string name;
   string vehicle;

public:

    Driver(){
        int ID= 0;
        string name= " ";
        string vehicle= " ";
    }

    ~Driver();

    void setID(int ID)
    {
        this -> ID= ID;
    }

    void setname(string name)
    {
        this -> name= name;
    }

    
    void setVehicle (string vehicle)
    {
      this->vehicle = vehicle;
      }
    
     void print() const
      {
           cout << "Name = " << name << " .Id = " << ID << " .Vehicle = " << vehicle << endl;
           }
};

class addDriver: public Driver{
      public:  
               
      Driver(void add (Driver& driver),{
        driver.setVehicle (name);
        drivers.push_back (driver);
      }  


class Vehicle{
private:
    int num;
    string type;
    int capacity;
    Driver*driver;

public:
       vector <Driver> drivers;
        Vehicle(){
        int num= 0;
        string type= "";
        int capacity= 0;
        driver= new Driver;
    }

    Vehicle(int num, string type, int capacity, Driver*driver)
    :num(num), type(type), capacity(capacity), driver(Driver){}

    ~Vehicle (){
        delete driver;}

   virtual void use() { cout << " vehicle in use" << endl;}

    void setnum( int num){
        this-> num= num;
    }

    int getnum() { return num; }

    void setcapacity(int capacity) {
        this -> capacity= capacity;
    }

    int getcapacity() { return capacity; }

    void settype(string type) {
        this -> type= type;
    }

    string gettype() {return type; }
    
 
  void add (Driver& driver)
  {
    driver.setVehicle (name);
    drivers.push_back (driver);
  }
  void print() const
  {
    cout << endl << name << " list:\n";
    cout << "---------\n";
    for (int i = 0; i < drivers.size(); i++)
      drivers[i].print();
    
    
    
    
    
    

};

class Taxi: public Vehicle{

private:
int taxi_id;

public:

Vehicle(int taxi_id, int num, int capacity, string type, Driver*driver)
    : taxi_id(taxi_id), Vehicle(num), Vehicle(capacity), Vehicle(type), Vehicle(Driver){
     cout << "a taxi is created: " << " taxi's id: " << taxi_id << "taxi plate number:" << getnum() << " " << getcapacity() << " " << gettype()
     << "taxi's driver:" << Driver << endl;
    }

    void use() { cout << "taxi in use \n;"}

    void setid(int taxi_id){
        this -> taxi_id= taxi_id;
    }

    int getid() { return taxi_i; }
    
    void add (Driver& driver)
  {
    driver.setVehicle (name);
    drivers.push_back (driver);
  }
  void print() const
  {
    cout << endl << name << " list:\n";
    cout << "---------\n";
    for (int i = 0; i < drivers.size(); i++)
      drivers[i].print();
    


class Car: public Vehicle{
    private:
    int car_id;

    public:
    Car(int car_id, int num, int capacity, srting type)
        :car_id(car_id), num(num), capacity(capacity), type(type), Vehicle(Driver){
        cout << " a car is created: "<< "car's id: " << car_id  <<" car plate number : " << getnum() << " " << getcapacity() << " " << gettype()
            << "car's driver:" << Driver<< endl;
        }

        void use() { cout << " car in use \n";}

        void setCarId(int car_id){
         this -> car_id= car_id;
        }

        int getCarId() { return car_id;}
        
         void add (Driver& driver)
         {
          driver.setVehicle (name);
          drivers.push_back (driver);
          }
           void print() const
            {
              cout << endl << name << " list:\n";
               cout << "---------\n";
               for (int i = 0; i < drivers.size(); i++)
                drivers[i].print();
        
};

class Truck : public Vehicle{
    private:
    int truckId;

    public:
    Truck(int truckId, int num, int capacity, string type)
        :truckId(truckId), Vehicle(num), Vehicle(capacity), Vehicle(type){
        cout << "a truck is created:" << " truck id : " << truckId << "truck's plate number: " <<  getnum()<<" " << getcapacity << " " <<gettype()
            << " truck's driver:" << Driver << endl; }

        void use() { cout << " truck in use " << endl;

        void setTruckId(int truckId){
         this -> truckId= truckId;
        }

        int getTruckId() { return truckId;}
        
         void add (Driver& driver)
      {
        driver.setVehicle (name);
        drivers.push_back (driver);
      }
      void print() const
      {
        cout << endl << name << " list:\n";
        cout << "---------\n";
        for (int i = 0; i < drivers.size(); i++)
          drivers[i].print();

};

    void createDriver() {
        cout << " enter id "
            << " enter name" << endl;
    
        cin >> ID;
        cin >> name;
    
        D1.push_back(ID, name));
    
    };
    
    void destroyDriver() {
     cout << " enter id to be deleted: "
            << " enter name to be deleted: " <<endl;
    
        cin >> ID;
        cin >> name;
    
        D1.clear(Driver(ID, name));
    
    };
    
    
    void createVehicle() {
        cout << "enter number "
            << "enter capacity"
            << "enter type" << endl;
    
        cin >> num;
            >> capacity;
            >> type;
    
         V1.push_back(Vehicle(num,capacity,type));
    };
    
    
    void deleteVehicle() {
        cout << " enter vehicle number to be deleted "
            << "enter capacity"
            << "enter type" << endl;
    
        cin >> num;
            >> capacity;
            >> type;
    
            V1.clear(Vehicle( num, capacity, type));
    };
    
    void assigndriver() {
    };
    
    void unassigndriver() {
    };
    
    void listdrivers(){
    };
    
    void listvehicles(){
    };
    
  vector<Driver> D1;
  vector<Vehicle> V1;  

int main(){

  int size;
  string name;
  int id;
  string vehicle;

  cout << "HOW MANY DRIVERS TO ASSIGN? ";
  cin >> size;
  cout << endl;
  for (int i = 0; i < size; i++) {
    cout << "Driver #" << i+1<< " : Enter name    = ";
    cin >> name;
    cout << "Driver #" << i+1<< " : Enter id      = ";
    cin >> id;
    cout << "Driver #" << i+1<< " : Enter vehicle = ";
    cin >> vehicle;
    Driver driver;
    driver.setname (name);
    driver.setID (id);
    driver.setVehicle(vehicle);
    drivers.push_back (driver);
  }

  Vehicle car ("Car");
  Vehicle taxi ("Taxi");
  Vehicle truck ("Truck");

  cout << endl;
  cout << "All drivers:\n";
  cout << "-----------\n";
  for (int i = 0; i < size; i++)
    drivers[i].print();

  car.use();

  truck.use();

  taxi.use();

  cout << endl;

}


This post has been edited by macosxnerd101: 29 August 2011 - 06:07 AM
Reason for edit:: Please use code tgas


Is This A Good Question/Topic? 0
  • +

Replies To: Question on inheritance, polymorphism and composition

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: Question on inheritance, polymorphism and composition

Posted 29 August 2011 - 06:20 AM

What are the errors that you're getting? It helps if you can paste all of the compiler errors that you're seeing
Was This Post Helpful? 0
  • +
  • -

#3 a'ha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-August 11

Re: Question on inheritance, polymorphism and composition

Posted 29 August 2011 - 06:38 AM

50 F:\compt prog2\assignment2.cpp expected identifier before '{' token
50 F:\compt prog2\assignment2.cpp expected `,' or `...' before '{' token
319 F:\compt prog2\assignment2.cpp expected `)' at end of input
319 F:\compt prog2\assignment2.cpp ISO C++ forbids declaration of `Driver' with no type
319 F:\compt prog2\assignment2.cpp ISO C++ forbids declaration of `parameter' with no type
319 F:\compt prog2\assignment2.cpp expected `;' at end of input
319 F:\compt prog2\assignment2.cpp expected `;' at end of input
319 F:\compt prog2\assignment2.cpp expected `}' at end of input
319 F:\compt prog2\assignment2.cpp expected unqualified-id at end of input
319 F:\compt prog2\assignment2.cpp expected `,' or `;' at end of input

These are the errors, what also puzzles me is that one line has more than one error.
Was This Post Helpful? 0
  • +
  • -

#4 DustyD  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 75
  • Joined: 19-August 09

Re: Question on inheritance, polymorphism and composition

Posted 29 August 2011 - 06:48 AM

Hey aha,

When you have this sort of situation, you have to work on solving the very first error and only that one unless you know exactly what's wrong, because generally solving one error will solve many of the others. I can see why you have the first error: At line 50 you put a comma that shouldn't be there.. try getting rid of that a try again, you should have much less errors :)
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Question on inheritance, polymorphism and composition

Posted 29 August 2011 - 07:00 AM

I see that addDriver::Vehicle::print() is missing a closing } -- in fact you seem to have a lot of missing closing brackets.


When I asked astyle to format your code I got this:
#include <iostream>
#include <vector>
#include <string>
using namespace std;



class Driver {
    private:

        int ID;
        string name;
        string vehicle;

    public:

        Driver() {
            int ID = 0;
            string name = " ";
            string vehicle = " ";
        }

        ~Driver();

        void setID(int ID) {
            this -> ID = ID;
        }

        void setname(string name) {
            this -> name = name;
        }


        void setVehicle (string vehicle) {
            this->vehicle = vehicle;
        }

        void print() const {
            cout << "Name = " << name << " .Id = " << ID << " .Vehicle = " << vehicle << endl;
        }
};

class addDriver: public Driver {
    public:

        Driver(void add (Driver& driver), {
            driver.setVehicle (name);
            drivers.push_back (driver);
        }


        class Vehicle {
private:
            int num;
            string type;
            int capacity;
            Driver*driver;

public:
            vector <Driver> drivers;
            Vehicle() {
                int num = 0;
                string type = "";
                int capacity = 0;
                driver = new Driver;
            }

            Vehicle(int num, string type, int capacity, Driver*driver)
                : num(num), type(type), capacity(capacity), driver(Driver) {}

            ~Vehicle () {
                delete driver;
            }

            virtual void use() {
                cout << " vehicle in use" << endl;
            }

            void setnum( int num) {
                this-> num = num;
            }

            int getnum() {
                return num;
            }

            void setcapacity(int capacity) {
                this -> capacity = capacity;
            }

            int getcapacity() {
                return capacity;
            }

            void settype(string type) {
                this -> type = type;
            }

            string gettype() {
                return type;
            }


            void add (Driver& driver) {
                driver.setVehicle (name);
                drivers.push_back (driver);
            }
            void print() const
            {
                cout << endl << name << " list:\n";
                cout << "---------\n";

                for (int i = 0; i < drivers.size(); i++)
                    drivers[i].print();







            };

            class Taxi: public Vehicle{

                private:
                    int taxi_id;

                public:

                    Vehicle(int taxi_id, int num, int capacity, string type, Driver*driver)
                        : taxi_id(taxi_id), Vehicle(num), Vehicle(capacity), Vehicle(type), Vehicle(Driver) {
                        cout << "a taxi is created: " << " taxi's id: " << taxi_id << "taxi plate number:" << getnum() << " " << getcapacity() << " " << gettype()
                        << "taxi's driver:" << Driver << endl;
                    }

                    void use() {
                        cout << "taxi in use \n;"
                    }

                    void setid(int taxi_id) {
                        this -> taxi_id = taxi_id;
                    }

                    int getid() {
                        return taxi_i;
                    }

                    void add (Driver& driver) {
                        driver.setVehicle (name);
                        drivers.push_back (driver);
                    }
                    void print() const
                    {
                        cout << endl << name << " list:\n";
                        cout << "---------\n";

                        for (int i = 0; i < drivers.size(); i++)
                            drivers[i].print();



                        class Car: public Vehicle{
                            private:
                                int car_id;

                            public:
                                Car(int car_id, int num, int capacity, srting type)
                                    : car_id(car_id), num(num), capacity(capacity), type(type), Vehicle(Driver) {
                                    cout << " a car is created: " << "car's id: " << car_id  << " car plate number : " << getnum() << " " << getcapacity() << " " << gettype()
                                    << "car's driver:" << Driver << endl;
                                }

                                void use() {
                                    cout << " car in use \n";
                                }

                                void setCarId(int car_id) {
                                    this -> car_id = car_id;
                                }

                                int getCarId() {
                                    return car_id;
                                }

                                void add (Driver& driver) {
                                    driver.setVehicle (name);
                                    drivers.push_back (driver);
                                }
                                void print() const
                                {
                                    cout << endl << name << " list:\n";
                                    cout << "---------\n";

                                    for (int i = 0; i < drivers.size(); i++)
                                        drivers[i].print();

                                };

                                class Truck : public Vehicle{
                                    private:
                                        int truckId;

                                    public:
                                        Truck(int truckId, int num, int capacity, string type)
                                            : truckId(truckId), Vehicle(num), Vehicle(capacity), Vehicle(type) {
                                            cout << "a truck is created:" << " truck id : " << truckId << "truck's plate number: " <<  getnum() << " " << getcapacity << " " << gettype()
                                            << " truck's driver:" << Driver << endl;
                                        }

                                        void use() {
                                            cout << " truck in use " << endl;
                                            void setTruckId(int truckId) {
                                                this -> truckId = truckId;
                                            }
                                            int getTruckId() {
                                                return truckId;
                                            }
                                            void add (Driver & driver) {
                                                driver.setVehicle (name);
                                                drivers.push_back (driver);
                                            }
                                            void print() const {
                                                cout << endl << name << " list:\n";
                                                cout << "---------\n";

                                                for (int i = 0; i < drivers.size(); i++)
                                                    drivers[i].print();
                                            };
                                            void createDriver() {
                                                cout << " enter id "
                                                     << " enter name" << endl;
                                                cin >> ID;
                                                cin >> name;
                                                D1.push_back(ID, name));
                                            };
                                            void destroyDriver() {
                                                cout << " enter id to be deleted: "
                                                     << " enter name to be deleted: " << endl;
                                                cin >> ID;
                                                cin >> name;
                                                D1.clear(Driver(ID, name));
                                            };
                                            void createVehicle() {
                                                cout << "enter number "
                                                     << "enter capacity"
                                                     << "enter type" << endl;
                                                cin >> num;
                                                        >> capacity;
                                                        >> type;
                                                V1.push_back(Vehicle(num, capacity, type));
                                            };
                                            void deleteVehicle() {
                                                cout << " enter vehicle number to be deleted "
                                                     << "enter capacity"
                                                     << "enter type" << endl;
                                                cin >> num;
                                                        >> capacity;
                                                        >> type;
                                                V1.clear(Vehicle( num, capacity, type));
                                            };
                                            void assigndriver() {
                                            };
                                            void unassigndriver() {
                                            };
                                            void listdrivers() {
                                            };
                                            void listvehicles() {
                                            };
                                            vector<Driver> D1;
                                            vector<Vehicle> V1;
                                            int main() {
                                                int size;
                                                string name;
                                                int id;
                                                string vehicle;
                                                cout << "HOW MANY DRIVERS TO ASSIGN? ";
                                                cin >> size;
                                                cout << endl;

                                                for (int i = 0; i < size; i++) {
                                                    cout << "Driver #" << i + 1 << " : Enter name    = ";
                                                    cin >> name;
                                                    cout << "Driver #" << i + 1 << " : Enter id      = ";
                                                    cin >> id;
                                                    cout << "Driver #" << i + 1 << " : Enter vehicle = ";
                                                    cin >> vehicle;
                                                    Driver driver;
                                                    driver.setname (name);
                                                    driver.setID (id);
                                                    driver.setVehicle(vehicle);
                                                    drivers.push_back (driver);
                                                }

                                                Vehicle car ("Car");
                                                Vehicle taxi ("Taxi");
                                                Vehicle truck ("Truck");
                                                cout << endl;
                                                cout << "All drivers:\n";
                                                cout << "-----------\n";

                                                for (int i = 0; i < size; i++)
                                                    drivers[i].print();

                                                car.use();
                                                truck.use();
                                                taxi.use();
                                                cout << endl;
                                            }


See how everything keep drifting to the right. This is caused by missing '}' marks.


note that this is not how programmer's really work. Most of us don't sit down and write 400lines of code and then prey that it compiles. We work in chunks at a time, compiling every so often to ensure we are working on the right path.

Use the compiler to HELP you write the code. Don't fight with it after you have 100's of lines of code riddled with syntax errors. If I write 300 lines of code I can guarantee you that there will be several syntax errors and I have been doing this for will over 18 years.

Write a program containing just Driver and a little main() -- use main() to test the class. Remember to compile the program after you add each function to Driver to ensure that the syntax is correct so far.

When you know that Driver is done, then add the next class, compiling and testing as you go.

Proper indention is important!
Are you using Dev-Cpp -- if so maybe you should think about a different IDE as indention in Dev-Cpp is a real hassle.
Was This Post Helpful? 0
  • +
  • -

#6 a'ha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-August 11

Re: Question on inheritance, polymorphism and composition

Posted 29 August 2011 - 07:17 AM

ok Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1