Loading lang_cpp_ex_0501...
->, *).
struct Room { std::string name; int capacity; int computers; };
struct Course { std::string title; std::string teacher; std::string topic; };
class TimeSlot { public:
TimeSlot(std::string code) : code_{std::move(code)} // ...to be completed... { // nothing more to be done }
const std::string & code() const { return code_; }
// ...to be completed...
private: std::string code_; // ...to be completed... };
void test_TimeSlot() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; std::vector<alloc::TimeSlot> today; for(const auto &code: {"H1", "H2", "H3", "H4", "H5", "H6", "H7"}) { today.emplace_back(code); } // ...provide today[1] with // a room made of {"2E-204", 72, 0} and // a course made of {"maths", "P. Redou", "derivatives"}... // ...provide today[2] with // a room made of {"B015", 25, 25} and // a course made of {"programming", "G. Desmeulles", "project"}... // ...provide today[4] with // a course made of {"programming", "E. Bevacqua", "composites"}... // ...provide today[5] with // a room made of {"D103", 24, 12}... for(const auto &time_slot: today) { std::cout << time_slot.code() << ":\n"; // ...if this time slot has a room, then display its properties... // ...if this time slot has a course, then display its properties... } }
struct Gpu { std::string vendor; std::string model; int cores; };
struct SoundSystem { std::string vendor; int channels; double power; };
class Computer { public:
Computer(std::string host_name) : host_name_{std::move(host_name)} // ...to be completed... { // nothing more to be done }
const std::string & host_name() const { return host_name_; }
// ...to be completed...
private: std::string host_name_; // ...to be completed... };
void test_Computer() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; std::vector<s4prc::Computer> cluster; for(const auto &host_name: {"alpha", "bravo", "charlie", "delta", "echo"}) { cluster.emplace_back(host_name); } // ...provide cluster[1] with // a gpu made of {"NVIDIA", "GeForce GTX TITAN X", 3072} and // a sound system made of {"Cyber Acoustics", 3, 30.0}... // ...provide cluster[2] with // a gpu made of {"NVIDIA", "Quadro K2000M", 384} and // a sound system made of {"Logitech", 2, 5.0}... // ...provide cluster[3] with // a sound system made of {"Creative", 6, 22.0}... // ...provide cluster[4] with // a gpu made of {"AMD", "Radeon RX 580X", 2304}... for(const auto &computer: cluster) { std::cout << computer.host_name() << ":\n"; // ...if this computer has a gpu, then display its properties... // ...if this computer has a sound system, then display its properties... } }
//----------------------------------------------------------------------------
#ifndef ALLOC_SMART_POINTER_HPP #define ALLOC_SMART_POINTER_HPP
#include <string> #include <memory>
namespace alloc {
struct Room { std::string name; int capacity; int computers; };
struct Course { std::string title; std::string teacher; std::string topic; };
class TimeSlot { public:
TimeSlot(std::string code) : code_{std::move(code)} , room_{} , course_{} { // nothing more to be done }
const std::string & code() const { return code_; }
void set_room(Room room) { room_=std::make_unique<Room>(std::move(room)); }
void set_room(std::unique_ptr<Room> room) // alternative { room_=std::move(room); }
const Room * room() const { return room_.get(); }
void set_course(Course course) { course_=std::make_unique<Course>(std::move(course)); }
void set_course(std::unique_ptr<Course> course) // alternative { course_=std::move(course); }
const Course * course() const { return course_.get(); }
private: std::string code_; std::unique_ptr<Room> room_; std::unique_ptr<Course> course_; };
struct Gpu { std::string vendor; std::string model; int cores; };
struct SoundSystem { std::string vendor; int channels; double power; };
class Computer { public:
Computer(std::string host_name) : host_name_{std::move(host_name)} , gpu_{} , sound_system_{} { // nothing more to be done }
const std::string & host_name() const { return host_name_; }
void set_gpu(Gpu gpu) { gpu_=std::make_unique<Gpu>(std::move(gpu)); }
void set_gpu(std::unique_ptr<Gpu> gpu) // alternative { gpu_=std::move(gpu); }
const Gpu * gpu() const { return gpu_.get(); }
void set_sound_system(SoundSystem sound_system) { sound_system_=std::make_unique<SoundSystem>(std::move(sound_system)); }
void set_sound_system(std::unique_ptr<SoundSystem> sound_system) // alternative { sound_system_=std::move(sound_system); }
const SoundSystem * sound_system() const { return sound_system_.get(); }
private: std::string host_name_; std::unique_ptr<Gpu> gpu_; std::unique_ptr<SoundSystem> sound_system_; };
} // namespace alloc
#endif // ALLOC_SMART_POINTER_HPP
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "smart_pointer.hpp"
#include <iostream> #include <vector>
void test_TimeSlot() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; std::vector<alloc::TimeSlot> today; for(const auto &code: {"H1", "H2", "H3", "H4", "H5", "H6", "H7"}) { today.emplace_back(code); } today[1].set_room({"2E-204", 72, 0}); today[1].set_course({"maths", "P. Redou", "derivatives"}); today[2].set_room({"B015", 25, 25}); today[2].set_course({"programming", "G. Desmeulles", "project"}); today[4].set_course({"programming", "E. Bevacqua", "composites"}); today[5].set_room({"D103", 24, 12}); for(const auto &time_slot: today) { std::cout << time_slot.code() << ":\n"; if(const auto *room=time_slot.room()) { std::cout << " " << room->name << ", " << room->capacity << ", " << room->computers << '\n'; } if(const auto *course=time_slot.course()) { std::cout << " " << course->title << ", " << course->teacher << ", " << course->topic << '\n'; } } }
void test_Computer() { std::cout << "\n~~~~ " << __func__ << "() ~~~~\n"; std::vector<alloc::Computer> cluster; for(const auto &host_name: {"alpha", "bravo", "charlie", "delta", "echo"}) { cluster.emplace_back(host_name); } cluster[1].set_gpu({"NVIDIA", "GeForce GTX TITAN X", 3072}); cluster[1].set_sound_system({"Cyber Acoustics", 3, 30.0}); cluster[2].set_gpu({"NVIDIA", "Quadro K2000M", 384}); cluster[2].set_sound_system({"Logitech", 2, 5.0}); cluster[3].set_sound_system({"Creative", 6, 22.0}); cluster[4].set_gpu({"AMD", "Radeon RX 580X", 2304}); for(const auto &computer: cluster) { std::cout << computer.host_name() << ":\n"; if(const auto *gpu=computer.gpu()) { std::cout << " " << gpu->vendor << ", " << gpu->model << ", " << gpu->cores << '\n'; } if(const auto *sound_system=computer.sound_system()) { std::cout << " " << sound_system->vendor << ", " << sound_system->channels << ", " << sound_system->power << '\n'; } } }
int main() { test_TimeSlot(); test_Computer(); return 0; }
//----------------------------------------------------------------------------