My base class:
class Item
{
protected:
int count;
string model_name;
int item_number;
public:
Item();
void input();
}
My derived Class:
class Bed : public Item
{
private:
string frame;
string frameColour;
string mattress;
public:
Bed();
void input();
}
for now all my input function is trying to do is output which method is being used:
void Item::input()
{
cout<<"Item input"<<endl;
}
void Bed::input()
{
cout<<" Bed Input"<<endl;
}
when I call the function in main I’d like the derived class input to be used but at present the item input is.
Main:
vector<Item> v;
Item* item;
item= new Bed;
v.push_back(*item);
v[count].input();
count++;
I have followed the method laid out in a book I have but I think i may be confused about how to create new objects stored in the vector.
Any help would be great,
Thanks
Hx
You haven’t marked your method as
virtual.Also, because you have a
vectorof objects, not pointers, you’ll run into object slicing. Although it will compile, it isn’t correct.The proper way is having a vector of pointers or smart pointers.