Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Inheritance and protected variables not working(C++).

+2 votes
asked Nov 4, 2021 by Areeb Sherjil (1,960 points)

I have a class called that has protected data:

class Sensors {
public:   
/* other code here  */
   
    uint16_t getType()const { return type; } // accessors defined
    float getValue()const { return value; }
    bool getValid()const { return valid; }

protected:
    uint16_t type=0;
    float    value=0;
    bool     valid=false; 
};

class RoomControllers:public Sensors  
{
public:
   
    RoomControllers(); // constructor
    void printStates() const;
    void setTemperatureTarget();
    

private:
    float   targetTemperature=19.0; // default initialisation 
    bool    currentlyHeating{false};
    float   targetHumidity=50;
    bool    currentlyHumidifying{false}; 
    std::array<Sensors, 5> sensors; // C-style array replaced by C++ array
};

void RoomControllers::printStates() const
{
    uint16_t validSensorCount = 0;

    cout << "Sensors:" << endl;
    cout << "--------" << endl;

    // For each sensor, print its Type and Value
    for (uint16_t count = 0; count < MAX_NUM_SENSORS; count++) {
        if (sensors[count].valid) {                     // the problem is here
            validSensorCount++;

Error says 

error: ‘bool Sensors::valid’ is protected within this context
Please provide advice on how to access the valid variable without using a getter function.

1 Answer

0 votes
answered Nov 4, 2021 by Peter Minarik (86,040 points)
selected Nov 5, 2021 by Areeb Sherjil
 
Best answer
Inheritance works fine. The problem is that you are not trying to access a protected member of the base class of your RoomController instance. Rather, you're trying to access a protected member of a Sensors class that has no relation (regarding inheritance) to your instance where you're trying to access it.

In other wolds inheritance only allows you to access member of the base class of your instance, not other instances of the same class.
commented Nov 4, 2021 by Areeb Sherjil (1,960 points)
what is the way around this problem?
commented Nov 5, 2021 by Peter Minarik (86,040 points)
edited Nov 5, 2021 by Peter Minarik
"Please provide advice on how to access the valid variable without using a getter function."

I'm afraid you need a getter function for this. :(

bool Sensors::IsValid() const { return valid; }

An alternative solution is to use friend functions/classes.

friend class RoomControllers;

Note: I tried to use a friend function, but it seems to be quite difficult as the friend would be a derived class at the same time.
commented Nov 5, 2021 by Areeb Sherjil (1,960 points)
thanks man, interesting problem tho.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...