Please make for this code C++ header .h file with class and method prototypes: #include…
- User
Please make for this code C++ header .h file with class and method prototypes:
#include #include #include #include #include
using namespace std;
// Паттерн Singleton для логгера class Logger { private: ofstream logFile; static Logger* instance;
// Приватный конструктор для Singleton Logger() { logFile.open("log.txt", ios::out | ios::app); if (!logFile.is_open()) { throw runtime_error("Не удалось открыть файл лога."); } }public: // Получение экземпляра логгера static Logger* getInstance() { if (!instance) { instance = new Logger; } return instance; }
// Запись сообщения в лог и вывод в консоль void log(const string& message) { logFile << message << endl; cout << message << endl; } // Деструктор для закрытия файла ~Logger() { if (logFile.is_open()) { logFile.close(); } } // Удаляем возможность копирования и присваивания Logger(const Logger&) = delete; Logger& operator=(const Logger&) = delete;};
// Инициализация статической переменной instance Logger* Logger::instance = nullptr;
// Интерфейс стратегии class PaymentStrategy { public: virtual void pay(int amount) = 0; };
// Конкретные стратегии class CreditCardPayment : public PaymentStrategy { public: void pay(int amount) override { string message = "Оплачено " + to_string(amount) + " с использованием кредитной карты."; Logger::getInstance()->log(message); cout << message << endl; } };
class SberPayPayment : public PaymentStrategy { public: void pay(int amount) override { string message = "Оплачено " + to_string(amount) + " с использованием СберПэй."; Logger::getInstance()->log(message); cout << message << endl; } };
class BankTransferPayment : public PaymentStrategy { public: void pay(int amount) override { string message = "Оплачено " + to_string(amount) + " с использованием банковского перевода."; Logger::getInstance()->log(message); cout << message << endl; } };
// Контекст, использующий стратегию class ShoppingCart { private: PaymentStrategy* paymentStrategy;
public: ShoppingCart(PaymentStrategy* strategy) : paymentStrategy(strategy) {}
void checkout(int amount) { paymentStrategy->pay(amount); }};
// Функция для выбора задачи (1 - оплата, 2 - автомобиль) int chooseTask() { int task; cout << "Выберите задачу:" << endl; cout << "1. Оплата" << endl; cout << "2. Работа с автомобилем" << endl; cout << "3. Выход" << endl; cout << "Ваш выбор: "; cin >> task; return task; }
// Функция для выбора способа оплаты PaymentStrategy* choosePaymentMethod() { int choice; cout << "Выберите способ оплаты:" << endl; cout << "1. Кредитная карта" << endl; cout << "2. СберПэй" << endl; cout << "3. Банковский перевод" << endl; cout << "Ваш выбор: "; cin >> choice;
- Assistant
- User
There are too many files. Please leave Logger.h and collect the remaining class prototypes in one .h file. Add comments in code on russian
- Assistant
- User
I get errors when try to compile program.cpp with your .h files in #include:
- Assistant
The error messages you are seeing indicate that there is a redefinition of classes. This suggests that you have included class definitions both in your and in the header files I provided (like , ), resulting in duplicate definitions.