with the code displayed below i have created 4 classes(3 subclasses and one superclass), each of them having the same print_info() function. I have created pointer objects from each class and inserted them into a vector created with the superclass.
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
using namespace std;
class Vehicle
{
private:
int wheels, max_speed;
string model;
public:
virtual void print_info()
{
cout << "This is function print_info() in Vehicle (parent class)" << endl;
}
};
class Car: public Vehicle
{
private:
int doors, passengers;
public:
void print_info()
{
cout << "This is function print_info() in Car (child class)" << endl;
}
};
class Bike: public Vehicle
{
private:
string owner;
bool hasLights;
public:
string my_owner();
};
class Truck: public Vehicle
{
private:
float cargo_weight;
int allowed_speed;
public:
void print_info()
{
cout << "This is function print_info() in Truck (child class)" << endl;
}
};
int main()
{
Vehicle *v1 = new Vehicle();
Vehicle v2;
Car c1;
Car c2;
Bike b1;
Truck t1;
Truck t2;
vector <Vehicle> vvec;
Vehicle *v = new Vehicle();
Car *c = new Car();
Bike *b = new Bike();
Truck *t = new Truck();
vvec.push_back(*v);
vvec.push_back(*c);
vvec.push_back(*b);
vvec.push_back(*t);
vector<Vehicle>::iterator iter = vvec.begin();
while( iter != vvec.end())
{
iter->print_info();
iter++;
}
v1 = &v2;
v1->print_info();
v1 = &c2;
v1->print_info();
v1 = &t2;
v1->print_info();
system("pause");
return 0;
}
The wanted output is this:
This is function print_info in Vehicle (parent class)
This is function print_info in Car (child class)
This is function print_info in Vehicle (parent class)
This is function print_info in Truck (child class)
This is function print_info in Vehicle (parent class)
This is function print_info in Car (child class)
This is function print_info in Truck (child class)
Instead i am getting:
This is function print_info in Vehicle (parent class)
This is function print_info in Vehicle (parent class)
This is function print_info in Vehicle (parent class)
This is function print_info in Vehicle (parent class)
This is function print_info in Vehicle (parent class)
This is function print_info in Car (child class)
This is function print_info in Truck (child class)
I believe this is due to early binding but i don't know how to solve this!