a) Write the .h file for a class Card, described below. Description of Card clas
ID: 3665663 • Letter: A
Question
a) Write the .h file for a class Card, described below.
Description of Card class:
Data members: a string suit to hold the suit of a card in a deck of playing cards
an integer face to hold the face of a card in a deck of playing cards
Function members: a default constructor
an explicit constructor which initializes the object to a Card with given suit and face. Receives: a suit and a face
an accessor(get operation) GetSuit() returns the card's suit value
an accessor(get operation) GetFace() returns the card's face value
a mutator(set operation) SetCard() assignes suit and face values from given suit and face values
overloaded < operator
receives another Card object C
returns: true if and only if this card C's face value is greater, otherwise false
friend functions
overloaded output operator
overloaded input operator
b) Write teh implementation file for the above Card class, with implementations for all of the above member functions and friend functions.
c) Write a driver to test all of the above functions as following:
Create two Card objects(cardOne, cardTwo), cardOne with default constructor and cardTwo with the explicit constructor
Output the above two Card objects
Re-set cardOne to a new card and output the new card's suit and face
Compare cardOne and cardTwo, and output the greater one
Create a third Card object cardThree with default constructor, then input new face and suit values to cardThree with overloaded input operator and display it with overloaded output operator
Explanation / Answer
main.cpp
#include "deck.h"
using namespace std;
void playFlip()
{
int points = 0;
int count = 0;
deck d;
// shuffle three times
d.shuffle();
d.shuffle();
d.shuffle();
cout << "Press Enter to deal a card. Press any other key to stop." << endl;
while (cin.get() == ' ')
{
card c = d.deal();
int val = c.getValue();
Suit s = c.getSuit();
// display the card dealt
cout << c << endl;;
// 10 points for an Ace
if (val == 14)
{
points += 10;
// 1 extra point for a Heart
if (s == HEARTS)
{
points += 1;
}
}
// 5 points for King, Queen, or Jack
else if (val == 13 || val == 12 || val == 11)
{
points += 5;
// 1 extra point for a Heart
if (s == HEARTS)
{
points += 1;
}
}
// 0 points for 8, 9, or 10
else if (val == 8 || val == 9 || val == 10)
{
points += 0;
// 1 extra point for a Heart
if (s == HEARTS)
{
points += 1;
}
}
// lose half points for a 7
else if (val == 7)
{
points /= 2;
// 1 extra point for a Heart
if (s == HEARTS)
{
points += 1;
}
}
// lose all points for 2, 3, 4, 5, or 6
else if (val == 2 || val == 3 || val == 4 || val == 5 || val == 6)
{
points = 0;
// 1 extra point for a Heart
if (s == HEARTS)
{
points += 1;
}
}
// Output user score
cout << "Your score is: " << points << endl;
// check if deck is complete
count ++;
if (count == 52)
{
cout << "The entire deck has been played through." << endl;
break;
}
} // while
} // playFlip
int main()
{
try
{
// play the Flip game
playFlip();
}
// catch errors thrown
catch (rangeError &re)
{
cout << re.what() << endl;
}
catch (underflowError &ue)
{
cout << ue.what() << endl;
}
catch (overflowError &oe)
{
cout << oe.what() << endl;
}
} // main
deck.h
/* deck.h Header file for Project 2.
Contains deck class functions:
default constructor
overloaded '<<' operator that prints all the cards in a deck
*/
#include "card.h"
#include <algorithm>
#include "d_node.h"
using namespace std;
/* deck class */
class deck
{
public:
deck(); // default constructor
~deck(); // destructor
card deal(); // deal a card from the top
void replace(card toAdd); // replace card at the bottom of a deck
void shuffle(); // shuffle all cards in a deck
friend ostream &operator<<(ostream &ostr, const deck &d);
private:
node<card> *aCard;
};
/* default constructor for deck
creates a deck of cards [2 - Ace] and [Clubs - Spades]
*/
deck::deck()
{
node<card> *curr = NULL;
/* loops through each suit starting at Spades since we want spades to
be the last suit when we go through and print out the deck
*/
for (int i = 3; i >= 0; i--)
{
/* loops through each card value [2 - Ace]
*/
for (int val = 14; val > 1; val--)
{
/* creates a new node that contains a single card consisting
of a card value and a card suit
*/
aCard = new node<card> (card(val, static_cast<Suit>(i)), curr);
curr = aCard;
} // end for
} // end for
}
/* destructor for the deck
*/
deck::~deck()
{
node<card> *next = NULL;
while (aCard != NULL)
{
next = aCard->next;
delete aCard;
aCard = next;
}
}
/* deal from the top of the deck
*/
card deck::deal()
{
if (aCard != NULL)
{
node<card> *curr = aCard;
card val = curr->nodeValue;
aCard = aCard->next;
delete curr;
return val;
}
else
{
throw underflowError("ERROR: No cards to deal");
}
} // deal
/* replace a card on the bottom of the deck
*/
void deck::replace(card toAdd)
{
node<card> *curr = aCard;
node<card> *bottomCard;
int count = 0;
while (curr != NULL)
{
if (curr->next == NULL)
{
bottomCard = curr;
} // if
curr = curr->next;
count++;
} // while
// check deck size
if (count >= 52)
{
throw overflowError("ERROR: Deck is already full");
}
// add the card to the bottom of the deck
curr = new node<card>(toAdd, NULL);
bottomCard->next = curr;
} // replace
/* shuffles the deck
*/
void deck::shuffle()
{
vector<card> cardVect;
node<card> *cards = aCard;
// transfer cards to a vector
while (cards != NULL)
{
cardVect.push_back(cards->nodeValue);
cards = cards->next;
} // for
// shuffle all elements in the vector
random_shuffle(cardVect.begin(), cardVect.end());
// transfer cards back to a list
int i = 0;
node<card> *top = aCard;
while (aCard != NULL)
{
aCard->nodeValue = cardVect[i];
aCard = aCard->next;
i++;
}
aCard = top;
} // shuffle
/* Overload the '<<' operator for deck objects */
ostream &operator<<(ostream &ostr, const deck &d)
{
node<card> *card = d.aCard;
// while the card doesn't point to NULL, we print the list
while (card != NULL)
{
ostr << card->nodeValue << endl;
card = card->next;
}
return ostr;
}
card.h
/* card.h Header file for Project 2.
Contains card class functions:
default constructor, constructor, getValue, setValue,
getSuit, setSuit
overloaded '<<' operator that prints the card value and suit
*/
#include "d_except.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/* Enumerated 'suit' data type
Needs too be global so deck can access
*/
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES };
/* card class */
class card
{
public:
card(); // default constructor
card(const card &other); // copy constructor
card(const int, const Suit); // constructor
int getValue() const { return value; } // returns card value
void setValue(int); // sets card value
Suit getSuit() const { return suit; } // returns card suit
void setSuit(const Suit); // sets card suit
friend ostream &operator<<(ostream &out, const card &c);
card &operator=(const card &cd);
private:
int value;
Suit suit;
};
/* default card Constructor */
card::card()
{}
/* copy constructor */
card::card(const card &other)
: value(other.value), suit(other.suit)
{}
/* card Constructor */
card::card(const int val, const Suit s)
: value(val), suit(s)
{
// check inputs
if (val < 2 || val > 14)
{
throw rangeError("ERROR: Invalid card value");
}
if (s < 0 || s > 3)
{
throw rangeError("ERROR: Invalid suit name");
}
}
/* Sets card value */
void card::setValue(int val)
{
// check input
if (val < 2 || val > 14)
{
throw rangeError("ERROR: Invalid card value");
}
value = val;
} // end setValue
/* Sets suit */
void card::setSuit(const Suit s)
{
// check input
if (s < 0 || s > 3)
{
throw rangeError("ERROR: Invalid suit name");
}
suit = s;
} // end setSuit
// strings that we will call to output the card values
// string of suits
const string suitStr[] = { "Clubs", "Diamonds", "Hearts", "Spades" };
// string of face cards
const string faceCard[] = { "Jack", "Queen", "King", "Ace" };
/* Overload the '<<' operator for card objects */
ostream &operator<<(ostream &ostr, const card &c)
{
int cardVal = c.getValue();
// if the card is less than 11 it is a number and we can output it
if (cardVal < 11)
{
ostr << cardVal << " of " << suitStr[c.getSuit()];
}
// if the card is greater than 11 it is a face card and we must
// subtract 11 from that number so we can find the face card name
else
{
ostr << faceCard[cardVal - 11] << " of " << suitStr[c.getSuit()];
}
return ostr;
}
/* Overload the '=' operator to the card class */
card &card::operator=(const card &cd)
{
value = cd.value;
suit = cd.suit;
return *this;
}
d_except.h
#ifndef EXCEPTION_CLASSES
#define EXCEPTION_CLASSES
#include <strstream>
#include <string>
using namespace std;
class baseException
{
public:
baseException(const string& str = ""):
msgString(str)
{
if (msgString == "")
msgString = "Unspecified exception";
}
string what() const
{
return msgString;
}
// protected allows a derived class to access msgString.
// chapter 13 discusses protected in detail
protected:
string msgString;
};
// failure to allocate memory (new() returns NULL)
class memoryAllocationError: public baseException
{
public:
memoryAllocationError(const string& msg = ""):
baseException(msg)
{}
};
// function argument out of proper range
class rangeError: public baseException
{
public:
rangeError(const string& msg = ""):
baseException(msg)
{}
};
// index out of range
class indexRangeError: public baseException
{
public:
indexRangeError(const string& msg, int i, int size):
baseException()
{
char indexString[80];
ostrstream indexErr(indexString, 80);
indexErr << msg << " index " << i << " size = " << size << ends;
// indexRangeError can modify msgString, since it is in
// the protected section of baseException
msgString = indexString;
}
};
// attempt to erase from an empty container
class underflowError: public baseException
{
public:
underflowError(const string& msg = ""):
baseException(msg)
{}
};
// attempt to insert into a full container
class overflowError: public baseException
{
public:
overflowError(const string& msg = ""):
baseException(msg)
{}
};
// error in expression evaluation
class expressionError: public baseException
{
public:
expressionError(const string& msg = ""):
baseException(msg)
{}
};
// bad object reference
class referenceError: public baseException
{
public:
referenceError(const string& msg = ""):
baseException(msg)
{}
};
// feature not implemented
class notImplementedError: public baseException
{
public:
notImplementedError(const string& msg = ""):
baseException(msg)
{}
};
// date errors
class dateError: public baseException
{
public:
dateError(const string& first, int v, const string& last):
baseException()
{
char dateStr[80];
ostrstream dateErr(dateStr, 80);
dateErr << first << ' ' << v << ' ' << last << ends;
// dateError can modify msgString, since it is in
// the protected section of baseException
msgString = dateStr;
}
};
// error in graph class
class graphError: public baseException
{
public:
graphError(const string& msg = ""):
baseException(msg)
{}
};
// file open error
class fileOpenError: public baseException
{
public:
fileOpenError(const string& fname):
baseException()
{
char errorStr[80];
ostrstream fileErr(errorStr, 80);
fileErr << "Cannot open "" << fname << """ << ends;
// fileOpenError can modify msgString, since it is in
// the protected section of baseException
msgString = errorStr;
}
};
// error in graph class
class fileError: public baseException
{
public:
fileError(const string& msg = ""):
baseException(msg)
{}
};
#endif // EXCEPTION_CLASSES
d_node.h
#ifndef NODE_CLASS
#define NODE_CLASS
#ifndef NULL
#include <cstddef>
#endif // NULL
// linked list node
template <typename T>
class node
{
public:
T nodeValue; // data held by the node
node<T> *next; // next node in the list
// default constructor with no initial value
node() : next(NULL)
{}
// constructor. initialize nodeValue and next
node(const T& item, node<T> *nextNode = NULL) :
nodeValue(item), next(nextNode)
{}
};
#endif // NODE_CLASS
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.