Sign in with Google to keep private notes

C++ Object Oriented Programming

Derived objects in OOP can be thought of as independent libraries that all have the same API.

What ties a derived class to various implementations is the base class. The base class constrains what the client has to do.

#include <iostream>
#include <memory>

using namespace std;

struct Logger 
{
    virtual void LogMessage(char const* message) = 0;
    virtual ~Logger() = default; 
};

struct ConsoleLogger final : Logger
{
    virtual void LogMessage(char const* message) override {
        std::cout << message << '\n';
    }
};


int main () {
    auto logger{std::make_unique<ConsoleLogger>()};
    Logger* logger_ptr{logger.get()};
    logger_ptr->LogMessage("Hello, world");
}

Will add more, when this finally becomes relevant.

Articles to read next