I have classes with inheritance (for now only Car, more to add on like Bus, Truck etc) and adding to list.

1) How can i display using the subclass display function?
2) Can list<Vehicle> list be in a class?

#include <iostream>
#include <list>  
using namespace std;

class Vehicle
{
  protected:
      int plateNum; 
  public:
      Vehicle(int plateNum=0);    
      void Display() const ;
};

Vehicle::Vehicle(int b) {plateNum=b;}

void Vehicle::Display() const
{
   cout << "\nPlate Number" << plateNum << '\n' ;
}


class Car : public Vehicle
{
  private:
      string owner;    
  public: 
      Car(int plateNum=0, string owner ="");
      void Display() const ;
};

Car::Car(int a, string b):Vehicle(a) {owner=b ; }

void Car::Display() const
{
    Vehicle::Display();
    cout << "\nOwner " << owner << '\n' ;
}

int main()
{
    list<Vehicle> list;
    list<Vehicle>::iterator i;
    
    Car car1(11, "James");
    Car car2(22, "Peter");

    //To add on Bus bus1(...............);
    // list.push_back(bus1);
    
   list.push_back(car1);
    list.push_back(car2);
    
   for(i=list.begin(); i != list.end(); ++i) 
   i->Display();
   cout << endl;
   
   return 0;
}

Alas, you can't get desired effects with list<Vehicle>.
The point is that std::list<Vehicle> container might save only Vehicle (base) part of an object, however you want to save not an abstract Vehicle but a Car object, a Bus object and so on.
That's why virtual functions and abstract classes were invented.
Well, you can't board a vehicle. You need a car (or bus or truck). So let's define Vehicle as an abstract class with pure virtual function Display:

class Vehicle {
public:
    explicit Vehicle(unsighed num = 0):plateNum(num) {}
    virtual ~Vehicle() {} // virtual (empty) destructor
    unsigned getNum() const { return plateNum; }
    unsigned setNum(unsigned newnum) {
        unsigned oldnum = plateNum;
        plateNum = newnum;
        return oldnum;
    }
    virtual void Display() const = 0; // pure virtual
protected:
    unsighed plateNum;
};

Now class Vehicle can get and set plateNum but it does not know how to display Vehicle info. It declares Display function but it has not such member function (see = 0 designator). You can't create objects of Vehicle type because no such beasts as Vehicles in the real world!
Let's create concrete childs of an abstract class Vehicle - classes Car and Bus:

class Car::public Vehicle { // a car IS A Vehicle
public:
    explicit Car(unsigned num = 0, const std::string& own = ""):
        Vehicle(num), owner(own) // pass num to Vehicle ctor
    {} // alas, we can't set/get owner after ctor...
    void Display() const { // you MUST define Display
        std::cout << "Car: owner " << owner
            << " number " << getNum(); // << '\n' ?
    }
protected:
    std::string owner;
};

class Bus::public Vehicle { // a bus IS A Vehicle
public:
    explicit Bus(unsigned num = 0, unsigned n = 8):
        Vehicle(num), passengers(n)
    {}
    void Display() const {
        std::cout << "Bus: " << passengers << " seats"
            << " number " << getNum(); // << '\n' ?
    }
protected:
    unsigned passengers;
};

You can create Car and Bus objects: all functions defined. The explicit keyword before class ctor is optional (but useful in that case). It's the other story.

Now we are ready to create std::list of... not Vehicle but pointers to Vehicle!

typedef std::list<Vehicle*> Park;
Park park;

If we call virtual member function via pointer to base class then correspondent true child class object's function will be called.
Now:

park.push_back(new Car("Jason",123));
park.push_back(new Bus(45,67));
for (Park::const_iterator p = park.begin(); p != park.end(); ++p) {
    (*p)->Display();
    std::cout << '\n';
}
std::cout.flush();

Try this and see what happens ;)...
And don't forget to delete all dynamically allocated objects...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.