Printing An array Class of Terms as Char & Int

I am working on building a deck of Cards and have a Class Card consisting of an integer for the card value 2-14 (Ace is high) and a character for the suite S, C, D, and H.

class Card
{
public:
Card();
Card(char S, int V);
friend ostream& operator<<(ostream& out, const Card & t) {
out << t.value << " of " << t.suite << endl;
return out;
}

private:
char suite;
int value;
};
--------
I build the deck by adding one Card to the Deck array. the deck size is defined as 52 using #define Size 52

Array::Deck()
{
int value_opt[] = { 2,3,4,5,6,7,8,9,10,11,12,13,14 };
char suite_opt[] = { 'S', 'C', 'H', 'D' };
Card*deck = new Card[Size];
for (int i = 0; i < Size; i++) {
deck[i] = Card(suite_opt[count / 13], value_opt[count % 13]);
}
}

-------
if I print deck[i] I will get the memory allocation for the values in that index. how do I pull value for the char and the value for the int separately from deck[i]. and can I push that to my main line of code?
You have made the suite and value (rank) private, so you need to provide some accessor method for introspection.

1
2
3
4
5
6
7
8
9
10
11
class Card
{
  public:
    ...
    char getSuite() const { return suite; }
    int getRank() const { return value; }

  private:
    char suite;
    int value;
};

You could even provide a way to return a card name as, say “2H” or even “2 of Hearts”.

1
2
3
4
5
6
7
8
    std::string toString() const
    {
      std::string result;
      if      (getRank()  <  9) result = (char)(getRank() + '0');
      else if (getRank() == 10) result = "10";
      else result = "JQKA"[ getRank() - 11 ];
      return result + getSuite();
    }

Printing each card then becomes very easy.

Hope this helps.
Topic archived. No new replies allowed.