-1

I have a class called "Game", with prototypes functions "Update" and "Draw" but they're not defined. It's up to the object inheriting the "Game" object to override them. Is this possible?

Contents of "Game.h"

class Game // does it have to abstract/virtual?
{
    public:
    //General stuff for all games here
}

void Update(Game *game) = 0; // or make it virtual in someway

Contents of "MyGame.h"

#include "Game.h"

class MyGame : public Game
{
    public:
    // General stuff for my game
}

void Update(MyGame *game);

// Contents of "MyGame.cpp"

#include "MyGame.h"

void Update(MyGame *game) // does it have to be overriden/overloaded?
{
}

// Contents of "GameManager.h"

#include "Game.h"

class GameManager
{
    public:
    Game *game;
}

void Update(GameManager *manager);

// Contents of "GameManager.cpp"

#include "Game.h"

void Update(GameManager *manager)
{
    Update(manager->game);
}

The key is the last method: Why can't GameManager call MyGame Update() method when GameManager's Game object = MyGame and not Game?

Deukalion
  • 2,516
  • 9
  • 32
  • 50

1 Answers1

2

Define Draw and Update as virtual methods in the base class Game.

class Game
{
public:
   virtual void Draw() {};
};

class MyGame : public Game
{
public:
   virtual void Draw() {}
};

void callDraw(Game* game) 
{
   game->Draw();
}

//...

Game* game = new MyGame;
callDraw(game);

The last call will call the method in MyGame although it's called on a Game pointer.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • But I don't add methods to the classes, sorry, they're structs to. To minimize how much is transfered between each object I define the methods outside the class/struct. – Deukalion Aug 20 '12 at 14:41
  • @Deukalion `structs` and `classes` are basically the same, and methods do not get "transferred between objects". – juanchopanza Aug 20 '12 at 14:44
  • Well, whatever you say I guess there must be some relevance to why few objects (in graphics) don't have methods, instead you call methods like SetTextColor(), CreateFont() instead of just Font->SetWidth() and such. Instead you have outside methods that creates objects instead of having objects having methods. But that's just my take on it, also what I've learned. – Deukalion Aug 20 '12 at 14:50
  • @Deukalion what's the point of using classes or deriving from Game if you're not going to use polymorphism, a basic and integral part of oop? – Luchian Grigore Aug 20 '12 at 14:54
  • Because all games have certain features, like a screen and such but how you define that game is up to the developer. So I want a structure that works to redefine, if I have 2 Default Methods (Update, Draw) for each game I know by using the GameManger a game must define at least these methods, so I can recreate it. Polymorphism becomes Update(Game), Update(MyGame), Update(YourGame) and so on but the manager should be able to see this when calling the method by determinig what object it is - is possible. – Deukalion Aug 20 '12 at 14:58
  • Like I said, a solution would be if I could define the GameManager->Update() method with something like a generic solution but it has to be an inherited object of "base class 'Game'". I only know how to make generics, I have no clue as to wheter what I just said works. – Deukalion Aug 20 '12 at 15:00
  • @Deukalion I suggest you read a good C++ book + a good design pattern book before starting developing in C++. It seems to me like you don't grasp basic oop concepts. If you don't want to use oop, just stick to C. I don't want to discourage you with this comment, but instead want to push you into correctly using C++ and its many features. – Luchian Grigore Aug 20 '12 at 15:00
  • Reading a book is always the answer when the question asked is to complicated even for the one saying it. I grasp basic and more advanced oop concepts, I am however not entirely familiar with C++ as you can see. Polymorphism is when calling a method with different arguments, or as the case seem to be in C++ you can also get different results (unlike C#). Object oriented programming means that one object can have other objects, like class car has an instance of 4 wheels, or a book of C++ has an array of pages and each page has text, which each have a font, width, height, style, and so on. – Deukalion Aug 20 '12 at 15:08
  • @Deukalion not really. Polymorphism is **not** when you call a method with different arguments. That's function overloading. Polymorphism is when you achieve different behavior depending on the run-time type of an object. See my code. – Luchian Grigore Aug 20 '12 at 15:43