/
AirLexa
/
07
Обзор
Документация
Войти
/
AirLexa
/
07
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Lesson_03/Task_1/Task_1.cpp
53 строки
1 KB
AirLexa
урок 3
25 июн 2026, 13:54
Верифицирован
25 июн 2026, 13:54
dd3ebc1
Код
Авторство
О чём код?
#include <iostream> #include <fstream> class LogCommand { public: virtual ~LogCommand() = default; virtual void print(const std::string& message) = 0; }; // Конкретная команда: вывод в консоль class ConsoleLogCommand : public LogCommand { public: void print(const std::string& message) override { std::cout << "[CONSOLE] " << message << std::endl; } }; // Конкретная команда: вывод в файл class FileLogCommand : public LogCommand { public: explicit FileLogCommand(const std::string& filePath) : m_filePath(filePath) { } void print(const std::string& message) override { std::ofstream file(m_filePath, std::ios::app); if (!file.is_open()) throw std::runtime_error("FileLogCommand: cannot open file " + m_filePath); file << "[FILE] " << message << "\n"; } private: std::string m_filePath; }; // Функция-исполнитель команды void print(LogCommand& command, const std::string& message) { command.print(message); } int main() { ConsoleLogCommand consoleCmd; FileLogCommand fileCmd("task1_log.txt"); print(consoleCmd, "Hello from Command pattern!"); print(fileCmd, "Logged to file via Command pattern."); std::cout << std::endl; return 0; }