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.
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
Classes and objects
A class bundles data with the functions that use it; private members keep the data safe behind public member functions.
- 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
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
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 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 [].
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
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
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
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 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.
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.
Projects
- A bank account system Build a program that manages several kinds of bank account, with deposits, withdrawals, transfers, monthly interest and a statement for each account.
- A shop inventory manager Build a program that tracks a shop's products, stock levels and sales, and produces reports.
What to learn next
- C++ Programming C++ from your first cout to references, strings, vectors, the standard library, templates and error handling, with every variable and object on screen.
- Data Structures Lists, stacks, queues, trees, heaps, hash tables and graphs, built in C node by node, with every pointer drawn.
- Competitive Programming Online judges and time limits, prefix sums, two pointers, binary search on the answer, greedy, DP and graphs with the STL, and contest math.