My question is more of a conceptual/learning one that applies in this specific instance. I'm doing an assignment, and I have a class called sensor, and a derived class called digitalSensor. One of the data members of sensor is "functioning". And, when I implement the print function for digitalSensor I need to print out a line based on whether the digital sensor is "functioning" or not.

essentially, I need to make an if statement in digitalSensor where it checks the value of "functioning". But Xcode tells me that "functioning is a private member of sensor". Since digitalSensor is derived from sensor, shouldn't it also have the "functioning" member variable? How do I check it in my creation of the digitalSensor print function?

Here is my sensor.h file:

#ifndef __Program_6__sensor__
#define __Program_6__sensor__

#include <iostream>

class sensor {
    char* SensorName;
    float energyDraw;
    int functioning;
    int onoff;

public:
    sensor(char*n, float pc);
    virtual void print();

    void setOK(int K);
    int getOK();
    void setOnOff(int n);
    int getOnOff();
};
//---------
class digitalSensor : public sensor {
    int reading;

public:
    digitalSensor(char*n, float pc);
    virtual void print();
    void setCurrentReading(int r);
    int getCurrentReading();
};

class analogSensor : public sensor {
    int Reading;
    int minRead;
    int maxRead;

public:
    analogSensor(char *n, float pc, int mm, int mx);
    virtual void print();
    void setCurrentReading(int r);
    int getCurrentReading();
};


#endif /* defined(__Program_6__sensor__) */

And here is my sensor.cpp file, my work on the print function is at the bottom:

#include "sensor.h"
#include "definitions.h"
using namespace std;

//--------SENSOR CLASS------------//
sensor::sensor(char *n, float pc) {

    SensorName = (char*)malloc(strlen(n)+1);
    energyDraw = pc;
    functioning = WORKING;
    onoff = OFF;
}
void sensor::print() {
    cout << "     Sensor: " << SensorName;
    cout << "   Power Consumption: " << energyDraw;
    if (functioning == WORKING) {
        cout << "\nSensor is functioning correctly\n";

        if (onoff == ON) {
        cout << "Sensor is On";
    }
        if (onoff == OFF) {
        cout << "Sensor is Off";
    }

    }
    if (functioning == NOTWORKING) {
        cout << "Sensor is not functioning correctly";
    }
    }
void sensor::setOK(int k) {
    functioning = k;
}
int sensor::getOK() {
    return functioning;
}
void sensor::setOnOff(int n) {
    onoff = n;
}
int sensor::getOnOff() {
    return onoff;
}
//---------------------------------//

//*********DIGITAL SENSOR**********//

digitalSensor::digitalSensor(char *n, float pc) : sensor(n, pc){
    reading = OFF;

}
void digitalSensor::print() {
    sensor::print();
    if (functioning == WORKING && onoff == ON) {
        cout << "Current sensor reading is: " << reading;
    }
    if (digitalSensor.functioning == WORKING && digitalSensor.onoff == OFF) {
        cout << "Current reading not available";
    }
}

The error is two lines after "void digitalSensor::print()" there at the end.

thanks for any help you all can give! I'll make sure to reciprocate and answer newbie questions too someday once I learn this stuff!

有帮助吗?

解决方案

In C++ the default visibility for class members is private, which means that fields and methods are not accessible from the outside, not even from subclasses. The solution is marking the concerned fields as protected (same as private, but accessible from subclass):

class sensor {
protected:
    char* SensorName;
    float energyDraw;
    int functioning;
    int onoff;

public:
    sensor(char*n, float pc);
    virtual void print();

    void setOK(int K);
    int getOK();
    void setOnOff(int n);
    int getOnOff();
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top