Thread: Stacks and enum questions

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    77

    Stacks and enum questions

    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.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I feel confuse about the enumerated type attribute
    You should because there is no enum type. An enum is a structure just like a class, or an array is a structure. Would you do this:
    Code:
    class Apple
    {
    public:
        int size;
    
    };
    
    int main()
    {
            class myApple;
            array myArray;
    
            return 0;
    }
    and expect to create an object of class Apple or an array called myArray? The answer is no. You need to do this:
    Code:
    int main()
    {
            Apple myApple;
            int  myArray[5];
    
            return 0;
    }
    ...and the type of the object myApple is Apple, and the type of the array myArray is, well that's not important.

    Here is an example using an enum:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	enum Suits {Spade, Club, Heart, Diamond};
    	//Spade is another name for the int 0.
    	//Club is another name for the int 1;
    	//Heart is another name for the int 2;
    	//Diamond is another name for the int 3;
    
    	Suits s;  //the variable type is Suits
    
    	s = Club;  //the same as saying s=1;
    
    	switch(s)
    	{
    	case Spade:  //same as case 0
    		cout<<"a spade"<<endl;
    		break;
    	case Club: //same as case 1
    		cout<<"a club"<<endl;
    		break;
    	case Heart: //same as case 2
    		cout<<"a heart"<<endl;
    		break;
    	case Diamond:  //same as case 3
    		cout<<"a diamond"<<endl;
    		break;
    	default:
    		cout<<"no such suit"<<endl;
    	}
    
    	return 0;
    }
    Last edited by 7stud; 04-25-2005 at 12:28 AM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    I am sorry, i don't really understand what it means.

    "You should because there is no enum type"
    Would you please explain it again?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    In C++ there are certain types like int, double, float, etc. Every variable must be declared with one of those types. You also can create classes which are user defined types, e.g.
    Code:
    class Apple
    {
    
    };
    And, then variables can be declared as type Apple, e.g.

    Apple myApple;

    The type of myApple is 'Apple'.

    In C++, there are structures like a class, an array, and a for-loop, but 'class' is not a type and 'array' is not a type and 'for-loop' is not a type. Similarly, 'enum' is not a type. So, you can't declare variables like this:
    Code:
    class a;
    array b;
    for-loop c;
    enum d;
    Last edited by 7stud; 04-24-2005 at 11:15 PM.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    My advice: don't use enums.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You care to explain your reasoning behind that.

    enums help you create self documenting code.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You care to explain your reasoning behind that.
    Sure, if you think an enum is a type, you'll never get your program to compile.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    enum itself is a keyword that defines a type. So while they might not be 100% correct they weren't that far off. Still no reason not to use enums and more of a reason to learn how to use them correctly. Also consider that they really only made one syntatical mistake as far as enum was concerned.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here's another explanation. The class keyword is used to create types:
    Code:
    class Apple
    {
    
    };
    Thereafter, you can declare variables of type Apple:

    Apple myApple;

    enum is also a keyword that is used to create types:
    Code:
    enum Suits {Spade, Club, Heart, Diamond};
    Thereafter, you can declare variables of type Suits:

    Suits s;

    However, you cannot declare variables of type 'class' or type 'enum' because they aren't types:

    class myApple; //error
    enum s; //error

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thats no justification for not using enums. And their wasn't that far off as you can do:
    Code:
    #include <iostream>
    using namespace std;
    enum Bob {
            a, b, c, d, e
    };
    
    int main()
    {
            enum Bob bob=a;
            cout<<bob<<endl;
    }
    And you can do this
    Code:
    #include <iostream>
    using namespace std;
    class Bob {
            int a;
            int b;
            int c;
            public:
                    Bob() : a(0), b(0), c(1) {}
            friend ostream& operator<<(ostream& o, const Bob &b)
            {
                    return o<<b.a<<'\t'<<b.b<<'\t'<<b.c;
            }
    };
    
    int main()
    {
            class Bob bob;
            cout<<bob<<endl;
    }
    So syntactically they were only 1 (very important) word off.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    77
    Is it a good way to code?
    Code:
    Card::Suit suit; // an enumerated type(located in the Card ADT) that
                             // identifies the suit of the jail cell.

Popular pages Recent additions subscribe to a feed