I have a parent class with a virtual function in it, I then make a child class and define the function. I then make a vector of vectors and insert one of the child classes into it. I then try to call the virtual function and nothing outputs to the screen. I do not know why this is happening, does anyone know?
Parent Class
class insect{
public:
string type;
int food_cost;
int armor;
int damage;
insect();
void set_food_cost(int x);
void set_armor(int x);
void set_damage(int x);
virtual void attack(){} // this is the problematic function
};
Child Class
class bee: public insect{
public:
bee();
int armor;
int damage;
void set_armor(int x);
void attack();
};
void bee::attack(){
cout << "im a bee, stab stab!\n";
}
Creating Vector of Vectors
vector< vector<insect> > insects_on_board(10);
Adding a bee to the vector of vectors
void add_bee(vector< vector<insect> > &insects_on_board, int &bees){
bees++;
insects_on_board[9].push_back(bee());
}
Function Call
cout << "testing " << insects_on_board.at(9).at(0).type << endl;
insects_on_board.at(9).at(0).attack();
Output
testing B
My Question Again
so in the output im expecting to see "testing B" and then "im a bee, stab stab!"
but only the "testing B" is outputted to the screen, any ideas why the other part is not?