fork download
  1. #include <iostream>
  2.  
  3. // Card class struct
  4. struct Card
  5. {
  6. enum RANK{ ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING };
  7. enum SUIT{ CLUBS, DIAMONDS, HEARTS, SPADES };
  8.  
  9. Card(RANK rank = ACE, SUIT suit = SPADES, bool isFaceUp = true);
  10.  
  11. friend std::ostream& operator<<(std::ostream&, const Card&);
  12.  
  13. private:
  14.  
  15. static const char* ranks[13];
  16. static const char* suits[4];
  17.  
  18. RANK rank;
  19. SUIT suit;
  20. bool isFaceUp;
  21. };
  22.  
  23. const char* Card::ranks [] =
  24. {
  25. "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
  26. "Eight", "Nine", "Ten", "Jack", "Queen", "King"
  27. };
  28.  
  29. const char* Card::suits [] = { "Clubs", "Diamonds", "Hearts", "Spades" };
  30.  
  31. // Card ctor
  32. Card::Card(RANK rank, SUIT suit, bool isFaceUp) : rank(rank), suit(suit), isFaceUp(isFaceUp) {}
  33.  
  34. std::ostream& operator<<(std::ostream& os, const Card& card)
  35. {
  36. return os << Card::ranks[card.rank - 1] << " of " << Card::suits[card.suit];
  37. }
  38.  
  39. int main()
  40. {
  41. Card c;
  42. Card d(Card::KING, Card::HEARTS);
  43.  
  44. std::cout << "c is the " << c << '\n';
  45. std::cout << "d is the " << d << '\n';
  46. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
c is the Ace of Spades
d is the King of Hearts