Enum Type Constructor Initialization?

E

Eric C

I'm reading a c++ book by Eric Nagler named Learning C++ and I ran
into an exercise that requires a cpp file that defines several member
functions of a header. Here is my problem, it defines a constructor
that requires two enum types to be initialized. I haven't been able to
figure it out. Here is the code for the header:

--------------------
#define CARD_H

class Card {

public:
enum Suit {
Clubs, Diamonds, Hearts, Spades
};
enum Rank {
Ace, Deuce, Trey, Four, Five, Six, Seven,
Eight, Nine, Ten, Jack, Queen, King
};
Card(Suit, Rank);
Suit getSuit() const;
Rank getRank() const;
bool equals(Card const &) const;

private:
Suit const suit;
Rank const rank;
Card(Card const &);
};

#endif
--------------------

Now I've created a .cpp file to define these member functions,
including the constructor Card(Suit, Rank);. How do I define enum
types in this case? I'd really appreciate some help. Thanks

Eric
 
W

Woebegone

Eric C said:
I'm reading a c++ book by Eric Nagler named Learning C++ and I ran
into an exercise that requires a cpp file that defines several member
functions of a header. Here is my problem, it defines a constructor
that requires two enum types to be initialized. I haven't been able to
figure it out. Here is the code for the header:

--------------------
#define CARD_H

class Card {

public:
enum Suit {
Clubs, Diamonds, Hearts, Spades
};
enum Rank {
Ace, Deuce, Trey, Four, Five, Six, Seven,
Eight, Nine, Ten, Jack, Queen, King
};
Card(Suit, Rank);
Suit getSuit() const;
Rank getRank() const;
bool equals(Card const &) const;

private:
Suit const suit;
Rank const rank;
Card(Card const &);
};

#endif
--------------------

Now I've created a .cpp file to define these member functions,
including the constructor Card(Suit, Rank);. How do I define enum
types in this case? I'd really appreciate some help. Thanks

Eric

Well, Suit and Rank are defined types now, and Clubs, Diamonds, Ace, Deuce
etc. are identified _values_ of their respective types, so you don't have to
worry about those definitions. Think about the declaration:

Suit s;

What might you put in s? In other words, what is a reasonable Suit value?
They are the enumerated values in the definition, so you could say e.g. "s =
Clubs;".

For your constructor question, you should see what your book has to say
about initializer lists in constructors: since the fields suit and rank are
const, you have to use initialization to set values into them.

HTH!

Woebegone.
 

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,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top