Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructing Objects of a Class

Reply
Thread Tools

Constructing Objects of a Class

 
 
kylemort@gmail.com
Guest
Posts: n/a
 
      03-06-2007
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

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      03-06-2007
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.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.


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      03-06-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
>


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[i] = 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
 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 3:03 pm, (E-Mail Removed) wrote:
> 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



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

HTH

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      03-06-2007
Sarath wrote:
> On Mar 6, 3:03 pm, (E-Mail Removed) wrote:
>
>>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

>
>
>
> Card^ deck = gcnew Card(plcmnt[i], ((int)plcmnt[i] / 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
 
Reply With Quote
 
=?iso-8859-1?q?Kirit_S=E6lensminde?=
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 1:03 pm, (E-Mail Removed) wrote:
> 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


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
constructing objects from a stream jesper@alphacash.se C++ 1 02-23-2006 08:40 AM
concurrency constructing objects Archimede Java 5 12-01-2005 11:42 AM
Ways of constructing objects. Robbie Hatley C++ 2 07-04-2004 08:43 AM
Constructing a Class Given a String Todd Bandrowsky C++ 1 05-21-2004 03:11 PM



Advertisments