Modify suit_test.cpp so that the program prints out all 52 cards of a deck. Modi
ID: 3819103 • Letter: M
Question
Modify suit_test.cpp so that the program prints out all 52 cards of a deck. Modify card.cpp so that it prints the rank of a card as 'Ace', 'Two', 'Three', ....
/////////////////////suit-test.cpp/////////////
#include "card.h"
int main(){
Card c1;
Card c2( diamond, 8 );
cout << "Card one ";
cout << c1.rank << endl;
cout << "Card two ";
cout << c2.rank << endl;
return 0;
}
///////////////////card.h///////////////
#ifndef CARD_H
#define CARD_H
#include
using namespace std;
enum suits { diamond, club, heart, spade};
class Card {
public:
//constructors
Card();
Card( suits, int );
//data fields
int rank; //rank of card
suits suit; //suit of card
};
ostream & operator << ( ostream &out, Card c );
#endif
//////card.cpp///////////////////
#include "card.h"
Card::Card()
//default
{
rank = 1;
suit = spade;
}
Card::Card( suits s, int r )
{
rank = r;
suit = s;
}
ostream & operator << ( ostream &out, Card c )
{
//first output rank
switch ( c.rank ) {
case 1: out << "Ace"; break;
case 11: out << "Jack"; break;
case 12: out << "Queen"; break;
case 13: out << "King"; break;
default:
out << c.rank; break;
}
//then output suit
switch( c.suit ) {
case diamond: out << " of Diamonds"; break;
case spade: out << " of Spadess"; break;
case heart: out << " of Heartss"; break;
case club: out << " of Clubs"; break;
}
return out;
}
Explanation / Answer
Below Is your solution. I have added comments as well as made the code which has been changed to bold to help you understand it better: -
#include "card.h"
int main(){
Card c1;
Card c2( diamond, 8 );
cout << "Card one ";
cout << c1 << endl;// Using operator overloading printing information of the card
cout << "Card two ";
cout << c2 << endl;
cout<<endl;
cout<<"Printing all the cards..."<<endl;
cout<<endl;
Card cArray[4][13];
//Adding all the cards in the array
Card* c; //Declaration of pointer of Card object
for(int i =0;i<4;i++) {
for(int j=0;j<13;j++){
if(i == 0) { //Instantiation of class object with mutiple suit and ranks
c = new Card(diamond,j+1);
} else if(i == 1) {
c = new Card(club,j+1);
} else if(i == 2) {
c = new Card(heart,j+1);
} else if(i == 3) {
c = new Card(spade,j+1);
}
cArray[i][j] = *c; //adding value at the pointer to array i.e. card object
}
}
for(int i =0;i<4;i++) {
for(int j=0;j<13;j++){
cout << cArray[i][j] << endl; //Iterating card array and printing each card
}
cout<<endl;
}
return 0;
}
#ifndef CARD_H
#define CARD_H
#include <iostream>
using namespace std;
enum suits { diamond, club, heart, spade};
class Card {
public:
//constructors
Card();
Card( suits, int );
//data fields
int rank; //rank of card
suits suit; //suit of card
};
ostream & operator << ( ostream &out, Card c );
#endif
#include "card.h"
Card::Card()
//default
{
rank = 1;
suit = spade;
}
Card::Card( suits s, int r )
{
rank = r;
suit = s;
}
ostream & operator << ( ostream &out, Card c )
{
//first output rank
switch ( c.rank ) { // Adding all types of cards
case 1: out << "Ace"; break;
case 2: out << "Two"; break;
case 3: out << "Three"; break;
case 4: out << "Four"; break;
case 5: out << "Five"; break;
case 6: out << "Six"; break;
case 7: out << "Seven"; break;
case 8: out << "Eight"; break;
case 9: out << "Nine"; break;
case 10: out << "Ten"; break;
case 11: out << "Jack"; break;
case 12: out << "Queen"; break;
case 13: out << "King"; break;
default:
out << c.rank; break;
}
//then output suit
switch( c.suit ) {
case diamond: out << " of Diamonds"; break;
case spade: out << " of Spadess"; break;
case heart: out << " of Heartss"; break;
case club: out << " of Clubs"; break;
}
return out;
}
Output: -
Card one
Ace of Spadess
Card two
Eight of Diamonds
Printing all the cards...
Ace of Diamonds
Two of Diamonds
Three of Diamonds
Four of Diamonds
Five of Diamonds
Six of Diamonds
Seven of Diamonds
Eight of Diamonds
Nine of Diamonds
Ten of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Clubs
Two of Clubs
Three of Clubs
Four of Clubs
Five of Clubs
Six of Clubs
Seven of Clubs
Eight of Clubs
Nine of Clubs
Ten of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Heartss
Two of Heartss
Three of Heartss
Four of Heartss
Five of Heartss
Six of Heartss
Seven of Heartss
Eight of Heartss
Nine of Heartss
Ten of Heartss
Jack of Heartss
Queen of Heartss
King of Heartss
Ace of Spadess
Two of Spadess
Three of Spadess
Four of Spadess
Five of Spadess
Six of Spadess
Seven of Spadess
Eight of Spadess
Nine of Spadess
Ten of Spadess
Jack of Spadess
Queen of Spadess
King of Spadess
--------------------------------
Process exited after 0.1572 seconds with return value 0
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.