SlideShare a Scribd company logo
1 of 7
Download to read offline
Question:
Problem 1: In a source file carddeck.cpp, provide an implementation for the Card class, i.e.,
please provide an implementation for all the member functions defined in the class. Observe that
there are two constructors: a default constructor to create the card representing Ace of Spades (or
AS) and a second constructor that defines a custom card with given rank and suit. The member
function toString() should return a two character string representation of a card, i.e. it would
return QH for Queen of Hearts, TD for Ten of Diamonds, 5S for Five of Spades, etc.
The objective of this assignment is to write classes to simulate a deck of cards. Recall that a deck
of cards consists of 52 cards consisting of the 13 ranks (2, 3, 4, 5, 6, 7, 8, 9, T(ten), J(jack),
Q(queen), K(kind), and A(ace) paired with the four suits CLUBS, DIAMONDS, HEARTS, and
SPADES. Consider the following header file carddeck.h:
#ifndef CARDDECK_H
#define CARDDECK
#include
using namespace std;
enum Suit
{
CLUBS, DIAMONDS, HEARTS, SPADES
};
class Card
{
private:
char rank; // use digit (e.g., 4) for cards valued 2 through 9, 'T' for 10,
// 'J','Q','K','A' for Jack, Queen, King, and Ace
Suit suit;
public:
Card(); // creates Ace of Spades as default card
Card(char,Suit); // creates card with given rank and suit
char getRank() const;
Suit getSuit() const;
void setRank(char);
void setSuit(Suit);
string toString() const; // creates text representation of card, e.g. "2C" for 2
// of Clubs, "KH" for King of Hearts, etc.
};
class CardDeck
{
private:
Card* deck;
Card* next_card; // keeps track of where we are in a deck after dealing some cards
//(initially, next_card == deck)
public:
CardDeck(); // sets member variable deck to be an array of 52 Card objects
// representing deck of cards
~CardDeck();
void shuffle(); // randomly shuffles the deck (you will need to think about how
// to do this). also, this function should reset next_card =
//deck
Card* dealHand(int); // deals a hand of N cards where N is the input to the
// function. this function should also increment the
// next_card pointer by N cards.
};
#endif
Solution
card.h
#ifndef CARD_H
#define CARD_H
#include
const int SUIT_MAX(4);
const int RANK_MAX(13);
class Card
{
friend class Deck; // Deck Class needs to access to Card Class but not vice versa
public:
explicit Card();
explicit Card(const int &suit, const int &rank);
std::string Card2Str() const;
private:
int generate_suit();
int generate_rank();
int get_suit() const;
int get_rank() const;
int m_suit;
int m_rank;
};
#endif
card.cpp
#include /* srand, rand */
#include "card.h"
#include
const std::string SUIT[SUIT_MAX] = {"S", "H", "D", "C"};
const std::string RANK[RANK_MAX] =
{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
Card::Card()
{
m_suit = generate_suit();
m_rank = generate_rank();
}
Card::Card(const int &suit, const int &rank) : m_suit(suit), m_rank(rank)
{
}
int Card::generate_suit()
{
return rand() % (SUIT_MAX-1) + 0;
}
int Card::generate_rank()
{
return rand() % (RANK_MAX-1) + 0;
}
std::string Card::Card2Str() const
{
return SUIT[get_suit()] + RANK[get_rank()];
}
int Card::get_suit() const
{
return m_suit;
}
int Card::get_rank() const
{
return m_rank;
}
deck.h
#ifndef DECK_H
#define DECK_H
#include
#include
#include
#include "card.h"
using namespace std;
class Deck
{
public:
explicit Deck();
void print_Deck() const;
void getOneCard();
private:
std::vector m_deck;
};
#endif
deck.cpp
#include
#include "deck.h"
Deck::Deck()
{
for (unsigned int i(0); i < SUIT_MAX; ++i)
{
for (unsigned int j(0); j < RANK_MAX; ++j)
{
Card card(i, j);
m_deck.push_back(card);
}
}
}
void Deck::print_Deck() const
{
unsigned int count(1);
for (unsigned int i(0); i < m_deck.size(); ++i)
{
std::cout << m_deck[i].Card2Str() << " ";
if ( count == 13 )
{
std::cout << std::endl;
count = 0;
}
++count;
}
}
void Deck::getOneCard()
{
Card cd(m_deck.back().get_suit(), m_deck.back().get_rank());
m_deck.pop_back();
std::cout << cd.Card2Str() << std::endl;
}
main.cpp
#include
#include
#include /* srand, rand */
#include /* time */
#include
#include "card.h"
#include "deck.h"
int main()
{
srand (time(NULL));
Deck _deck;
_deck.print_Deck();
_deck.getOneCard();
std::cout << std::endl;
_deck.print_Deck();
std::cout << std::endl;
return 0;
}

More Related Content

Similar to QuestionProblem 1 In a source file carddeck.cpp, provide an impl.pdf

Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdfHere is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
trishulinoverseas1
 
Goal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfGoal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdf
aaicommunication34
 
Need help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdfNeed help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdf
feelinggifts
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
princepavan
 

Similar to QuestionProblem 1 In a source file carddeck.cpp, provide an impl.pdf (7)

Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdfHere is the game description- Here is the sample game- Goal- Your goal (1).pdf
Here is the game description- Here is the sample game- Goal- Your goal (1).pdf
 
Goal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdfGoal- Your goal in this assignment is to write a Java program that sim.pdf
Goal- Your goal in this assignment is to write a Java program that sim.pdf
 
Need help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdfNeed help with questions 2-4. Write assembly code to find absolute v.pdf
Need help with questions 2-4. Write assembly code to find absolute v.pdf
 
Card Games in C++
Card Games in C++Card Games in C++
Card Games in C++
 
Debugging & Tuning in Spark
Debugging & Tuning in SparkDebugging & Tuning in Spark
Debugging & Tuning in Spark
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 

More from footstatus

Without using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdfWithout using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdf
footstatus
 
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdfwrite a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
footstatus
 
What is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdfWhat is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdf
footstatus
 
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdfa) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
footstatus
 

More from footstatus (20)

A storm warning siren emits sound uniformly in all directions. Along.pdf
A storm warning siren emits sound uniformly in all directions. Along.pdfA storm warning siren emits sound uniformly in all directions. Along.pdf
A storm warning siren emits sound uniformly in all directions. Along.pdf
 
Without using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdfWithout using cut, write a predicate split3 that splits a list of i.pdf
Without using cut, write a predicate split3 that splits a list of i.pdf
 
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdfwrite a MATLAB GUI program that implements an ultrasound image viewi.pdf
write a MATLAB GUI program that implements an ultrasound image viewi.pdf
 
With regards to eipdemiology, match the statements in the right colum.pdf
With regards to eipdemiology, match the statements in the right colum.pdfWith regards to eipdemiology, match the statements in the right colum.pdf
With regards to eipdemiology, match the statements in the right colum.pdf
 
Which of these are temperamental dimensionsoptionsAvoidant, re.pdf
Which of these are temperamental dimensionsoptionsAvoidant, re.pdfWhich of these are temperamental dimensionsoptionsAvoidant, re.pdf
Which of these are temperamental dimensionsoptionsAvoidant, re.pdf
 
Which of the following can cause OLS estimators to be biased Hetero.pdf
Which of the following can cause OLS estimators to be biased  Hetero.pdfWhich of the following can cause OLS estimators to be biased  Hetero.pdf
Which of the following can cause OLS estimators to be biased Hetero.pdf
 
what is the relationship between capsular type of H. influenza and p.pdf
what is the relationship between capsular type of H. influenza and p.pdfwhat is the relationship between capsular type of H. influenza and p.pdf
what is the relationship between capsular type of H. influenza and p.pdf
 
What is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdfWhat is the curve classification of ...I am working on the analysi.pdf
What is the curve classification of ...I am working on the analysi.pdf
 
Water is essential to all living organisms. Discuss THREE properties .pdf
Water is essential to all living organisms. Discuss THREE properties .pdfWater is essential to all living organisms. Discuss THREE properties .pdf
Water is essential to all living organisms. Discuss THREE properties .pdf
 
tree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdf
tree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdftree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdf
tree to answer question 2. n Arabidopsis arenosa Arabidopsis halleri .pdf
 
The plant pathogen Xylella fastidiosa is a gram negative that attach.pdf
The plant pathogen Xylella fastidiosa is a gram negative that attach.pdfThe plant pathogen Xylella fastidiosa is a gram negative that attach.pdf
The plant pathogen Xylella fastidiosa is a gram negative that attach.pdf
 
The region labeled with the letter B is transcribed into mRNA True.pdf
The region labeled with the letter B is transcribed into mRNA  True.pdfThe region labeled with the letter B is transcribed into mRNA  True.pdf
The region labeled with the letter B is transcribed into mRNA True.pdf
 
The positron decay of 15 o goes directly to the ground state of,15 N;.pdf
The positron decay of 15 o goes directly to the ground state of,15 N;.pdfThe positron decay of 15 o goes directly to the ground state of,15 N;.pdf
The positron decay of 15 o goes directly to the ground state of,15 N;.pdf
 
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdfa) What is the Chromosome Theory of Inheritance and how was it deter.pdf
a) What is the Chromosome Theory of Inheritance and how was it deter.pdf
 
