1

My question is very beginner, and yes I have looked it up extensively, but when I do the things I've found online Xcode gives me errors.

Basically, I'm just curious how to implement a constructor for a derived class. My class is called "Sensor" and the derived classes are digitalSensor and 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__) */

And here's my sensor.cpp, you can see the beginnings of my digitalSensor work 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**********//

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

}

In a nutshell: I need to make a constructor function for the digital sensor class. What am I missing, how do I do that? Thanks in advance for any help or knowledge on this!

singmotor
  • 3,930
  • 12
  • 45
  • 79
  • 1
    This is important; don't ignore it: http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier – chris May 24 '13 at 22:15

2 Answers2

5

Implement its constructor like this:

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

As you can see, it doesn't return anything and it calls its parent's constructor.

 

But you're doing this which is wrong:

sensor digitalSensor::digitalSensor(char *n, float pc) {
^^^^^^ constructors shall not return anything
}
masoud
  • 55,379
  • 16
  • 141
  • 208
3

You should call your base class constructor at the derived class's member initialization list.

For example:

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

This defines the digitalSensor constructor inline. You can also define it with scope resolution operator outside class:

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

It really depends on how your base class constructors are provided. But this could be one way to do it. You may find Initializing base classes and members useful.

taocp
  • 23,276
  • 10
  • 49
  • 62