How to access the protected variables?

I cannot access the protected variables even within a derived class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>

using namespace std;

class Weapon {
  protected:
    int strength;
    char type;
  public:
    int modified;
    
    void setPower(const int s) {
      this->strength = s;
    }
    
    int return_strength () {
      return strength;
    }
    
    char return_type () {
      return type;
    }
};

class Rock: public Weapon
{

public:
    Rock()
    {
        type='r';
    }
    bool battle(Weapon w)
    {
        switch (w.return_type()) {
            case 'p':
                modified   = strength / 2;
                w.modified = w.strength * 2;
                break;
            case 's':
                modified   = strength * 2;
                w.modified = w.strength / 2;
                break;
            default:
                cout << "Error";
                break;
        }

        if (modified > w.modified)
        {
            return true;
        }
        else if (modified < w.modified)
        {
            return false;
        }
        
    }

};
A derived class may access it's own protected members, but it cannot access the protected members of another class, even if that class is the parent type.

Weapon provides accessors: return_strength() and setPower(); why not use those?

37
38
    modified = strength / 2;
    w.setPower( w.return_strength() * 2 );


BTW, your naming conventions leave a lot to be desired.

Hope this helps.
Last edited on
You can't directly access a protected member of the base class from a derived class. Since you already have an accessor function (i.e: return_strength()). So just use it instead.
Hi,

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 battle function should not be a part of the Rock class. Instead you can do Polymorphism:

http://www.cplusplus.com/doc/tutorial/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 const, they don't change anything in the class:

int return_strength () const {
return strength;
}

The Weapon class doesn't have any constructors.

Good Luck !!
Topic archived. No new replies allowed.