There probably isn't much point in making the variables protected and
providing accessor functions, just have the latter. It's not a good idea to have protected data - it's just as bad as public data. That is according to Scott Meyers.
Having said that, one can access protected variables/ functions from higher up the inheritance tree - they are a part of the derived class because they are inherited - that is the whole point of protected access. But where you have gone wrong, is that you are trying to access a different object
- which means the access is private.
Now that I have mentioned that, please don't take the easy way out and use protected variables everywhere. It is much more proper to use to interface of each class. One can have a public interface with public accessor (get functions) and optionally mutator (set ) functions. Also, one can put the Interface into a separate class. One can also have a protected interface.
As well as that, you shouldn't need to put the type of the object into the class. The
function should not be a part of the
class. Instead you can do Polymorphism:
There is also no need to use the this pointer in this context:
this->strength = s;
just do :
strength = s;[
The accessor functions should be marked
, they don't change anything in the class:
int return_strength () const
The Weapon class doesn't have any constructors.
Good Luck !!