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

C++ 在.cpp中为派生类实现构造函数?,c++,xcode,class,constructor,derived-class,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 sen

我的问题是非常初级的,是的,我已经广泛地查找了它,但是当我做我在网上发现的事情时,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__) */
这是我的传感器。h:
#ifndef uu程序u 6 uu传感器__
#定义程序6传感器__
#包括
类传感器{
字符*传感器名称;
漂浮能量牵引;
智力功能;
内开外关;
公众:
传感器(字符*n,浮点pc);
虚拟空打印();
无效setOK(INTK);
int getOK();
无效设置关闭(int n);
int getOnOff();
};
//---------
数字传感器类别:公共传感器{
整型阅读;
公众:
数字传感器(字符*n,浮点pc);
虚拟空打印();
无效设置电流读数(INTR);
int getCurrentReading();
};
类传感器:公共传感器{
整型阅读;
int minRead;
int-maxRead;
公众:
模拟传感器(字符*n、浮点pc、整数毫米、整数mx);
虚拟空打印();
无效设置电流读数(INTR);
int getCurrentReading();
};
#endif/*已定义(\程序\传感器\传感器)*/
这是我的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) {

}
#包括“sensor.h”
#包括“定义.h”
使用名称空间std;
//--------传感器类------------//
传感器::传感器(字符*n,浮点pc){
SensorName=(char*)malloc(strlen(n)+1);
energyDraw=pc;
功能=工作;
onoff=关;
}
无效传感器::打印(){

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

digitalSensor::digitalSensor(char*n, float pc) : sensor(n, pc)
{
}
如您所见,它不返回任何内容,并调用其父级的构造函数

但你这样做是错误的:

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

按如下方式实现其构造函数:

digitalSensor::digitalSensor(char*n, float pc) : sensor(n, pc)
{
}
如您所见,它不返回任何内容,并调用其父级的构造函数

但你这样做是错误的:

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

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

例如:

   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

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

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

例如:

   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

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

这很重要;不要忽略它:这很重要;不要忽略它:非常感谢您的解释!我现在明白了。非常感谢!非常感谢您的解释!我现在明白了。非常感谢!非常感谢谢谢你的帮助!非常感谢你的帮助!