classes

Hey Guys ! i have a question related to classes ..
s played using the standard French deck with 52 cards. Each card is from one of the 4 suits, clubs (♣), diamonds (♦), hearts (♥) and spades (♠). Each suit includes 13 cards which are ranked as A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3 and 2.
Make a class called Card. Each card is represented by a suit and a rank. You can keep both as strings. Implement getters and setters for the Card class as well as a print function. A 7 of spades should be printed as 7S or 7♠.
Create a class Deck which contains 52 cards.

I am unable to understand to understand the last line in the question ..
So far i have written this much code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include<string>
using namespace std;
class Card 
{
public :
	string suit ;
	string rank ;


	
	void Setsuit(string Suit)
	{
		suit = Suit ;
	}
	string GetSuit ()
	{
		return suit ;
	}

	void SetRank( string Rank)
	{
		rank = Rank ;
	}

	string GetRank ()
	{
		return rank ;
	}
	/*void print()
	{
		
	}*/
};

#include <iostream>
#include <iomanip>
#include <string>
#include"q1.h"
using namespace std;

class Deck
{
public :
	Deck()
	{
		string Card[52];
	}	

	void Print_Cards_in_Deck()
	{
		for (int i = 0 ; i<52; i++ )
		{
			if (i==0 || i<13)
			{
				
				cout << setw(2);
				Card[i] = '\x03' ;
				cout << Card[i] ;
				cout << endl;
			}
			else if (i>=13 && i<26)
			{
				cout << setw(2);
			        Card[i] = '\x04' ;
				cout << Card[i] ;
				cout << endl;
			}
			
			else if (i>26 && i<=39)
			{
				cout << setw(2);
				Card[i] = '\x05' ;
                        	cout << Card[i] << endl;
			}
			else if (i>=38 && i<=52)
			{
				cout << setw(2);
				Card[i] = '\x06' ;
				cout << Card[i] << endl;
			}enter code here

		}
};

How do i make a deck of 52 cards ? thats the major issue .. please somenone help me with this ..
Your deck should contain 52 Card objects. Each Card object must be initialized. So, the loop in your Print... function can be used in the Deck constructor to initialize the Card objects.

Incidentally, line 47 creates a local array of 52 string objects named Card, not an array of Card objects, that disappears immediately when the constructor completes operation.
Last edited on
can u please tell the syntax ?
Card card[52]; will create an array of 52 cards named card[0], card[1], ... card[51].

In your constructor you can do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int index = 0;
for (int suit = 0 ; suit<4; suit++ )
{
	/* determine suitString */

	for (rank = 0; rank < 13; rank++)
	{	
		/* determine rankString */

 		card[index].setSuit(suitString);
 		card[index].setRank(rankString);

		index++;
	}
}
^^^^

Why don't you use the counter in you for loops instead of creating an extra one?

1
2
3
4
5
6
7
8
9
10
11
12
for (int suit = 0 ; suit<4; suit++ )
{
	/* determine suitString */

	for (rank = 0; rank < 13; rank++)
	{	
		/* determine rankString */

 		card[rank].setSuit(suitString);
 		card[rank].setRank(rankString);
         }
}
Why don't you use the counter in you for loops instead of creating an extra one?
Line 15: maximum rank is 12. Maximum index is 51... correct way will be (suit*13 + rank), but with third variable loop will execute faster than with all this multiplications and additions.

Why don't you use the counter in you for loops instead of creating an extra one?

...



The problem with this is that the loop counter for both loops is necessary to index into the whole array of Cards. Because rank ranges from 0 - 12 4 times, the suggested code causes each of the 1st 13 cards gets modified 4 times each, but Cards 13 - 51 are never modified.

There are a number of ways to do it. The way I did it seems like the most intuitive to me. You could also:

- use the loops as is without the count and calculate the index with (suit * 13) + rank

- loop (i = 0; i < 52; i++) and then calculate the suit (i / 13) and the rank (i % 13).

- create a 2-dimensional array Card card[4][13]; and access card[suit][rank]

- cast the 1-dimensional array of Cards as a 2-dimensional array of cards and access card[suit][rank].

Last edited on
Topic archived. No new replies allowed.