473,382 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Enum Type Constructor Initialization?

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
Jul 19 '05 #1
1 4428
"Eric C" <co*********@hotmail.com> wrote in message
news:98**************************@posting.google.c om...
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.
Jul 19 '05 #2

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

Similar topics

2
by: Eric C | last post by:
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...
18
by: Marina | last post by:
Hi, if anyone can help on this, please! This is driving me crazy. I have a user control hierarchy several levels deep that derive for UserControl (windows). The class that is at the top level,...
7
by: cppaddict | last post by:
Hi, I've been trying to debug a strange runtime error for the last 5 hours... I'm hoping someone might have an insight about it. I have an application that creates a vector of MyDisplay...
3
by: Teddy | last post by:
what is the special meaning of enum members in class ?
3
by: Sanjay Pais | last post by:
I know that string/char enum is not possible in c# (.NET2.0) I need to create the equivalent of this: public enum HOW_GOOD { AWESOME = "A", GREAT= "G", NOT_TOO_BAD = "N", TERRIBLE="T" }
6
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
10
by: kar1107 | last post by:
Hi all, Can the compiler chose the type of an enum to be signed or unsigned int? I thought it must be int; looks like it changes based on the assigned values. Below if I don't initialize...
4
by: =?Utf-8?B?0JrQvtC70LXQstCw?= | last post by:
I want to use an enum field in a constructor. For example: class Student { private string name; private enum university {.....}; private byte course; ......
3
by: theBNich | last post by:
Hi, I currently have an enumeration in class Lexer: enum lexType { /* token keywords */ lexIF, lexTHEN, lexWHILE, lexDO, lexBEGIN, lexEND, lexELSE, lexPROGRAM, ... /*...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.