Exercise: AveragingSensor_nopoly (Non-Polymorphic)

Requirement

Implement a sensor class AveragingSensor_nopoly which is not a real sensor. Rather, it calculates its temperature as the average from a configurable set of sensors that it knows.

#include <gtest/gtest.h>

#include <sensor-mock-nopoly.h>
#include <sensor-avg-nopoly.h>


TEST(sensor_avg_nopoly_suite, basic)
{
    MockSensor_nopoly s1(3);
    MockSensor_nopoly s2(4);

    AveragingSensor_nopoly avg;                        // <--- uses-many Sensor
    avg.add(&s1);                                      // <--- add one sensor
    avg.add(&s2);                                      // <--- add another sensor

    ASSERT_FLOAT_EQ(avg.get_temperature(), 3.5);       // <--- calculates average of all measurements
}
#include <gtest/gtest.h>

#include <sensor-mock-nopoly.h>
#include <sensor-avg-nopoly.h>


TEST(sensor_avg_nopoly_suite, change_temperatures)
{
    MockSensor_nopoly s1(3);
    MockSensor_nopoly s2(4);

    AveragingSensor_nopoly avg;
    avg.add(&s1);
    avg.add(&s2);

    ASSERT_FLOAT_EQ(avg.get_temperature(), 3.5);

    s1.set_temperature(10);
    s2.set_temperature(20);

    ASSERT_FLOAT_EQ(avg.get_temperature(), 15);
}

Topics Covered

cluster_cxx03 C++ cluster_cxx03_data_encapsulation Data Encapsulation cluster_linux Linux cluster_linux_basics Linux Basics cluster_linux_basics_shell The Shell (Bash - “Bourne Again Shell”) cluster_linux_basics_intro Introduction: Concepts and Terminology cluster_linux_toolchain Toolchain, And Cross Development cluster_cxx C++: Miscellaneous Live-Hacking cluster_cxx_exercises C++ Exercises cluster_c The C Programming Language cluster_c_introduction Introduction cxx03_introduction Introduction c_introduction_installation Installation cxx03_introduction->c_introduction_installation cxx03_data_encapsulation_ctor_custom Custom Constructor cxx03_data_encapsulation_classes_objects Classes and Objects cxx03_data_encapsulation_ctor_custom->cxx03_data_encapsulation_classes_objects cxx03_data_encapsulation_classes_objects->cxx03_introduction cxx03_data_encapsulation_c Object Oriented Programming In Good Ol’ C cxx03_data_encapsulation_classes_objects->cxx03_data_encapsulation_c cxx03_data_encapsulation_c->cxx03_introduction cxx03_data_encapsulation_initializer_list Constructors: Member Initialization cxx03_data_encapsulation_initializer_list->cxx03_data_encapsulation_ctor_custom linux_basics_shell_paths Absolute and Relative Paths linux_basics_shell_commandline Commandline linux_basics_shell_paths->linux_basics_shell_commandline linux_basics_shell_cwd Current Working Directory linux_basics_shell_cwd->linux_basics_shell_paths linux_basics_shell_cwd->linux_basics_shell_commandline linux_basics_intro_process Processes, Scheduling, Address Spaces linux_basics_shell_cwd->linux_basics_intro_process linux_basics_shell_file_dir_create_rm Creating And Removing Files and Directories linux_basics_shell_file_dir_create_rm->linux_basics_shell_paths linux_basics_shell_file_dir_create_rm->linux_basics_shell_cwd linux_basics_intro_overview Overview linux_basics_shell_commandline->linux_basics_intro_overview linux_basics_intro_process->linux_basics_intro_overview linux_toolchain_static_library Object Code Archives/Static Libraries linux_toolchain_separate_compilation Zooming In: Separate Compilation, and Linking Statically linux_toolchain_static_library->linux_toolchain_separate_compilation linux_toolchain_cmake_local CMake: Local Build linux_toolchain_cmake_local->linux_toolchain_static_library linux_toolchain_basics Toolchain: Basics linux_toolchain_separate_compilation->linux_toolchain_basics linux_toolchain_basics->linux_basics_shell_file_dir_create_rm cxx_exercises_sensor_mock_nopoly Exercise: MockSensor_nopoly (Non-Polymorphic) cxx_exercises_sensor_mock_nopoly->cxx03_data_encapsulation_classes_objects cxx_exercises_sensor_mock_nopoly->linux_toolchain_cmake_local cxx_exercises_sensor_avg_nopoly Exercise: AveragingSensor_nopoly (Non-Polymorphic) cxx_exercises_sensor_avg_nopoly->cxx03_data_encapsulation_classes_objects cxx_exercises_sensor_avg_nopoly->cxx03_data_encapsulation_initializer_list cxx_exercises_sensor_avg_nopoly->linux_toolchain_cmake_local cxx_exercises_sensor_avg_nopoly->cxx_exercises_sensor_mock_nopoly