How do I get classes within a class???

Reply

Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

How do I get classes within a class???

 
0
  #1
Feb 18th, 2009
For instance... I have the class Deck, which represents a deck of cards, and I want another class, derived from that, called cards, I want class Deck to have a class array called Cards[52], so I can assign all the different values and strings to the individual Cards class instances and have the class Deck do things to the deck as a whole... Im talkin something like this
  1. class Deck{
  2. protected:
  3. class Cards[52]; // This doesnt work to have multiple instances in the class
  4.  
  5. public:
  6. void Shuffle();
  7.  
  8. };
  9.  
  10. class Cards : public Deck{
  11. Cards(int,string);
  12. };

ik that you might not understand why i want to do this in two different classes, but i do... so please just gimme a clear cut answer. please and thank you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,802
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 213
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: How do I get classes within a class???

 
0
  #2
Feb 18th, 2009
Don't define class Card within class Deck.
Create a card class.
Then let deck class contain an array of card objects

class Card {
string suit;
int value;
///more stuff
};

class Deck{
Card theDeck[52];
//more stuff
};
Don't own a gun but I'm going to join the NRA.
I figure anything that irritates liberals is worth my support.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 187
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster

Re: How do I get classes within a class???

 
0
  #3
Feb 18th, 2009
When I had to use two classes that interlaced, I wrote my first class out, then wrote the second class out, then had a variable in the second class that called the first class. For your example, I would write out what you want class Card to do. Then write out what you want class Deck to do, and when you know where they overlap, use a variable that jumps between the classes.

For me, I'm going to call my classes Deck and PCards so I can make sure it all makes sense. So, say you want class Deck to shuffle the cards. You would try something like this:
  1. PCards Shuffle (PCards cards[], int 52)
  2. {
  3. cards.randomize();
  4. return cards;
  5. }

Where they would be a function in class PCards that would randomize them. Now, that's just off the top of my head, and I don't know what you are trying to do, but that's how I handled having two classes.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 322 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC