Advertisement

Class inheritance

Started by March 13, 2003 02:03 PM
6 comments, last by deadlydog 21 years, 8 months ago
I''m making my first game, a Tetris clone, right now and I have 2 classes. One class , CBoard, holds all the information about the board, such as what cells are filled with what, if the Board is transparent, if the Boards grid should be shown, stuff like that. My other class, CBlock, inherits CBoard, and also holds all the Block information, like the shapes of the blocks, how to rotate them, move them, etc, which of course manipulate my CBoard object, "Board", to get the job done. Then in my main.cpp I just create CBlock "CurrentBlock", which holds both the CBlock and CBoard info. Now, when I call any of my CBlock functions, they all work fine, but, when I call my CBoard functions, none of them have any effect. This is because all of my CBoard functions manipulate the CBoard object "Board", and just calling a CBoard function from CurrentBlock is not manipulating "Board", it''s just calling the CBoard function with no specified object. The only way I can think of to solve this problem is to include the CBoard functions, that CBlock does not need to use, in CBlock, and just have the CBlock functions call "Board"''s functions. example: CBlock::TurnBoardGridOn() { Board.TurnGridOn(); } Is there another, preferably better, way to do this without including all of CBoards functions in CBlock as well, which pretty much defeats the purpose of having 2 separate classes. - God is my favorite fictional character
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
I think you''ve misunderstood the meaning of inheritence. Class inheritence describes an "is a" relationship. So, by inheriting your block class from your board class, you are saying that a block is a board.

What you probaby meant to say is a board "has a" block. In this case, that means that the board class should have a data member that is of the block class, like this:

class CBlock {    // block stuff goes here...};class CBoard {    void TurnGridOn(void);    // other board stuff...    CBlock currentBlock;}; 


Then in your main function you''ll create a CBoard class, like this:

int main(int argc, char *argv[]){    CBoard board;} 


Then access the current block with something like this:

    board.currentBlock.whatever(); 


Obviously, this is just one way of doing it. There are as many different ways of designing classes as there are people on this board...

Tony
Advertisement
Yea, as I understand it, you would use inheritance in something like the following case:

You create a cat class, and then you could use inheritance to derive the different breeds. For example, a mountain lion has four legs, feline appearance, etc... but it''s not quite the same as a domestic house cat. Still, it IS A cat.

As anthonylewis suggested, I think what you have hear is more of a "has a" relationship. I use a similar set up for the game I''m writing. My ship class has a sprite class, but it is not a sprite class itself.

Peon
Peon
Sorry, I never meant inheritance, I meant the "has a" relationship. What is that called again, I think it''s composition. But anyways, I do have it said up like you said AnthonyLewis, well sort of.

I''ll show you pretty much what it looks like.

class CBoard
{
public:
void DrawBoard
void TurnGridOn();
.
.
private:
.
.
}

class CBlock : public CBoard
{
public:
RotateBlock();
DrawAllBlocks();
.
.

private:
CBoard Board;
.
.
}

WinMain()
{
.
.
CBlock CurrentBlock;
.
.
.
CurrentBlock.DrawAllBlocks();
CurrentBlock.TurnGridOn();
.
.
}

where CurrentBlock.DrawAllBlocks() works, but CurrentBlock.TurnGridOn() does not work. This is because DrawAllBlocks() looks like this:

CBlock::DrawAllBlocks()
{
Board.DrawBoard();
}

so it actually manipulates Board, whereas CurrentBlock.TurnGridOn() does not manipulate Board, it just calls the CBoard class function, but with no object specified. ie Board. Now, I could go through and add all the functions from CBoard into CBlock.

ex. CBlock::TurnBoardGridOn()
{
Board.TurnGridOn();
}

But I think that defeats the purpose of having 2 separate classes. I just want to know if there is an alternative or not that would provide looser coupling between the classes. Or just any alternative at all.

Thanks in advance

- God is my favorite fictional character
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Hmmm... I''m still a little confused For a Tetris game, I kind of see your board class as the "manager" class that controls all of the actions between your different objects. In your CBlock class, you have it hold a CBoard. However, since your CBoard is kind of like a manager, it would make more sense, IMO, to have your CBoard class hold an array of the CBlock objects, instead of forcing the board into a block.

If you do it as I suggested above, you can then have the board tell the bricks to draw themselves. For example, your CBlock class might have a function called "drawBlock()" that would draw an indivdual block. You would have a similarly named function in your CBoard (maybe something like drawBoard()) which would loop through an array of CBlocks, calling their drawBlock() function.

I might be totally off the mark, but hopefully this is somewhat helpful

Peon
Peon
Looking at your code, I still think you have it backwards.

First, when you define CBlock you have this:
class CBlock : public CBoard 

The part that says ": public CBoard" means inheritence so get rid of that.

Also in your definition of CBlock, you have this:
private:  CBoard Board; 

That means that the CBlock has a CBoard. In other words the board is on the block. I think that should be the other way around.

Here''s how I would do it. First define CBlock:
class CBlock{public:  RotateBlock();  DrawAllBlocks();  .  .}; 

Now, define CBoard which has a CBlock:
class CBoard{public:  void DrawBoard  void TurnGridOn();  CBlock CurrentBlock;..}; 

Then your main program will look like this:
WinMain(){.CBoard Board;.Board.CurrentBlock.DrawAllBlocks();Board.TurnGridOn();.} 

If you do it that way, you can call CBlock functions and CBoard functions.

Tony
Advertisement
Thanks for the replies guys. My game does work the way I have it, but looking at your suggestions now, it does seem to make more sense the way you guys put it. I''m not too sure if I want to completely overhaul my game or not to change it. This is my first game So as long as I can get it running I''ll be happy.

Thanks to everyone who replied!

- God is my favorite fictional character
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA
Ahhh, I''m the biggest idiot I know. All I have to do to accomplish not having to create functions to the Board functions is just actually make "Board" a public object. That way I can just go CurrentBlock.Board.Whatever(). I can''t believe I didn''t think of that earlier. Now my game works great, the way it was. But thanks everyone for trying to help me out anyways.

- God is my favorite fictional character
-Dan- Can't never could do anything | DansKingdom.com | Dynamic Particle System Framework for XNA

This topic is closed to new replies.

Advertisement