How to Represent the Deck of Cards Using Array in C++?
Last Updated :
06 Jun, 2024
A deck of cards is a collection of 52 playing cards in which each card is uniquely identified by its suit and rank, there are four suits: heart, diamond, club and spade each having 13 ranks: Ace, 2 to 10, jack, queen, and king. In this article, we will learn how to represent a deck of cards using a C++ array.
Representing Deck of Cards in C++ Arrays
In C++, we can make the use of classes to represent a deck of cards as it provides a structured and efficient way to represent a deck of cards. This arrangement allows for easy access, manipulation, shuffling, and allotment of cards to players, as the deck internally uses array the user defined data type card.
Approach
- Define a Card class with suit and rank as members.
- Create a Deck class to manage the deck of cards. Inside it, we create an array of cards to store the deck of card.
- Create and Initialize the suits and ranks arrays with the names of the suits and ranks.
- Initialize the deck of cards in a nested loop.
- For each combination of suit and rank, create a card and add it to the deck.
- Finally, using a loop, print each card in the deck.
C++ Program that Represents a Deck of Cards
Below is the C++ program that represents a deck of cards using an array
C++
// C++ program for representing the deck of cards in arrays
#include <iostream>
#include <string>
using namespace std;
// class representing a card
class Card {
private:
string suit;
string rank;
public:
// Default constructor
Card()
: suit("")
, rank("")
{
}
// Parameterized constructor
Card(string s, string r)
: suit(s)
, rank(r)
{
}
// Setter for suit
void setSuit(string s) { suit = s; }
// Setter for rank
void setRank(string r) { rank = r; }
// Getter for suit
string getSuit() const { return suit; }
// Getter for rank
string getRank() const { return rank; }
// Method to print card details
void printCard() const
{
cout << suit << " " << rank;
}
};
// class representing a deck of cards
class Deck {
private:
Card deck[52];
// data for initialization
string suits[4]
= { "hearts", "diamonds", "clubs", "spades" };
string ranks[13]
= { "Ace", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "Jack", "Queen", "King" };
public:
// Constructor to initialize and populate the deck
Deck()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
deck[i * 13 + j].setSuit(suits[i]);
deck[i * 13 + j].setRank(ranks[j]);
}
}
}
// Method to print the entire deck
void printDeck() const
{
int suitChange = 0;
for (int i = 0; i < 52; i++) {
deck[i].printCard();
suitChange++;
if (suitChange < 13)
cout << " | ";
else {
suitChange = 0;
cout << endl;
}
}
}
};
// Driver code
int main()
{
Deck deck;
// Print the deck of cards
deck.printDeck();
return 0;
}
Output
hearts Ace | hearts 2 | hearts 3 | hearts 4 | hearts 5 | hearts 6 | hearts 7 | hearts 8 | hearts 9 | hearts 10 | hearts Jack | hearts Queen | hearts King
diamonds Ace | diamonds 2 | diamonds 3 | diamonds 4 | diamonds 5 | diamonds 6 | diamonds 7 | diamonds 8 | diamonds 9 | diamonds 10 | diamonds Jack | diamonds Queen | diamonds King
clubs Ace | clubs 2 | clubs 3 | clubs 4 | clubs 5 | clubs 6 | clubs 7 | clubs 8 | clubs 9 | clubs 10 | clubs Jack | clubs Queen | clubs King
spades Ace | spades 2 | spades 3 | spades 4 | spades 5 | spades 6 | spades 7 | spades 8 | spades 9 | spades 10 | spades Jack | spades Queen | spades King
Time Complexity: O(1), It is constant because the deck size is fixed (52).
Auxiliary Space: O(1)
Similar Reads
How to Create a Set of Arrays in C++? In C++, the set container represents a collection of unique, sorted elements, and an array is a collection of items stored at contiguous memory locations. In this article, we will learn about how to create a set of arrays in C++. Set of Arrays in C++A set of arrays refers to a collection of arrays w
2 min read
How to Calculate the Size of a Static Array in C++? In C++, static arrays are the type of arrays whose size is fixed, and memory for them is allocated during the compile time. In this article, we will learn how to calculate the size of a static array in C++. Example: Input:myArray = {1, 2, 3, 4, 5}Output:The size of the array is 5.Size of a Static Ar
2 min read
How to Create an Array of Structs in C++? In C++, a struct is a user-defined data type that allows us to combine data of different types and an array of structs is an array in which each element is of the struct type. In this article, we will learn how to create an array of structs in C++. Creating an Array of Structs in C++To create an arr
2 min read
How to Create a Set of Vectors in C++? In C++, a set is a type of associative container in which duplicate elements are not allowed and a vector is a dynamic array in which duplicate elements are allowed. In this article, we will learn how to create a set of vectors in C++. For Example, Input:vector<int> vec1={1, 2, 3};vector<in
2 min read
How to Create a Stack of Arrays in C++? In C++, the std::stack is a container that follows the LIFO (Last In, First Out) rule, whereas std::array is a sequence container that stores elements in contiguous memory. In this article, we will learn how to create a stack of an array in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; ar
2 min read