SlideShare a Scribd company logo
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
 
Card Games in C++
Card Games in C++Card Games in C++
Card Games in C++
Programming Homework Help
 
Debugging & Tuning in Spark
Debugging & Tuning in SparkDebugging & Tuning in Spark
Debugging & Tuning in Spark
Shiao-An Yuan
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 

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

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
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
 
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
footstatus
 
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
footstatus
 
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
footstatus
 
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
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
 
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
footstatus
 
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
footstatus
 
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
footstatus
 
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
footstatus
 
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
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
 
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
footstatus
 
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
footstatus
 
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
footstatus
 
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
footstatus
 
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
footstatus
 
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
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 Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 

Recently uploaded (20)

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 

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;