Re: Constructing Objects of a Class
<kylemort@gmail.com> wrote in message
news:1173161011.375115.282750@v33g2000cwv.googlegroups.com...
I am currently using a class (Card) to represent a card in a deck of
cards. The class has a constructor:
Card::Card(int num, int su)
{
suit = su;
number = num;
}
and I am trying to create multiple objects of that class (52 to be
exact). As I am learning C++ (already know Java, Visual Basic and C#)
I am not sure how to instantiate an object of that class. I am trying
to use
deck = gcnew Card(plcmnt[i], ((int)plcmnt[i] / 13));
but I am running into problems. Any help is greatly appreciated
I don't know what gcnew is supposed to be. Or plcmt. Nor how you delared
deck.
The simplest, of course, is simply:
Card card( 1, 1 );
If you want to make it using new it would be:
Card* card = new card( 1, 1 );
Now, it seems you want a deck of cards, 52. Do you want a vector? Then
this would work:
std::vector<Card> Deck;
for ( int Suit = 0; Suit < 4; ++Suit )
for ( int Rank = 0; Rank < 13; ++ Rank )
Deck.push_back( Card( Rank, Suit ) );
Or do you want Deck to be soemthing else?
Incidently, your constructor would probalby be better written as:
Card::Card(int num, int su): Suit( su ), number( num )
{
}
This uses what is called the "initialization list". For integers, such as
this, it's trivial, but for other things can be not so trivial.