Constructing Objects of a Class

Discussion in 'C++' started by kylemort@gmail.com, Mar 6, 2007.

  1. Guest

    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
     
    , Mar 6, 2007
    #1
    1. Advertising

  2. Jim Langston Guest

    <> wrote in message
    news:...
    >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.
     
    Jim Langston, Mar 6, 2007
    #2
    1. Advertising

  3. 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, ((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
     
    John Harrison, Mar 6, 2007
    #3
  4. Sarath Guest

    On Mar 6, 3:03 pm, 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, ((int)plcmnt / 13));
    >
    > but I am running into problems. Any help is greatly appreciated



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

    HTH
     
    Sarath, Mar 6, 2007
    #4
  5. Sarath wrote:
    > On Mar 6, 3:03 pm, 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, ((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
     
    John Harrison, Mar 6, 2007
    #5
  6. On Mar 6, 1:03 pm, 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, ((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
     
    =?iso-8859-1?q?Kirit_S=E6lensminde?=, Mar 6, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Archimede

    concurrency constructing objects

    Archimede, Nov 25, 2005, in forum: Java
    Replies:
    5
    Views:
    418
    Chris Uppal
    Dec 1, 2005
  2. Todd Bandrowsky

    Constructing a Class Given a String

    Todd Bandrowsky, May 21, 2004, in forum: C++
    Replies:
    1
    Views:
    354
    Victor Bazarov
    May 21, 2004
  3. Robbie Hatley

    Ways of constructing objects.

    Robbie Hatley, Jul 4, 2004, in forum: C++
    Replies:
    2
    Views:
    300
    Jonathan Turkanis
    Jul 4, 2004
  4. Replies:
    1
    Views:
    327
    Alf P. Steinbach
    Feb 23, 2006
  5. 7stud
    Replies:
    11
    Views:
    700
    Dennis Lee Bieber
    Mar 20, 2007
Loading...

Share This Page