Constructing Objects of a Class

K

kylemort

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, ((int)plcmnt / 13));

but I am running into problems. Any help is greatly appreciated
 
J

Jim Langston

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, ((int)plcmnt / 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.
 
J

John Harrison

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, ((int)plcmnt / 13));

but I am running into problems. Any help is greatly appreciated


You should stop trying to program C++ like you program Java.

Card a_card(3, 3);

might be the 3 of diamonds, for instance.

Card deck[52];
for (int i = 0; i < 52; ++i)
deck = Card(i%13, i/13);

would be one way to initialise a whole deck of cards.

You do not have to use new (or gcnew) to create objects in C++, and
generally speaking, you code will be a lot more efficient for it. This
is one of C++'s big advantages over Java.

john
 
S

Sarath

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, ((int)plcmnt / 13));

but I am running into problems. Any help is greatly appreciated



Card^ deck = gcnew Card(plcmnt, ((int)plcmnt / 13));

HTH
 
J

John Harrison

Sarath said:
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, ((int)plcmnt / 13));

but I am running into problems. Any help is greatly appreciated




Card^ deck = gcnew Card(plcmnt, ((int)plcmnt / 13));

HTH


However helpful that might be to the OP, it is not legal C++, and he/she
may not be aware of that.

OP, if you are trying to learn 'managed C++' (or whatever it is that
Microsoft are calling it these days) you need to ask about it on a
Microsoft group. Here we only deal with 'straight' C++ and you going to
get everyone very confused (including yourself) if you start asking
about Microsoft's extensions to the C++ language here.

john
 
?

=?iso-8859-1?q?Kirit_S=E6lensminde?=

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, ((int)plcmnt / 13));

but I am running into problems. Any help is greatly appreciated


You probably ought to be using an enum for the suit.

Whether you use object or value semantics for the cards themselves
depends on how you intend to use them (what should the results of
comparisons be for example). Do you consider every ace of spades to be
the same (value semantics) or do you need to track individual aces of
spades (object semantics)? If the former you should be storing the
cards in, for example, a vector. I.e. std::vector< Card >. If you want
to track individual cards then you'll be handling pointers to Card
instances (preferably with smart points) so something more like
std::vector< boost::shared_ptr< Card > >.


K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top