example3_partial_implementation.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

// Abstract class acting as an interface
class Logger {
public:
    virtual void logInfo(const std::string& message) = 0;
    virtual void logWarning(const std::string& message) = 0;
    virtual void logError(const std::string& message) = 0;
};

class ConsoleLogger : public Logger {
public:
    void logInfo(const std::string& message) override {
        std::cout << "INFO: " << message << std::endl;
    }

    void logWarning(const std::string& message) override {
        std::cout << "WARNING: " << message << std::endl;
    }

    void logError(const std::string& message) override {
        std::cout << "ERROR: " << message << std::endl;
    }
};

class FileLogger : public Logger {
public:
    void logInfo(const std::string& message) override {
        // Imagine this writes to a file
        std::cout << "Writing INFO to file: " << message << std::endl;
    }

    void logWarning(const std::string& message) override {
        // Imagine this writes to a file
        std::cout << "Writing WARNING to file: " << message << std::endl;
    }

    void logError(const std::string& message) override {
        // Imagine this writes to a file
        std::cout << "Writing ERROR to file: " << message << std::endl;
    }
};

int main() {
    ConsoleLogger consoleLogger;
    FileLogger fileLogger;

    consoleLogger.logInfo("Application started.");
    fileLogger.logError("File not found.");

    return 0;
}
Back to abstract_class