Object is my base class. Tile needs to extend Object. Mob needs to extend Object. Player needs to extend Mod.
I set up my Object class and then started on my Tile class. Soon I realized I didn't know how to inherit my Object class the way I wanted to, or if that is even what I should be doing.
#ifndef _OBJECT_H_
#define _OBJECT_H_
class Object
{
private:
int id;
int type;
int x;
int y;
public:
Object(int objID);
void SetType(int objType);
void SetX(int posX);
void SetY(int posY);
int GetID();
int GetType();
int GetX();
int GetY();
}
#endif
I am thinking that I still want to be able to access type, x, & y directly, but not id. I also don't know how I would use Object's constructor after that to set id. id is not to be changed after being set.
However in the case of the Player class, would I have Player's constructor call Mob's constructor, then have Mob's constructor call Object's constructor?
I just did some reading on virtual functions, and I see some benefit to it. However, I don't see the need to do that just yet. The project I am working on is a very small one, and I think it would just over complicate it for me at this point.
The game simply allows the player to click on the screen and go to that location, avoiding objects along the way. The Mobs just walk around the screen randomly.
If you ever going to use polymorphism, use virtual on all your methods (especially the destructors!) It doesn't over complicate anything... In fact, all you have to do is add the keyword virtual before all your methods... That's it! So the above Object class would be:
#ifndef _OBJECT_H_
#define _OBJECT_H_
class Object
{
private:
int id;
protected:
int type;
int x;
int y;
public:
Object(int objID);
//The destructor:
virtual ~Object();
//I don't know if you still want these public mutator methods:
virtualvoid SetType(int objType);
virtualvoid SetX(int posX);
virtualvoid SetY(int posY);
virtualint GetID();
//I assume you want these public accessor methods still:
virtualint GetType();
virtualint GetX();
virtualint GetY();
}
#endif
You put virtual in the methods that you want polymorfic behaviour. It is not necessary for all of them.
However if you have at least one virtual method, then you should make the destructor virtual too.
Those accessor are already bad, ¿but why make them virtual? They refer to variables of the parent class ¿when will you override the behaviour?
You don't know that, there is no body to these methods.
ne555 wrote:
why make them virtual... when will you override the behaviour
I don't know, but it dosen't hurt performance to do so. You might decide to run some debugging later in a derived class so that getType() always returns 0, (or something like that.)
But I do know what they do, based on meaningful name
Actually getType could be virtual as an implementation of RTTI. However it's got the type variable, so if you want to return 0; then simply assign it that value.
Accessors break encapsulation and put the responsibillity in the wrong class.
By the way, please don't misquote. It seems that I'm saying exactly the opposite that I meant.
ne555 is right, you should only virtualize the destructor and any member functions that should be run based on the true type of an instance rather than the type it looks like. Getters and setters should NEVER be virtual.
Well it appears we have different approaches. I don't see how declaring a getter/setter as virtual could be bad, and the whole point of getter and setter methods is abstraction. The other programmer(s) need not know that there is a variable somewhere with the return value.
What I meant about performance was: I was under the impression that once one method is declared virtual, it makes no difference if the rest are virtual in performance time (although I didn't think about inline functions.)
If a function is virtual then it may be redefined by an inheriting class. Why in the world would an inheriting class need to change a getter or setter? It sure can't change the base class' private variables, why should it be able to change the behavior of the functions that deal with them?
Also, does it really matter if there is a (good) reason? What is your reasoning as to not make them virtual? Can you explain when that would be a good idea?
No you didn't. You just asked "why" someone would want to do it, implying there is no good reason. However, you never explained why it could be bad...
And you should look up interfaces, as they are not a (real) thing in C++, but more in OOP design. (A way to avoid some multiple inheritance issues.) Note that the class Matrix dosen't have any implemented methods (minus the default destructor) and has no variables. Also notice that, there is no need to store the width and height as properties like that in the derived class.
This matrix interface supplied all the stuff you need to be able to do with a matrix without giving any restriction to the derived class on how to do it. Also the matrix interface does not add to the size in memory of the derived class.