< Previous | Contents | Next >
The following function definition overloads the << operator so I can send a Card
object to the standard output.
378 Chapter 10 n Inheritance and Polymorphism: Blackjack
//overloads << operator so Card object can be sent to cout ostream& operator<<(ostream& os, const Card& aCard)
{
const string RANKS[] = {"0", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
const string SUITS[] = {"c", "d", "h", "s"};
if (aCard.m_IsFaceUp)
{
os << RANKS[aCard.m_Rank] << SUITS[aCard.m_Suit];
}
else
{
os << "XX";
}
return os;
}
The function uses the rank and suit values of the object as array indices. I begin the array RANKS with "0" to compensate for the fact that the value for the rank enumeration defined in Card begins at 1.
The last function definition overloads the << operator so I can send a GenericPlayer
object to the standard output.
//overloads << operator so a GenericPlayer object can be sent to cout ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer)
{
os << aGenericPlayer.m_Name << ":\t";
vector<Card*>::const_iterator pCard; if (!aGenericPlayer.m_Cards.empty())
{
for (pCard = aGenericPlayer.m_Cards.begin(); pCard != aGenericPlayer.m_Cards.end();
++pCard)
{
os << *(*pCard) << "\t";
}
if (aGenericPlayer.GetTotal() != 0)
{
}
}
else
{
cout << "(" << aGenericPlayer.GetTotal() << ")";
os << "<empty>";
}
return os;
}
The function displays the generic player’s name and cards, along with the total value of the cards.