Skip to content

Latest commit

 

History

History
78 lines (58 loc) · 5.04 KB

README.md

File metadata and controls

78 lines (58 loc) · 5.04 KB

Object-Oriented Programming in C++

Object-oriented programming is a way of organising code around things (objects that bundle data with the behaviour that belongs to it) instead of around loose functions and global variables. Done well, it makes large programs easier to understand, change and test, because each object guards its own data and talks to the rest of the system through a small, clear interface.

This repository builds that idea up one concept at a time in modern C++17, then brings everything together in a multi-file Library Management System.

The four pillars

Pillar In one sentence Where to see it
Encapsulation Hide an object's data and only allow safe, validated changes. 03-encapsulation
Abstraction Show what an object does, hide how it does it. 07-abstraction
Inheritance Build a new class on top of an existing one. 05-inheritance
Polymorphism One interface, many behaviours, chosen at compile time or run time. 06-polymorphism

What's inside

# Topic What it covers
01 Classes & objects Blueprint vs instance, access specifiers, member functions, const methods, ->
02 Constructors & destructors Default, parameterised, delegating and copy constructors; object lifetime; deep copy and the Rule of Five (move semantics)
03 Encapsulation Private data, validating setters, this, method chaining, class invariants
04 Static members Class-wide data and functions, object counting, shared configuration
05 Inheritance Single, multilevel, hierarchical and multiple inheritance; protected; construction order
06 Polymorphism Overloading, virtual/override/final, virtual destructors, static vs dynamic binding, dynamic_cast
07 Abstraction Pure virtual functions, abstract base classes, interfaces
08 Operator overloading A complete Fraction type: arithmetic, comparisons, ++, stream << / >>, explicit conversion
09 Friend functions Friend functions across two classes, friend classes, friend operators
10 Object relationships Composition, aggregation and association, and how each affects object lifetime
11 Templates Function and class templates, non-type parameters, specialisation, a generic Stack<T>
12 Exception handling try/catch/throw, custom exception hierarchies, rethrowing, RAII during stack unwinding
13 Smart pointers unique_ptr, shared_ptr, weak_ptr and breaking reference cycles
14 Design patterns Singleton, Factory, Observer and Strategy, each with a realistic example

Project: Library Management System

project/library-management-system is a menu-driven console app split into headers and source files, the way real C++ projects are organised.

Book.h / Book.cpp          a book and its issue status
Member.h / Member.cpp      abstract Member, with StudentMember (3 books) and FacultyMember (10 books)
Library.h / Library.cpp    owns books and members, enforces the rules, throws LibraryError
main.cpp                   the menu

It uses nearly every concept above: encapsulated classes, an abstract base class with polymorphic borrowing limits, composition, std::unique_ptr, std::map, and exceptions for every broken rule (issuing a book twice, going over a borrowing limit, unknown IDs).

Running the code

You need a C++17 compiler: g++, clang++ or Visual Studio.

git clone https://github.com/BigB-904/object-oriented-programming-cpp.git
cd object-oriented-programming-cpp
make                                   # builds every example into build/
./build/06-polymorphism/polymorphism   # run one
./build/library-management-system      # run the project

Without make:

g++ -std=c++17 08-operator-overloading/fraction.cpp -o fraction
g++ -std=c++17 project/library-management-system/*.cpp -o library

Everything builds cleanly with -Wall -Wextra -pedantic.

Ideas to extend

  • Add due dates and late fines to the library (hint: a Loan class composed of a Book*, a Member* and a date).
  • Save and load the library from a file, so data survives between runs.
  • Add a Matrix class with overloaded *, + and [].
  • Implement the Decorator or Command pattern next to the existing four.

License

MIT