Abstract Class

This is probably a Quick Question

I am working on a Tic-Tac-Toe Game, for my players I want to assign certain values; such as, the Symbol. I am using an Abstract Player Class, and want to have the two derived classes for the Player X, and the Player O.
The question is,

"Would it be standard practice to create a separate .h and .cpp file for all three classes, or would it be OK to include all three in one file set, since they're so closely related?

Thank You In Advance
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nr4-dont-place-each-class-declaration-in-its-own-source-file

However, what you probably want is to inherit a single PlayerXO class and create two instances of it.

rhamm5798 wrote:
using an Abstract Player Class, and want to have the two derived classes for the Player X, and the Player O.


Why?

Surely X and O are just objects of type Player? (With different symbols).
Can you explain why X and O are derived classes in the first place? You need just one class if the only difference between X and O is that they have different symbols; just use a char variable to store the symbol.

If it's an assignment and your assignment wants you to do so, then yeah; you have no choice (btw horrible assignment in that case). But otherwise what's the intention behind having two derived classes instead of just one class?
psst, rhamm5798, just tell them the derived classes are for humanPlayer and computerPlayer. ;0)
Just one class for the board might be a better idea. But if you intended to have objects that modified a board then that's perfectly fine too.
Last edited on
Grime wrote:
Just one class for the board might be a better idea. What say?
edited to
Just one class for the board might be a better idea. But if you intended to have objects that modified a board then that's perfectly fine too.
It would be a bit hard to say what level of abstraction would be appropriate but if I went for a single class it would be TicTacToe not board.
How'd you catch that edit Grey Wolf! :O
closed account (E0p9LyTq)
This is probably a Quick Question

Quick question, with lots and lots of passionate religious wars rhetoric of what The True Level of Abstraction Is The Holiest.

Simple questions usually aren't, but having differing opinions to grapple is instructive and helpful.

Personally I find the C++ Core Guideline Non-Rule about multiple class source files to be overly nitpicky and pedantic without any solid explanation of why it shouldn't be done.

I've seen experienced programmers advocate yes, giving very strong opinions for the separation, and other experienced programmers say nay with equal fervor.

Ultimately, you, the programmer will have to decide what makes sense to you. What makes it easier to write, test, debug and maintain solid C++ code.

With that all said, I would create an abstract base class that has two derived classes. One for the human player, another for the computer player.

Push as much similar code for either player up into the base class. Quick, off the cuff musing the only code I'd have in the derived classes is how the player chooses where to place their symbol. All other player related code resides in the ABC.

Going to a further level of abstraction I might consider creating a game board class that has uses the player classes as data members.

I'd put the player classes in one source with a header file, the game board in another, and if abstracted well main() might look like this:

1
2
3
4
5
6
7
#include "Gameboard.hpp"
#include "Player.hpp"

int main()
{
   Gameboard game;
}

The classes, if coded well, will handle all the game initialization, player moves and cleanup on game termination.

This is simply my personal opinion for creating a simple game, others will have differing ideas of how to make it all work.
Registered users can post here. Sign in or register to post.