Skip to main content

Lesson 7 of 7

Practice & Quiz - OOP Part I

Test your understanding of class vs struct, constructors, const methods, static members, and RAII with a practical exercise and 5 quiz questions.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Temperature Sensor Class

Practical Exercise - Temperature Sensor Class
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>

class TemperatureSensor {
    std::string name_;
    std::vector<double> readings_;
    const int max_readings_;       // const member - MUST use MIL
    static int sensor_count_;

public:
    TemperatureSensor(std::string name, int max = 100)
        : name_(std::move(name))
        , max_readings_(max)
    {
        if (max <= 0) throw std::invalid_argument("max_readings must be > 0");
        ++sensor_count_;
    }

    ~TemperatureSensor() { --sensor_count_; }

    // Deleted copy - each sensor is unique
    TemperatureSensor(const TemperatureSensor&)            = delete;
    TemperatureSensor& operator=(const TemperatureSensor&) = delete;

    void record(double celsius) {
        if ((int)readings_.size() >= max_readings_)
            throw std::overflow_error("Sensor buffer full");
        readings_.push_back(celsius);
    }

    double average() const {
        if (readings_.empty()) return 0.0;
        double sum = 0;
        for (const auto& r : readings_) sum += r;
        return sum / readings_.size();
    }

    const std::string& name() const { return name_; }
    int count() const { return (int)readings_.size(); }
    static int total_sensors() { return sensor_count_; }
};

int TemperatureSensor::sensor_count_ = 0;

int main() {
    std::cout << "Sensors: " << TemperatureSensor::total_sensors() << "\n"; // 0
    {
        TemperatureSensor s{"CPU", 5};
        s.record(45.2); s.record(47.8); s.record(50.1);
        std::cout << s.name() << " avg: " << s.average() << "°C\n";
        std::cout << "Sensors: " << TemperatureSensor::total_sensors() << "\n"; // 1
    } // ~TemperatureSensor called - sensor_count_ decremented
    std::cout << "Sensors: " << TemperatureSensor::total_sensors() << "\n"; // 0
    return 0;
}

Module Quiz

Module Quiz
Question 1 of 5

What is the ONLY difference between `class` and `struct` in C++?

Tip

Tip

Practice Practice Quiz OOP Part I in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Encapsulation, Constructors, const-Correctness, Static Class Members & RAII Destructors

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz OOP Part I from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz OOP Part I is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cpp code.

Topics in This Module

0/7 done