Challenge 22b

Description

The purpose of this challenge is to define overloaded operators for a custom class. This challenge simulates playing cards.

 Requirements

  1. Define a class called Card as below. Complete the as_string() function and the overloaded operators as described in the comments below.
    class Card
    {
      private: 
        // suit contains values 1-4, to represent suits below
        // 1 - Diamonds, 2 - Hearts, 3 - Clubs, 4 - Spades
        int suit; 
        // value contains values 1 - 13
        // Ace - 1, 2-10, J - 11, Q - 12, K - 13
        int value;
      public:
        // you will need to write:
        //  - an overloaded constructor to set suit and value
    
        // function to return a string that describes the card
        string as_string()
        {
          // for example, if suit == 1 and value == 1
          // this function would return "Ace of Diamonds"
        }  
    
        // write an overloaded operator for less-than-or-equal-to 
        // this function allows you to compare two cards. 
        // despite an Ace having a value of 1, it is 
        // actually greater in value than all the other cards
        // make sure your overload for less-than-or-equal-to takes
        // this into account
    
        // write an overloaded operator for << that will allow
        // you to cout a Card object. This overload will call the 
        // as_string() function to display its description
    };

Sample main()

// class definitions here

// Use this main() function
int main()
{
  Card test1(1,1);   // Ace of Diamonds
  Card test2(1,10);  // 10 of Diamonds

  cout << test1 << endl;    // should show "Ace of Diamonds"
  
  if (test2 <= test1)
  {
    // should show "Ace of Diamonds beats 10 of diamonds"
    cout << test1 << " beats " << test2 << endl;
  }
  else
  {
    cout << test2 << " beats " << test1 << endl;
  }
}

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0022b

Print Requirements