provide a brief definition of self assemblySolutionMolecular s.pdf
provide a brief definition of self assemblySolutionMolecular s.pdfprovide a brief definition of self assemblySolutionMolecular s.pdf
provide a brief definition of self assemblySolutionMolecular s.pdf
 
Match the Statements below with bacteriological tests. Eosin-methyle.pdf
Match the Statements below with bacteriological tests.  Eosin-methyle.pdfMatch the Statements below with bacteriological tests.  Eosin-methyle.pdf
Match the Statements below with bacteriological tests. Eosin-methyle.pdf
 
Many non-scientists have focused their criticism of evolution on the.pdf
Many non-scientists have focused their criticism of evolution on the.pdfMany non-scientists have focused their criticism of evolution on the.pdf
Many non-scientists have focused their criticism of evolution on the.pdf
 
It has been observed that F_st increases with the distance between po.pdf
It has been observed that F_st increases with the distance between po.pdfIt has been observed that F_st increases with the distance between po.pdf
It has been observed that F_st increases with the distance between po.pdf
 
Let R be an integral domain and F a field such that R is a subring of.pdf
Let R be an integral domain and F a field such that R is a subring of.pdfLet R be an integral domain and F a field such that R is a subring of.pdf
Let R be an integral domain and F a field such that R is a subring of.pdf
 
