1

I have 2 questions

  1. Is enumeration constant of int datatype?
  2. For class Card, do I create two data member of enum type Face and Suit and then initialize it via the constructor?

Background:

9.23 (Card Shuffling and Dealing) Create a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and a driver program. Class Card should provide:

  • a) Data members face and suit—use enumerations to represent the faces and suits.
  • b) A constructor that receives two enumeration constants representing the face and suit and uses them to initialize the data members.
  • c) Two static arrays of strings representing the faces and suits.
  • d) A toString function that returns the Card as a string in the form “face of suit.” You can use the + operator to concatenate strings.

Class DeckOfCards should contain:

  • a) An array of Cards named deck to store the Cards.
  • b) An integer currentCard representing the next card to deal.
  • c) A default constructor that initializes the Cards in the deck.
  • d) A shuffle function that shuffles the Cards in the deck. The shuffle algorithm should iterate through the array of Cards. For each Card, randomly select another Card in the deck and swap the two Cards.
  • e) A dealCard function that returns the next Card object from the deck.
  • f) A moreCards function that returns a bool value indicating whether there are more Cards to deal.

This is from c++ how to program by deitel.

sorry, I am quite confused because I have never initialized scoped enum in class constructor before.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • The book says " The values of these enumeration constants are of type int, start at 0 (unless specified otherwise) and increment by 1. " – WorldTreeBoy Jun 18 '22 at 00:43
  • Was trying to confirm what I read in the book because I am not sure to initialize an enum data member using constructor that receive enumeration constant which is an int? – WorldTreeBoy Jun 18 '22 at 00:45
  • @TedLyngmo Do you have any c++ book to recommend for beginners to learn how to program in c++? – WorldTreeBoy Jun 18 '22 at 00:50
  • 1
    @WorldTreeBoy There's a [list here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that has some good beginner-friendly material – Nathan Pierson Jun 18 '22 at 00:51

1 Answers1

1

An enumeration constant presumably is of an enum type: https://en.cppreference.com/w/cpp/language/enum .

Yes, depending on the kind enumeration, it can be converted to integers, sometimes the conversion makes sense (e.g. for the face value) but in some cases you want to stay away from it (e.g. for the suit value). For the later enum class helps.

And yes, it seems that the exercise is pushing you into designing the Card class like that.

enum Suit { spades, clubs, hearts, diamonds };
enum Face { ace, two, three, ...};

class Card {
 public:
  Suit s;
  Face f;
};

Finally, to answer the question on the title, you don't need a constructor because you can already do:

    Card my_card{spades, two};

But otherwise, yes you can have a constructor that takes enums:

class Card {
...
    Card(suit my_suit, face my_face) : s{my_suit}, f{my_face} {}
};

Note that this is where books go out their way to make you think that a certain thing should implemented in a certain way just because you have learned a new feature of the language. This is not the only way to implement a Card class or more importantly a Card game application.

It depends a lot what you do with them later. Specially what kind of "collections" you tolerate for the Cards class. (A card in isolation is kind of meaningless.)

Think also if Cards should be copyable?, movable?, swappable? (or even mutable!?). What about special values, is there a "blank card"? is there a "jack"? How to ensure in the logic of the program that there are no duplicated flying around? These are the real questions in the design of a class, IMO.

alfC
  • 14,261
  • 4
  • 67
  • 118
  • well I haven't learn about struct yet but thanks for the answer. guess I will skip this question. – WorldTreeBoy Jun 18 '22 at 00:57
  • 2
    @alfC Good job on the the _"Note that this is where books..."_ part. Brilliant! – Ted Lyngmo Jun 18 '22 at 00:58
  • 1
    @WorldTreeBoy , ok, removed the keyword `struct` to avoid confusion. Take into account that you are learning features of the language and almost nothing about design with these examples. – alfC Jun 18 '22 at 00:59
  • 1
    @TedLyngmo, I wasted ten years of my life as an amateur programmer believing that I could learn how to program from books that shouldn't promise more than just basic examples of language features. It was unfair to single out Deitel but it is one of the paradigmatic books. – alfC Jun 18 '22 at 01:04
  • @alfC Nice delivery :-) – Ted Lyngmo Jun 18 '22 at 01:05
  • @alfC So it's true most books only teach you about the language feature but does not teach about good design. – WorldTreeBoy Jun 18 '22 at 01:06
  • 1
    @WorldTreeBoy Presumably, there are books on that topic too. – Ted Lyngmo Jun 18 '22 at 01:11
  • 1
    There are books that only teach you the language and keep examples to the minimum and carefully not misleading you into bad designs (this is the hard part) [e.g. "A Tour of C++"]. There are others that teach you the language through "good" applications of the language ["C++ for mathematicians" or "Scientific and Engineering C++"]. And then there are these terrible books where examples are artificial and end up teaching bad design from people than never worked in real applications. I would prefer books with clear goals, like those on game programming in C++ which at least have a clear goal. – alfC Jun 18 '22 at 01:14
  • how do i close this question? – WorldTreeBoy Jun 18 '22 at 01:18