cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : General C++ Programming : Declaring an array of a class
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  Declaring an array of a class

VenomousNinja (42)
Hey. I'm coding a text card game using classes 'card' and 'deck'. Now, in order for 'deck' to compile and function like a real life deck of cards, it needs to declare 52 different cards.
I was wondering how I would do this. Here's my card class.
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
//Card.h - A reusable card class
namespace cardgame
{
	class card
	{
	protected:
		enum face
		{
			ace, two, three, four, five, six, seven, eight, nine, ten, Jack, Queen, King, Joker
		} value;

		enum suit
		{
			spade, diamond, club, heart
		}type;

		enum location
		{
			inhand, indeck,
		}loc;

		bool faceup;

	public:
		card(card::face val, card::suit tpe, card::location startloc, bool up = false) //Constructor A: The Full Constructor
		{
			value = val;
			type = tpe;
			loc = startloc;
			faceup = up;
		};

		card(face val, card::suit tpe, bool up = false) //Constructor B: The 'indeck' Constructor
		{
			value = val;
			type = tpe;
			loc = indeck;
			faceup = up;
		};

		bool flip(card)
		{
			if(faceup == true)
			{
				faceup = false;
			}
			else
			{
				faceup = true;
			}
			return faceup;
		};

	};
	
}




|
firedraco (847)
Just create it like you would any other array, i.e.:

TYPE[52] NAME;
|
Bazzy (539)
I think that you should add a constructor with no arguments or with only optional arguments
|
seymore15074 (114)
Additionally, you could default the location in the constructor like you defaulted up. Then you would only have one constructor, so far.

Here's another improvement that could save you some lines: replace lines 43-50 with: faceup = !faceup;
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us