473,503 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructing Objects of a Class

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

Mar 6 '07 #1
5 2069
<ky******@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr 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<CardDeck;
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.
Mar 6 '07 #2
ky******@gmail.com 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
Mar 6 '07 #3
On Mar 6, 3:03 pm, kylem...@gmail.com 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

Mar 6 '07 #4
Sarath wrote:
On Mar 6, 3:03 pm, kylem...@gmail.com 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
Mar 6 '07 #5
On Mar 6, 1:03 pm, kylem...@gmail.com 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

Mar 6 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2469
by: Hyoung Lee | last post by:
A simple method of simulating a class, such as C++ or UML class, in C would be using the struct construct. Similarly, we use C functions to simulate a methods; e.g., rv method_a (&struct,...
1
1358
by: Gravy | last post by:
Hi, When I create a new reference to a webservice proxy class (one that was created when I imported a web reference) there is a slight delay. Can someone tell me what it's doing to cause this...
1
1448
by: jesper | last post by:
Hi. This might be to basic, or of topic, or just plain silly. But, Is there a nice way to construct objects dynamically from a data stream (file, socket, memory) without prior enumerating...
9
8027
by: Dennis Jones | last post by:
Hi, I have some old code that I am refactoring to use smart pointers and have run into a small problem. My original code looks something like this: class WorkerThread { std::map<int,...
11
1895
by: bgreen | last post by:
I am hoping for some assistance with formatting a large text file which consists of a series of individual records. Each record includes specific labels/field names (a sample of 1 record (one of...
6
1225
by: Big George | last post by:
Hello, I develop in ASP.NET with VB.NET code. I need some help constructing a class: Worker. I'm designing the properties of this class. The class is filled reading tables in database. ...
1
1382
by: nabil | last post by:
Hi I have probleam while while initilazing nested class ... I needs to create a object 'b' of B(nested class) in A(surrounding class). while constructing b I have to pass pointer of funtion...
2
1422
by: PeterAlt | last post by:
Hello all! This is my first post here. I've been going crazy lately trying to master the art of working with and creating my own objects with Javascript. I see the HUGE power potential of object...
1
1210
by: Brent White | last post by:
I can't figure out what I'm doing differently with this one drop-down list control from the other two that are working just fine. Background: I am constructing a page that will allow a user to...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.