Introduction and Lesson Goal

Today's mission involves using multiple Object-Oriented Programming (OOP) principles to tackle complex tasks in C++. When principles like Encapsulation, Abstraction, Polymorphism, and Composition are blended, the resulting code becomes streamlined and easier to manage.

Our goal is to dissect two real-world examples, gaining insights into how these principles can seamlessly orchestrate solutions in C++.

Real-life Example 1: Building an Online Library System

Let's design an online library system using C++ to reinforce our understanding of Encapsulation and Polymorphism. Encapsulation will help us protect the attributes of books, members, and transactions, ensuring they are accessible in a controlled manner. Polymorphism will demonstrate its power by enabling a single interface for different forms, such as digital and print versions of books.

#include <iostream>
#include <vector>

// Base class for different types of library users
class Member {
public:
    Member(std::string name) : name(name) {}

    void check_out_book(class Book* book);

private:
    std::string name;
};

// Base class for different types of books
class Book {
public:
    Book(std::string title) : title(title) {}
    virtual std::string get_book_type() = 0;
    std::string get_title() { return title; }

private:
    std::string title;
};

// Inherits from Book, represents a digital book
class DigitalBook : public Book {
public:
    DigitalBook(std::string title) : Book(title) {}

    std::string get_book_type() override {
        return "Digital";
    }
};

// Inherits from Book, represents a physical book
class PhysicalBook : public Book {
public:
    PhysicalBook(std::string title) : Book(title) {}

    std::string get_book_type() override {
        return "Physical";
    }
};

// Implementation of the checkout method
void Member::check_out_book(Book* book) {
    std::cout << name << " checked out " << book->get_book_type() << " book " << book->get_title() << ".\n";
}

// Library class that manages members and books
class Library {
public:
    void add_member(Member* member) {
        members.push_back(member);
    }

    void add_book(Book* book) {
        books.push_back(book);
    }

private:
    std::vector<Member*> members;
    std::vector<Book*> books;
};

int main() {
    Library my_library;

    Member alice("Alice");
    Member bob("Bob");

    my_library.add_member(&alice);
    my_library.add_member(&bob);

    DigitalBook digital_book("The C++ Handbook");
    PhysicalBook physical_book("Learning C++ Design Patterns");

    my_library.add_book(&digital_book);
    my_library.add_book(&physical_book);

    alice.check_out_book(&digital_book);  // Prints: Alice checked out Digital book The C++ Handbook.
    bob.check_out_book(&physical_book);   // Prints: Bob checked out Physical book Learning C++ Design Patterns.

    return 0;
}

In this code snippet, Encapsulation is clearly observed through the class structures and the controlled access to their attributes. Polymorphism is vividly illustrated by how both DigitalBook and PhysicalBook classes inherit from the Book class but provide their implementations of the get_book_type method. This setup allows objects of DigitalBook and PhysicalBook to be used interchangeably when a book's type needs to be identified, demonstrating polymorphism's capability to work with objects of different classes through a common interface.

  • Encapsulation ensures that details about members and books are well-contained within their respective classes.
  • Polymorphism showcases flexibility by treating different book types uniformly, making the system more adaptive and scalable.
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal