Before I ask my question: yes, this is an assignment for university.
The assignement is quite simple: implement the Whist card game with added functionality. The assistant wants us to 'meet' with inheritance (it's the second time that I follow this course, so, I know what it is) and told us to use the following for our Card class: create a base class Card from which you derive 4 inherited classes according to the suit they have (heart, diamond, ...).
I find this pretty weird myself to use inheritance here (since these classes will have the .fSuit
data member anyway) and we haven't seen virtual
yet, so we won't use it here. This isn't that hard to code, but the problem I have with this: We need to derive operators
to compare cards, but the derived classes may NOT be compared to each other (the compiler should throw out an error according to the assistant). I first used friend bool operator<(Card&, Card&)
but I learned that friend functions are not associated with derived classes in any way and the derived classes are handled as if they're the base class.
How am I supposed to get my code work the way he wants it to? I know I could stop using friend
and make the operators part of my Card class, but I'd still be able to compare the derived classes with each other. Am I missing something? Thanks a lot in advance.
ps: if it's not clean in any way to code a solution for it, I might as well just check the suitss in operators part of my Card class before comparing.
edit: He also wanted to make one operator def/decl work for all derived classes at once. Creating a friend operator for each derived class would else be a possible solution.
If you know the rules of the game, perhaps it makes sense to build the class the way your instructor is asking it.
In this game, each of the 4 players will get 13 cards out of a regular 52 card deck. The games is then played in 13 rounds called tricks. At each round, the winner of the last round decides which suit must be played, and others may either follow suit, play a trump card (whose suit is decided from the last card of the donor), or any other card (which will then be weaker than any suit or trump). The <
operator will therefore change meaning according to these parameters for each game (since the trump is not the same from one game to the next), and for each round (since the suit isn't the same from one round to the next).
Your class interface seems pretty much on track. Try to figure out how the comparison operator code must change. Somehow, you must keep track of the game round, and which suit is the chosen one, as well as which one is the trump one. These parameters should probably be part of the ancestor, while each of the specialized class would check if they are equal to the trump or the 1st card color of current round.
I don't think you need to use friend
functions for that.
Ideally, the game mechanics should perhaps be separated from the card handling part, but I guess you could mix these in your code for this assignment, since it's for studying the inheritance aspect of the language.