To be specific I'm trying to implement this into a small game. What I'm thinking of is having a base/default class for derived objects such as player, enemies, tiles and so on and later the derived classes would inherit this base class and add whatever is needed depending on the object.

Right now the base class contains all the basic variables needed and all the basic virtual functions. I'm thinking of making that the base class would have a constructor and parameters to initialize all the default variables and the derived class would add anything if needed in its own constructor, though this would mean I would have to add all the basic parameters which I would use in the base class constructor for initialization to the derived class constructor by using such syntax: classB(int x) : classA(x) and I think it's a bit too much work, I would like for a more automatic method.

Anyways, sorry for a long and hard to understand post, I would like some tips on how I should design such things. Posting some code below just clearance:

class Object
    fRect box_obj;
    float xVel, yVel, maxVel;
    float speed;
    std::string surfaceName;
    World* world_ptr;
    b2Body* body;
    Surface* sur_obj;
    Rotation* rot_pnt;
    virtual ~Object(); //Virtual methods defined in the .cpp file
    virtual void move();
    virtual void show();
    void set_angle();

And the Player:

#include "Object.h"

class Player : public Object
    Player(fRect fr_obj, World* bWorld, Surface* sur, std::string surName, bool isDynamic = false, bool rotatable = false);
    void handle_input(SDL_Event& event);
    void move();

But like I've said, this is a current method of mine which I think might need some work. I'm initializing those parameters in the derived class.

Edited by nuclear

4 Years
Discussion Span
Last Post by Despairy

since the fields are protected you are able to change them in the derived classes,
so not sure if I understand what you mean.
anyway i think your inheritance tree should look abit different .

first have a controller that will manage the game ( event handler display handler , stuff like that)
and its better if objects wont hold EVERYTHING, why does the player need to have World*?
to know if he can walk in some direction? instead make a Logic class that will recieve the players current
position and the next move position ( by the controller) and it will return true if the player can move there or not

make sure every char inherits only what it needs, you can make interfaces instead of only classes
tell me what youre thinking about and ill try to think about it too ;)

Votes + Comments
good answer
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.