Operator Overload ( >, < )

I need help creating a function to overload 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.

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

Definition: Header file for the Card class

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

#ifndef _CARD //macros
#define _CARD

#include <iostream>
#include <cstdlib>

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 <iostream>
#include "card_header.h" //header file 'card_header.h' holds the class header
#include <cstdlib>

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";
}
Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <string>
#include <stdexcept>

struct Card
{
    enum Suit_t { Clubs, Diamonds, Hearts, Spades, None }; // joker == None

    Card() = default ;
    Card( Suit_t suit, int rank ) : suit(suit), rank(rank) { must_be_valid() ; }

    Suit_t get_suit() const { return suit ; }
    int get_rank() const { return rank ; }

    std::string to_string() const ;

    private:
        Suit_t suit = None ; // joker
        int rank = 0 ; // joker

        bool valid() const
        {
            if( rank == 0 ) return suit == None ;
            else return rank > 0 && rank < 14 ;
        }

        void must_be_valid() const
        {
            // assert( valid() ) ;
            if( !valid() ) throw std::logic_error( "invalid card state " ) ;
        }
};

std::string Card::to_string() const
{
    must_be_valid() ;

    static const std::string suit_name[] = { "Clubs", "Diamonds", "Hearts", "Spades" } ;

    if( rank > 1 && rank < 11 ) return std::to_string(rank) + " of " + suit_name[suit] ;

    switch(rank)
    {
        case 1 : return "Ace of " + suit_name[suit] ;
        case 11 : return "Jack of " + suit_name[suit] ;
        case 12 : return "Queen of " + suit_name[suit] ;
        case 13 : return "King of " + suit_name[suit] ;
    }

    return "Joker" ;
}

bool operator== ( Card a, Card b )
{ return a.get_rank() == b.get_rank() && a.get_suit() == b.get_suit() ; }

bool operator!= ( Card a, Card b ) { return !(a==b) ; }

bool operator< ( Card a, Card b )
{
    if( a.get_rank() == 0 ) return false ; // joker is not less than any other card

    // this assumes that Clubs < Diamonds < Hearts < Spades
    // if suits are the same, then the lower ranked card is less
    const int weight_a = a.get_suit() * 100 + a.get_rank() ;
    const int weight_b = b.get_suit() * 100 + b.get_rank() ;
    return weight_a < weight_b ;
}

bool operator> ( Card a, Card b ) { return b < a ; }
bool operator<= ( Card a, Card b ) { return a==b || a < b ; }
bool operator>= ( Card a, Card b ) { return a==b || a > b ; }

int main()
{
    const Card deck[] { {}, { Card::Hearts, 7 }, { Card::Hearts, 6 }, { Card::Diamonds, 7 } } ;

    for( Card a : deck ) for( Card b : deck )
    {
        std::string relop  = " > " ;
        if( a == b ) relop = " == " ;
        else if( a < b ) relop = " < " ;

        std::cout << a.to_string() << relop << b.to_string() << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/fa3389bed9bb6fcf
http://rextester.com/YMTZ56553
Topic archived. No new replies allowed.