Object-Oriented Programming in C++

Learn object-oriented programming in C++: classes, constructors and destructors, copying, operator overloading, inheritance, virtual functions, templates, the standard library and exceptions, ending with a full design.

Level
Intermediate
Time
About 47 hours
Modules
6
Lessons
15
Exercises
48

Object-oriented programming builds a program out of objects that keep their data together with the functions that work on it. Each object protects its own rules, so large programs stay understandable and safe to change. This course teaches the idea in C++, the language where you also see exactly what each object costs in memory.

You start with what C++ adds to C, then write classes, control how objects are created, copied and destroyed, and give your own types operators. After that come inheritance and virtual functions, abstract classes, templates, the standard library and exceptions, and a final lesson that reads a complete design. You can step through every example and watch objects, pointers and calls change, and each exercise is checked against hidden tests. Every reference solution also compiles cleanly with a real C++ compiler.

What you will learn

  • Design classes that protect their data with encapsulation
  • Control how objects are built, copied and destroyed
  • Give your own types operators that read naturally
  • Use inheritance and virtual functions for polymorphism
  • Write templates and use the standard library's containers
  • Report errors with exceptions, and design a small system from classes

Skills you will gain

  • Choosing classes and member functions from a problem description
  • Managing memory with constructors, destructors and RAII
  • Writing const-correct code
  • Reading and drawing class hierarchies
  • Testing objects with edge cases such as empty, copied and self-assigned objects

Before you start

  • The C course, up to structs, pointers and malloc
  • Writing functions and loops with confidence
  • Reading short programs and predicting their output

Where it is used

  • Games, graphical interfaces and simulations
  • Banking, booking and stock-control systems
  • Libraries, frameworks and plug-in systems
  • Technical interviews that ask about classes and design

Modules

Module 1: From C to C++

What C++ adds to C for everyday code: streams for input and output, the string class, references, overloaded functions and default arguments.

  1. 1

    From C to C++

    cout and cin, using namespace std, bool and the string class: what C++ adds on top of the C you know.

  2. 2

    References, overloading and default arguments

    A reference is another name for a variable; one function name can have several versions; parameters can have default values.

Module practice and quiz →

Module 2: Classes and object lifetime

Writing classes that guard their data, and controlling the whole life of an object: how it is built, where it lives, how it is copied and how it is destroyed.

  1. 1

    Classes and objects

    A class bundles data with the functions that use it; private members keep the data safe behind public member functions.

  2. 2

    Constructors and destructors

    Constructors build an object the moment it is created, destructors clean up when it dies, and objects die in reverse order.

  3. 3

    Objects on the heap: new and delete

    new builds an object on the heap and runs its constructor; delete runs the destructor and frees it; new[] needs delete[].

  4. 4

    Copying objects: shallow and deep copies

    Copying an object that owns memory copies the pointer, not the memory; the copy constructor, assignment and the rule of three fix it.

Module practice and quiz →

Module 3: Static members and operators

Data that belongs to a whole class rather than one object, const correctness, and operator overloading, so that your own types work with +, ==, << and [].

  1. 1

    Static and const members

    A static member belongs to the class, not to each object; const member functions promise not to change the object.

  2. 2

    Operator overloading

    Teach your classes +, ==, [] and <<, as member functions or as friend functions, so they read like built-in types.

Module practice and quiz →

Module 4: Inheritance and polymorphism

Building classes on other classes, choosing the right function while the program runs with virtual functions, and describing abilities with abstract classes and interfaces.

  1. 1

    Inheritance

    A derived class is a base class plus more: shared code in one place, protected members, and the order constructors and destructors run.

  2. 2

    Virtual functions and polymorphism

    A virtual function call picks the version for the object's real class, so one loop over base pointers works for every kind of object.

  3. 3

    Abstract classes and interfaces

    A pure virtual function (= 0) makes a class abstract: a promise that every derived class keeps, and a way to write interfaces.

Module practice and quiz →

Module 5: Templates and the standard library

Writing code once for many types with templates, and using the standard library's vector, map and algorithms instead of writing your own.

  1. 1

    Templates

    Write a function or class once for any type: the compiler makes an int version, a string version, and so on.

  2. 2

    The standard library: vector, string, map and sort

    Ready-made containers and algorithms: a vector grows by itself, a map keeps keys sorted, sort works on any range.

Module practice and quiz →

Module 6: Exceptions and design

Reporting errors with exceptions and cleaning up during stack unwinding, then putting the whole course together in the design of a complete small program.

  1. 1

    Exceptions

    throw reports an error, try and catch handle it far away, and destructors still run on the way: safe error handling in C++.

  2. 2

    Project: a small library system

    Putting it together: classes with private data, inheritance and virtual functions, a vector of pointers, and exceptions for errors.

Module practice and quiz →

Projects

Final assessment →

What to learn next