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.