473,320 Members | 1,572 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,320 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
2 12379

"Eric C" <co*********@hotmail.com> wrote in message news:98**************************@posting.google.c om...
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

You already defined them. I'm not srue what the question is. If
you're asking how to initialize the suit and rank variables (since you
made them const), you need to provide the constructor init syntax:

Card::Card(Suit s, Rank r) : suit(s), rank(r) { }
Card::Card(const Card& that) : suit(that.suit), rank(that.rank) { }

Of course, the above copy constructor is spurious. The compiler will generate
one with the same behavior if you just omit it. I'd recommend NOT defining
it.
Jul 19 '05 #2

"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 don't understand your question. The enums are already defined. Is it not
working correctly?
Jul 19 '05 #3

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

Similar topics

1
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, ... /*...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.