64. Sensor Packet Handling

#include <iostream>
using namespace std;
 
class Packet {
private:
    int id;
    int temperature;
    int humidity;
 
public:
    // Constructor initializes ID and resets sensor values
    Packet(int i) : id(i), temperature(0), humidity(0) {}
 
    void update(int t, int h) {
        temperature = t;
        humidity = h;
    }
 
    void print() {
        cout << "ID=" << id
             << " TEMP=" << temperature
             << " HUM=" << humidity;
    }
};
 
int main() {
    int id, t1, h1, t2, h2;
    cin >> id >> t1 >> h1 >> t2 >> h2;
 
    Packet pkt(id);
    pkt.update(t1, h1);  // first sample
    pkt.update(t2, h2);  // second sample
 
    pkt.print();
    return 0;
}

Explanation & Logic Summary:

  • Packet represents a small embedded data structure holding telemetry values
  • Constructor sets the packet ID and initializes sensor readings
  • update() simulates receiving new sensor measurements
  • print() outputs the packet contents in a deterministic format

Firmware Relevance & Real-World Context:

  • Telemetry packets are common in embedded sensor systems
  • Encapsulation prevents invalid state manipulation
  • Mirrors data handling patterns used in MCU firmware, IoT stacks, and sensor middleware
  • Reinforces foundational Embedded C++ class design principles

 

 

 

 

Loading...

Input

7 20 40 25 45

Expected Output

ID=7 TEMP=25 HUM=45