Write the member function implementations for the class hand, which simulates a
ID: 3776576 • Letter: W
Question
Write the member function implementations for the class hand, which simulates a hand of 2 cards, into the file hand.cpp. The relative strength of 2 hands are determined by the following rules: A pair (two cards of the same number) is the strongest hand. Two cards of the same suit is the next strongest hand. Two cards of different numbers and suits is the weakest hand. Within the same kind of hands, the stronger hand is determined by the larger number. If two hands are of the same kind and largest numbers are the same, the stronger hand is determined by the smaller number. If all above fails, the two hands are of equal strength. I.e., all suits are of equal strength. 2 is the weakest number. An Acc is stronger than a King. You are using a single deck of cards. So a hand of A A is impossible. Here are some examples: 3 3 is stronger than 9 2. 9 2 is stronger than 8 7. 8 7 is stronger than 8 6. 8 6 is stronger than A J. 3 3 has the same strength as 3 3. The public data members card c1, c2; represent the 2 cards of a hand. the public member function bool operator (hand rhs); bool operator=(hand rhs); are defined similarly.Explanation / Answer
PROGRAM CODE:
#include <iostream>
using namespace std;
//No implementation for the class card is provided. hence Im simulating on my own.
class card
{
public:
int number;
char type;
//numbers : 2 to 10 then Jack = 11, Queen = 12, King = 13 and A = 1
//type : Diamond = 'D', Clover = 'C', Heart = 'H', Spades = 'S'
card()
{
}
card(int num, char cardtype)
{
number = num;
type = cardtype;
}
};
class hand
{
public:
card c1, c2;
bool strongHand = false;
bool operator<(hand rhs)
{
bool islhsSameNumber = this->c1.number == this->c2.number;
bool isrhsSameNumber = rhs.c1.number == rhs.c2.number;
bool islhsSameType = this->c1.type == this->c2.type;
bool isrhsSameType = rhs.c1.type == rhs.c2.type;
if(isrhsSameNumber && isrhsSameType)
{
if(!islhsSameNumber || !islhsSameType)
strongHand = true;
else
{
if(rhs.c1.number > this->c1.number)
strongHand = true;
}
}
else
{
int lhsLargest = this->c1.number > this->c2.number? this->c1.number : this->c2.number;
int rhsLargest = rhs.c1.number > rhs.c2.number? rhs.c1.number : rhs.c2.number;
int lhsSmallest = this->c1.number < this->c2.number? this->c1.number : this->c2.number;
int rhsSmallest = rhs.c1.number < rhs.c2.number? rhs.c1.number : rhs.c2.number;
if(rhsLargest > lhsLargest)
{
strongHand = true;
}
else if(rhsLargest == lhsLargest)
{
if(rhsSmallest > lhsSmallest)
strongHand = true;
}
}
return strongHand;
}
bool operator ==(hand rhs)
{
bool equalhand = false;
if((this->c1.number == this->c2.number) && (rhs.c1.number == rhs.c2.number))
{
equalhand = true;
}
return equalhand;
}
bool operator>(hand rhs)
{
if(!(this < rhs) && ! (this == rhs))
return true;
else return false;
}
bool operator<=(hand rhs)
{
if((this < rhs) || (this == rhs))
return true;
else return false;
}
bool operator>=(hand rhs)
{
if((this > rhs) || (this == rhs))
return true;
else return false;
}
};
int main() {
hand lhs, rhs;
lhs.c1 = card(3,'D');
lhs.c2 = card(3,'D');
rhs.c1 = card(3,'H');
rhs.c2 = card(3,'H');
bool lessthan = lhs<rhs;
bool greaterthan = lhs>rhs
bool lessthanorequal = lhs<=rhs;
bool greaterthanorequal = lhs>=rhs;
bool equalto = lhs==rhs;
cout<<"Less than: "<<lessthan<<endl;
cout<<"Greater than: "<<greaterthan<<endl;
cout<<"Less than or equal: "<<lessthanorequal<<endl;
cout<<"Greater than or equal: "<<greaterthanorequal<<endl;
cout<<"Equal: "<<equalto<<endl;
return 0;
}
OUTPUT:
Less than: 0
Greater than: 0
Less than or equal: 1
Greater than or equal: 1
Equal: 1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.