Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

The best strategy to convert this C code(structs) into C++(classes+objects) ?

+3 votes
asked Nov 1, 2021 by Areeb Sherjil (1,960 points)
Hi,

I have a old C code which contains a bunch of structs which I want to turn it into proper C++ code i.e object-oriented.

C/C++ code snippet:

struct Sensors {
    uint16_t type;
    float    value;
    bool     valid;
};
struct RoomControllers {
    Sensors sensor[MAX_NUM_SENSORS];
    float   targetTemperature;
    bool    currentlyHeating;
    float   targetHumidity;
    bool    currentlyHumidifying;
};

int main() {

    RoomControllers roomController = { {0},19.0,false,50.0,false };

This is my progress so far:

class Sensors {
public:
    Sensors(uint16_t typ, float val, bool vali) :type{typ}, value{val}, valid{vali}{};
    Sensors(uint16_t typ, float val) :type{typ}, value{val}{};
    Sensors(uint16_t typ) :type{typ} {};// all constructors defined
    Sensors() = default; // defaulted default constructor
    
    friend class RoomControllers; // RoomControllers can access all data from Sensors

protected:
    uint16_t type=0;
    float    value=0;
    bool     valid{};
    uint16_t MAX_NUM_SENSORS{ 5 };
    enum { INVALID, TEMPERATURE, HUMIDTY };
    enum { NO_CHANGE, HEATING_STATUS_CHANGED, HUMIDIFIER_STATUS_CHANGED };
    enum { NEW_SENSOR, SENSOR_NOT_VALID, MAX_SENSORS_REACHED };
    enum { OK, INVALID_SENSOR_SELECTED };
};

class RoomControllers :public Sensors
{
public:
    RoomControllers(Sensors sensor1);
    void printStates() const;
    void setTemperatureTarget();
    void setHumidityTarget();
    uint8_t checkValues();
    uint8_t newSensor();
    uint8_t updateSensor();

    void checker(); // tidy up bulky switch statements

private:
    float   targetTemperature=19.0; // default initialisation
    bool    currentlyHeating{false};
    float   targetHumidity=50;
    bool    currentlyHumidifying{false};
    std::array<Sensors, 5> sensors; // C-style array replaced by C++ array
    static int32_t counter; // static variable for array indexing
};

int32_t RoomControllers::counter{}; //variable must be initialised to 0 outside class

RoomControllers::RoomControllers(Sensors sensor1)
{
        sensors[counter] = sensor1;
        ++counter;   
}

 My question is how can I place many Sensors objects inside the array in RoomControllers from the int main() function(just like it was using the structs in the C code)? Because the constructor as it is for the RoomControllers only takes one object.

1 Answer

0 votes
answered Nov 1, 2021 by Peter Minarik (86,040 points)
edited Nov 1, 2021 by Peter Minarik

There are plenty of ways to do that.

You can create a new constructor for RoomControllers that accepts an std::vector<Sensors> so you can pass in all the Sensorss (what is the plural of Sensors, lol? --> Probably the correct name for that class should be Sensor -- singular, not plural).

Another option is to create a new function to add a new Sensors to your existing list, e.g.

void AddSensors(Sensors sensors);

In C++ you have dynamic collections (such as the already mentioned std::vector). Are you sure you want to have a static sized collection that has a fixed size all the time?

commented Nov 1, 2021 by Areeb Sherjil (1,960 points)
thanks man, I'll have a look. The array has to be fixed to 5 as that is the max sensors I am supposed to use.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...