I'm having problems getting a vector of objects to print the name of the derived class rather then the base class.
These are my classes
#include <vector>
#include <iostream>
using namespace std;
class Object
{
string name;
public:
string getName()
{
return name;
}
};
class Flashlight : public Object
{
string name = "Flashlight";
public:
string getName();
};
This is my main, it has a vector of the objects and at this point just needs to print out their names.
int main()
{
vector<Object> items;
items.push_back(*new Flashlight);
for(int i = 0; i < items.size(); i++)
{
cout << i+1 << " " << items.at(i).getName();
}
}
Right now if I assign something to the name in Object it will print that but its not using the derived classes values, I just want it to inherit the function then use its own values with it. I've tried implementing the function in the base classes(but seeing as I could have a lot of those in future it would lead to lots of redundant code) but that doesn't work either.