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;
}