6. Constructor

#include <iostream>
using namespace std;

class Sensor {
public:
    Sensor(int id) {
        cout << "Sensor " << id << " initialized\n";
    }
};

int main() {
    Sensor s(101);
    return 0;
}

 

Solution Explanation

  • The constructor Sensor(int id) runs automatically when Sensor s(101); is executed.
  • It prints the initialization message.
     

Layman’s Terms

The constructor is like a setup routine — it runs the moment a device is plugged in.

 

Significance for Embedded Developers

Constructors are useful for initializing peripheral drivers (e.g., setting baud rate for UART, or configuring GPIO pin modes) when the object is created.

Loading...

Input

Expected Output

Sensor 101 initialized