Judith, Martha, and Sophia are organizing their backpacks. While org.pdf
Judith, Martha, and Sophia are organizing their backpacks. While org.pdfJudith, Martha, and Sophia are organizing their backpacks. While org.pdf
Judith, Martha, and Sophia are organizing their backpacks. While org.pdf
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 

QuestionProblem 1 In a source file carddeck.cpp, provide an impl.pdf

  • 1. Question: Problem 1: In a source file carddeck.cpp, provide an implementation for the Card class, i.e., please provide an implementation for all the member functions defined in the class. Observe that there are two constructors: a default constructor to create the card representing Ace of Spades (or AS) and a second constructor that defines a custom card with given rank and suit. The member function toString() should return a two character string representation of a card, i.e. it would return QH for Queen of Hearts, TD for Ten of Diamonds, 5S for Five of Spades, etc. The objective of this assignment is to write classes to simulate a deck of cards. Recall that a deck of cards consists of 52 cards consisting of the 13 ranks (2, 3, 4, 5, 6, 7, 8, 9, T(ten), J(jack), Q(queen), K(kind), and A(ace) paired with the four suits CLUBS, DIAMONDS, HEARTS, and SPADES. Consider the following header file carddeck.h: #ifndef CARDDECK_H #define CARDDECK #include using namespace std; enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }; class Card { private: char rank; // use digit (e.g., 4) for cards valued 2 through 9, 'T' for 10, // 'J','Q','K','A' for Jack, Queen, King, and Ace Suit suit; public: Card(); // creates Ace of Spades as default card Card(char,Suit); // creates card with given rank and suit char getRank() const; Suit getSuit() const; void setRank(char); void setSuit(Suit); string toString() const; // creates text representation of card, e.g. "2C" for 2 // of Clubs, "KH" for King of Hearts, etc. };
  • 2. class CardDeck { private: Card* deck; Card* next_card; // keeps track of where we are in a deck after dealing some cards //(initially, next_card == deck) public: CardDeck(); // sets member variable deck to be an array of 52 Card objects // representing deck of cards ~CardDeck(); void shuffle(); // randomly shuffles the deck (you will need to think about how // to do this). also, this function should reset next_card = //deck Card* dealHand(int); // deals a hand of N cards where N is the input to the // function. this function should also increment the // next_card pointer by N cards. }; #endif Solution card.h #ifndef CARD_H #define CARD_H #include const int SUIT_MAX(4); const int RANK_MAX(13); class Card { friend class Deck; // Deck Class needs to access to Card Class but not vice versa public: explicit Card(); explicit Card(const int &suit, const int &rank);
  • 3. std::string Card2Str() const; private: int generate_suit(); int generate_rank(); int get_suit() const; int get_rank() const; int m_suit; int m_rank; }; #endif card.cpp #include /* srand, rand */ #include "card.h" #include const std::string SUIT[SUIT_MAX] = {"S", "H", "D", "C"}; const std::string RANK[RANK_MAX] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; Card::Card() { m_suit = generate_suit(); m_rank = generate_rank(); } Card::Card(const int &suit, const int &rank) : m_suit(suit), m_rank(rank) { } int Card::generate_suit() {
  • 4. return rand() % (SUIT_MAX-1) + 0; } int Card::generate_rank() { return rand() % (RANK_MAX-1) + 0; } std::string Card::Card2Str() const { return SUIT[get_suit()] + RANK[get_rank()]; } int Card::get_suit() const { return m_suit; } int Card::get_rank() const { return m_rank; } deck.h #ifndef DECK_H #define DECK_H #include #include #include #include "card.h" using namespace std; class Deck { public:
  • 5. explicit Deck(); void print_Deck() const; void getOneCard(); private: std::vector m_deck; }; #endif deck.cpp #include #include "deck.h" Deck::Deck() { for (unsigned int i(0); i < SUIT_MAX; ++i) { for (unsigned int j(0); j < RANK_MAX; ++j) { Card card(i, j); m_deck.push_back(card); } } } void Deck::print_Deck() const { unsigned int count(1); for (unsigned int i(0); i < m_deck.size(); ++i) { std::cout << m_deck[i].Card2Str() << " "; if ( count == 13 ) {
  • 6. std::cout << std::endl; count = 0; } ++count; } } void Deck::getOneCard() { Card cd(m_deck.back().get_suit(), m_deck.back().get_rank()); m_deck.pop_back(); std::cout << cd.Card2Str() << std::endl; } main.cpp #include #include #include /* srand, rand */ #include /* time */ #include #include "card.h" #include "deck.h" int main() { srand (time(NULL)); Deck _deck; _deck.print_Deck(); _deck.getOneCard(); std::cout << std::endl; _deck.print_Deck(); std::cout << std::endl;