-3
$\begingroup$

I need help overloading the comparison operators ( > , < ) so that I can compare Cards from my Card class. The card are made up of a rank that is of type integer and a Suit that is type Enum. The header file and implementation file are both pasted below. I need it to cout which card is greater than the other.

/********************************************************** File: card_header.h

Definition: Header file for the Card class

Author: Hunter Jarvis **********************************************************/

ifndef _CARD //macros

define _CARD

include

include

using namespace std;

enum Suit {Clubs, Diamonds, Hearts, Spades}; //ensures that only the 4 card suits are used to construct the cards

class Card //class definition { public:

Suit suit; //will hold the value that will determine the suit of the card

int rank; //integer value that will determine the rank of the card

Card(); //default constructor, will create a joker

// private: //keeps the set functions private so the user cannot tamper with them

void setSuit(Suit arg_suit); //sets the value of the suit for each card

void setRank(int arg_rank); //sets the value of the rank for each card

public: //the remainder of the class will be public so the user can make changes as they wish or as the programmer allows

Suit getSuit(); //returns the value of the choice for the card's Suit

int getRank(); //returns the value of the choice for the card's Rank

void toString(int arg_rank, Suit arg_suit); //will take in the choices for Suit and Rank and cout the card

};

endif //ends macro


/****************************************************** File: card.cpp

Definition: Implementation for the Card class

Author: Hunter Jarvis ******************************************************/

include

include "card_header.h" //header file 'card_header.h' holds the class header

include

using namespace std;

Card::Card() //default constuctor - sets the value of default card to rank 0 which generates a JOKER { rank = 0; }

void Card::setSuit(Suit arg_suit) //sets the value of the cards suit { suit = arg_suit; }

void Card::setRank(int arg_rank) //sets the value of the cards rank { rank = arg_rank; }

Suit Card::getSuit() //returns the value of the choice for Suit { return suit; }

int Card::getRank() //returns the value of the choice for Rank { return rank; }

void Card::toString(int arg_rank, Suit arg_suit) { if ( arg_rank = 0 ) cout << "Joker";

else if ( arg_rank = 1 ) cout << "Ace of ";

else if ( arg_rank = 2 ) cout << "2 of ";

else if ( arg_rank = 3 ) cout << "3 of ";

else if ( arg_rank = 4 ) cout << "4 of ";

else if ( arg_rank = 5 ) cout << "5 of ";

else if ( arg_rank = 6 ) cout << "6 of ";

else if ( arg_rank = 7 ) cout << "7 of ";

else if ( arg_rank = 8 ) cout << "8 of ";

else if ( arg_rank = 9 ) cout << "9 of ";

else if ( arg_rank = 10 ) cout << "10 of ";

else if ( arg_rank = 11 ) cout << "Jack of ";

else if ( arg_rank = 12 ) cout << "Queen of ";

else if ( arg_rank = 13 ) cout << "King of ";

if ( arg_suit == Clubs ) cout << "Clubs";

if ( arg_suit == Diamonds ) cout << "Diamonds";

if ( arg_suit == Hearts ) cout << "Hearts";

if ( arg_suit == Spades ) cout << "Spades"; }

$\endgroup$

closed as off-topic by fade2black, David Richerby, Evil, chi, adrianN Oct 12 '17 at 15:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – fade2black, David Richerby, chi, adrianN
If this question can be reworded to fit the rules in the help center, please edit the question.

-1
$\begingroup$

This site is used more for theoretical computer science questions, try not to ask implementation questions here, instead try stack overflow first.

With that being said, here is a link that will help you solve your problem. It is from TutorialsPoint and it shows you exactly how to override a boolean operator such as a '<' for objects, they specifically used a Distance object in this case.

https://www.tutorialspoint.com/cplusplus/relational_operators_overloading.htm

$\endgroup$
  • 2
    $\begingroup$ Try not to answer off topic questions, if possible. $\endgroup$ – user23422 Oct 12 '17 at 15:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.