I'm very new to C++ and would really appreciate any and all help. I have this assignment that plays around with classes and inheritance. My understanding is that I can write a virtual function in the base class that gets overridden by functions of the same name in inherited classes. When I call any of the inherited classes this works just fine, but when I plug them into a function written to take in the base class, even when the objects it receives are from inherited classes, it calls initializers from the inherited class, but other functions only from the base class. Here is a shortened version of my code:
base class:
#ifndef PLAYER_CPP
#define PLAYER_CPP
class Player
{
protected:
string playerThrow;
public:
//function that sets player throw
void virtual setMove();
string performMove() {return(playerThrow);}
};
#endif
inherited class:
class Avalanche: virtual public Player
{
public:
Avalanche();
//set specific move for class
void setMove()
{
//always plays rock
playerThrow = "rock";
}
};
//initializer, inherit from player, set new name
Avalanche::Avalanche() : Player()
{
//initialize name string
string newName;
//set name
newName = "Avalanche Player";
//sets name in player class
name = newName;
}
How I'm trying to use it:
class Tournament
{
public:
Tournament();
Player bout(Player, Player);
Player Tournament::bout (Player p1, Player p2)
{
p1.setMove();
p2.setMove();
return p1;
}
};
what this winds up doing is setting the move to nothing, rather than to "rock".
Thanks in advance for any pointers in the right direction. This one's got me stumped.
-Victoria