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...
LB wrote:
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?
I don't understand where you're confused?
Mathhead200 wrote:
Also notice that, there is no need to store the width and height as properties like that in the derived class.
I don't, I assign to them. because the interface does not define a way to change the width and height and neither does the derived class, it does exactly the same as your original example. To completely protect the width and height variables from deriving classes, you can use private inheritance and only publicly expose the functions you want to expose.
ne555, thanks for the great reading. This really sheds a light on the bad practices out there being taught. I'm glad that I haven't yet done much OO or I may have already contracted these bad habits...
No, I meant there was no need to store the width and height in ANY variables. By creating the variables you force TwoByTwo to inherit them, regardless of whether they are private or not. It's waisted memory.
Okay... Give me an example or explain (not in the form of a question please) why declaring a getter or setter method as virtual is bad. That is, when does it cause unwanted behavior.
Also, you don't have to completely abandon the parent class's method to override it. You can invoke it within the overriden method. Example:
1 2 3 4 5 6 7 8 9 10
class P {
public:
virtualdouble f(double x) = 0;
virtualdouble m(double x) { return f(x); }
};
class C {
public:
virtualdouble m(double x) { return P::m( x >= 0 ? x : -x ); }
};
struct Entity
{
virtualsignedint getX() const = 0;
virtualsignedint getY() const = 0;
virtual ~Entity(){}
};
class Enemy: public Entity
{
signedint x, y;
public:
virtualsignedint getX(){ return(x); } //notice that these
virtualsignedint getY(){ return(y); } //are virtual funcs
virtualvoid Attack() = 0;
virtual ~Enemy(){}
};
class Goomba: public Entity
{
boolean direction;
public:
//why would you want to override getX and getY at this point?
//If you do, you're obviously not going to be able to access x and y from Enemy,
//so ANY definition would give unwanted behavior
virtualvoid Attack(){}
virtual ~Goomba(){}
};
//why would you want to override getX and getY at this point?
//If you do, you're obviously not going to be able to access x and y from Enemy
But yes you can! x and y are not accessed directly (i.e. by name) they are accessed via the methods getX and getY. I can't think of a really good reason you would need to reimplement the accessor methods, but that doesn't mean there won't come a time when you will think of a reason, and you'll wish you could.
There is no reason to re-implement them. A deriving class should NEVER need to change the result of the original functions, because if they do then that is a design flaw. Obviously the code I posted is flawed too because x and y should be protected, but they were for examples. And besides, ne555 linked to a nice article about telling and not asking and thus making this discussion rather pointless, as this situation would never arise. Well, I've made a mess, and I hope with this I have cleaned it up: if you end up in a situation where you need tor redefine function behavior that should not change, you're doing something very, very wrong.