more C++ hullabaloo

Started by
15 comments, last by musasabi 16 years, 3 months ago
this language is really intense. =P so. about inheritance. im sorry in advance to beat a dead a fossilized horse, but its important to me that i _really_ understand whats going on, what peoples opinions are, and so forth. inheritance. i know the usage of the 'protected' keyword is disputed on a number of levels, and i remember the old OOP prof from my local college giving C++ hell because of it. so (following a similar theme to my thread over here) if you have an item class from which you derive several useful items for the player (weapons, armor, what have you). if the base class of item has attributes you wish all items to have (name, weight, and cost maybe)... but you cant have those members in the derived classes because theyre private (should be, right?)... then whats the point of inheritance at all? from my understanding, you take the attributes that all inherited items will share and bump them up a level to the base class. that plays into the logic of a derivative, right? someone help me out, haha.
Advertisement
Sometimes there are objects that you dont want shared through what inherits it. For instance all guns will shoot bullets. So the bullet object doesnt have to be seen, just have its damage set. Sometimes there are objects that cant be seen because if they are changed in an inherited class they can screw up some other dependencies.

Thats why the Protected keyword comes in. Inherited classes can then use the Protected keyword so nothing can access the member outside of the class, yet inherited classes can still see it.

Lastly there is the Public keyword with allows for everything outside the class and inherited classes to see/use the member.

Was that your question?

--Brad
--X
Private means nothing outside the class can access it, but an inheritor can access it. So weapon can access name and weight from Item, if those attributes are private.

If a method/attribute is public, anything and his brother can access and change the value.
That is kind of the point of protected.
So that only child classes can access data that other wise would be private. Private members are not accessible from anything but the class they are declared in.
Quote:Original post by Stormtrooper
Private means nothing outside the class can access it, but an inheritor can access it.


Did you mean Protected? To my knowledge I have never been able to access a private member in an inherited class... maybe a typo?
--X
Quote:Original post by Stormtrooper
Private means nothing outside the class can access it, but an inheritor can access it.

This is wrong.
thanks all.

i understand how all three keywords work, im just asking about inheritance theory.

i know protected is shunned by some (with good reason, in a lot of circumstances), but _without_ it, i dont really see how a class can inherit anything useful at all.

like i was saying. item->weapon or item->armor for example. all items have a name and weight... but only weapons have damage, and armor has defensive properties.

however, without declaring name and weight as protected, how can you have them inherited?
Quote:Original post by musasabi
thanks all.

i understand how all three keywords work, im just asking about inheritance theory.

i know protected is shunned by some (with good reason, in a lot of circumstances), but _without_ it, i dont really see how a class can inherit anything useful at all.

like i was saying. item->weapon or item->armor for example. all items have a name and weight... but only weapons have damage, and armor has defensive properties.

however, without declaring name and weight as protected, how can you have them inherited?


AHHAHAH "protected" is shunned? Id love to hear a discussion where people can say its bad to use it. Can I ask where you heard this? I hope not from a book.

and you can keep them public if you really dont want to use protected. But protected was created just for that reason, so use it! heh.



EDIT: BTW after reading my post, it came off a bit mean to me, that was not my intention. I just cant seem to think of a reason to not use protected where it is needed. Also I meant public will let you do the same thing as protected but everything can see it.

--Brad
--X
Well, here's an example of your Item class.

class Item{public:    Item(float weight) :         m_weight(weight)     { }    float GetWeight() const    {        return m_weight;    }private:    float m_weight;};class Weapon : public Item{public:    Weapon(float weight, int damage) :        Item(weight),        m_damage(damage)    {    }    int GetDamage() const    {        return m_damage;    }    bool DamageGreaterThanWeight() const    {        return (GetDamage() > GetWeight());    }private:    int m_damage;};int main(){    Weapon w(3.0f, 4);    cout << w.GetWeight() << endl; // outputs 3.0    cout << w.GetDamage() << endl; // outputs 4    cout << w.DamageGreaterThanWeight() ? "Yes" : "No" << endl; // Outputs "No"    Item* i = &w;    cout << i->GetWeight() << endl; // outputs 3.0}


[Edited by - Sneftel on December 24, 2007 3:39:27 PM]
Quote:Original post by xsirxx
AHHAHAH "protected" is shunned? Id love to hear a discussion where people can say its bad to use it. Can I ask where you heard this? I hope not from a book.

From a strict OO perspective, "protected" can indeed be a very bad idea. The basic idea is that a class should have a public interface, which should be the sole means of accessing and mutating the class. protected breaks this invariant, by allowing derived classes to access what is obviously internal data. A derived class should not be responsible for maintaining the invariants specified by the base class, and protected forces derived classes to do this.

That is, I'd remind you, from a strict OO perspective, and C++ is not a good language for strict OO programming. C++ style often allows for protected to be useful. See the C++ FAQ-Lite for more; I'd link you to the specific FAQ, but I think finding it for yourself would be more instructive.

This topic is closed to new replies.

Advertisement