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
      int plateNum; 
      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
      string owner;    
      Car(int plateNum=0, string owner ="");
      void Display() const ;

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

void Car::Display() const
    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);
   for(i=list.begin(); i != list.end(); ++i) 
   cout << endl;
   return 0;
8 Years
Discussion Span
Last Post by ArkM

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 {
    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
    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
    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' ?
    std::string owner;

class Bus::public Vehicle { // a bus IS A Vehicle
    explicit Bus(unsigned num = 0, unsigned n = 8):
        Vehicle(num), passengers(n)
    void Display() const {
        std::cout << "Bus: " << passengers << " seats"
            << " number " << getNum(); // << '\n' ?
    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.

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) {
    std::cout << '\n';

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.