在.cpp中为派生类实现构造函数?

时间:2013-05-24 22:14:17

标签: c++ xcode class constructor derived-class

我的问题非常初学,是的,我已经广泛查找了,但是当我在网上找到的东西Xcode给了我错误。

基本上,我只是好奇如何为派生类实现构造函数。我的类叫做“传感器”,派生类是digitalSensor和analogSensor。

Here's my 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,你可以看到我的digitalSensor工作的起点在底部。

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

sensor digitalSensor::digitalSensor(char *n, float pc) {

}

简而言之:我需要为数字传感器类创建一个构造函数。我错过了什么,我该怎么做?提前感谢您对此有任何帮助或了解!

2 个答案:

答案 0 :(得分:4)

像这样实现它的构造函数:

digitalSensor::digitalSensor(char*n, float pc) : sensor(n, pc)
{
}

如您所见,它不会返回任何内容,而是调用其父级的构造函数。

但你这样做是错误的:

sensor digitalSensor::digitalSensor(char *n, float pc) {
^^^^^^ constructors shall not return anything
}

答案 1 :(得分:3)

您应该在派生类的成员初始化列表中调用基类构造函数。

例如:

   class digitalSensor : public sensor {
    int reading;

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

这定义了内联的digitalSensor构造函数。您还可以使用类外的范围解析运算符来定义它:

digitalSensor::digitalSensor(char*n, float pc):sensor(n, pc), reading(0){}
//^^Note that constructor of a class does not have any return type

这实际上取决于如何提供基类构造函数。但这可能是一种方法。您可能会发现Initializing base classes and members有用。