Stacks and enum questions

J

joenchinghkg

I am gonna to create an ADT named JailCell, inside the JailCell class
I have 2 private attributes:
Here is my jailcell.h
Code:

// Jailcell.h
#ifndef JAILCELL_H
#define JAILCELL_H
#include "card.h"

class Jallcell
{
public:



private:
stack<Card> cards; // an STL stack of Card ADT's

enum suit; // an enumerated type(located in the Card ADT) that
// identifies the suit of the jail cell.


};
#endif


I feel confuse about the enumerated type attribute, since I think it's
from my Card class, anyone can advise me how i can code it?

Here is the card.h and card.cpp for references.
Code:

// card.cpp
#include <cassert>
#include <iostream>

#include "card.h"

using namespace std;

Card::Card() :
rank( ACE ),
suit( SPADES )
{
}

Card::Card( Rank newRank, Suit newSuit ) :
rank( newRank ),
suit( newSuit )
{
}

void Card::setRank( Rank newRank )
{
rank = newRank;
}

void Card::setSuit( Suit newSuit )
{
suit = newSuit;
}

ostream& operator<<( ostream& os, const Card& card )
{
switch( card.getRank() )
{
case Card::ACE:
os << "Ace";
break;
case Card::TWO:
os << "Two";
break;
case Card::THREE:
os << "Three";
break;
case Card::FOUR:
os << "Four";
break;
case Card::FIVE:
os << "Five";
break;
case Card::SIX:
os << "Six";
break;
case Card::SEVEN:
os << "Seven";
break;
case Card::EIGHT:
os << "Eight";
break;
case Card::NINE:
os << "Nine";
break;
case Card::TEN:
os << "Ten";
break;
case Card::JACK:
os << "Jack";
break;
case Card::QUEEN:
os << "Queen";
break;
case Card::KING:
os << "King";
break;
default:
assert( 0 );
}

os << " of ";

switch( card.getSuit() )
{
case Card::SPADES:
os << "Spades";
break;
case Card::HEARTS:
os << "Hearts";
break;
case Card::CLUBS:
os << "Clubs";
break;
case Card::DIAMONDS:
os << "Diamonds";
break;
default:
assert( 0 );
}

return os;
}



Code:

#include <iostream>

using namespace std;

class Card
{
public:

enum Rank
{
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
};

enum Suit
{
SPADES,
HEARTS,
CLUBS,
DIAMONDS
};

Card(); // Default constructor will initialize to Ace of Spades
Card( Rank newRank, Suit newSuit );

Rank getRank() const {return rank;}
Suit getSuit() const {return suit;}

void setRank( Rank newRank );
void setSuit( Suit newSuit );

private:

Rank rank;
Suit suit;
};

ostream& operator<<( ostream& os, const Card& card );



Appericate it.
 
V

Victor Bazarov

joenchinghkg said:
I am gonna to create an ADT named JailCell, inside the JailCell class
I have 2 private attributes:
Here is my jailcell.h
Code:

// Jailcell.h
#ifndef JAILCELL_H
#define JAILCELL_H
#include "card.h"

class Jallcell

Did you notice that the name of the class is misspelled? That's what
happens when you instead of copy-and-pasting, just type into a newsgroup
posting.
{
public:



private:
stack<Card> cards; // an STL stack of Card ADT's

enum suit; // an enumerated type(located in the Card ADT) that
// identifies the suit of the jail cell.

Did you mean to say

Card::Suit suit;

?

V
 
J

joenchinghkg

I think this should be what I want, can i put it inside private
directly?

Card::Suti suit;
 
J

joenchinghkg

For Card::Suit suit, is it depends on the Card class?

Can you explain what Card::Suit means ?
thanks
 
V

Victor Bazarov

joenchinghkg said:
For Card::Suit suit, is it depends on the Card class?

Depends? I guess. By the time the compiler encounters 'Card::Suit',
it better know what 'Card' is.
Can you explain what Card::Suit means ?

Card::Suit is a name. In your case, it's a name of the enumeration.
To the compiler the name of an enumeration is a type. So, Card::Suit
is a type.

V
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top