检查派生类的数据成员

时间:2013-05-26 19:54:32

标签: c++ class implementation derived-class datamember

我的问题更多是适用于这个特定情况的概念/学习问题。我正在进行一项任务,我有一个名为sensor的类,以及一个名为digitalSensor的派生类。传感器的数据成员之一是“正常运行”。而且,当我为digitalSensor实现打印功能时,我需要根据数字传感器是否“正常工作”打印出一条线。

基本上,我需要在digitalSensor中创建一个if语句,它会检查“运行”的值。但Xcode告诉我“功能是传感器的私人成员”。由于digitalSensor是从传感器派生的,它不应该也有“功能”成员变量吗?如何在我创建的digitalSensor打印功能中进行检查?

这是我的sensor.h文件:

#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__) */

这是我的sensor.cpp文件,我对打印功能的工作在底部:

#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";
    }
}

错误是“void digitalSensor :: print()”之后的两行。

感谢您提供的任何帮助!一旦我学会了这些东西,我一定会回复并回答新手问题!

1 个答案:

答案 0 :(得分:4)

在C ++中,class成员的默认可见性为private,这意味着无法从外部访问字段和方法,甚至不能从子类访问。该解决方案将相关字段标记为protected(与private相同,但可从子类访问):

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();
};