Data Validation inside Setters

Medium Encapsulation, Access Specifiers & The this Pointer
Solve in Playground →

Problem Statement

Create a `Thermometer` class with a private integer `temp`. In its setter `setTemp(int t)`, add validation: if the given temperature is below absolute zero (-273), set `temp` to exactly -273. Otherwise, set it to `t`. Print the result using a getter.

Input Format

A single integer.

Output Format

Print the validated temperature.

Constraints

None

Sample Input

-300

Sample Output

Temp: -273

Explanation

Setters allow you to intercept incoming data and sanitize or validate it before assigning it to the object's state.

Starter Code

#include <iostream>

class Thermometer {
    // Define private state and public validated setter
};

int main() {
    // Read temperature, assign via setter, and print
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used