C++ OOP
Object-oriented programming (OOP) is a way of organising code around objects — bundles of data together with the functions that operate on that data. C++ was one of the languages that made OOP mainstream, and its class system is at the heart of the standard library.
Procedural vs object-oriented
In procedural code, data and functions are separate. You pass the data around to whichever function needs it:
#include <iostream>
using namespace std;
struct Account {
double balance;
};
void deposit(Account& a, double amount) { a.balance += amount; }
void printBalance(const Account& a) { cout << "Balance: " << a.balance << "\n"; }
int main() {
Account acc{100};
deposit(acc, 50);
acc.balance = -1000000; // nothing stops this!
printBalance(acc);
}Balance: -1e+06
Nothing prevents other code from putting the account in a nonsensical state. In object-oriented code, the data and the operations live together in a class, and the class decides which changes are allowed:
#include <iostream>
using namespace std;
class Account {
public:
void deposit(double amount) {
if (amount > 0) balance += amount;
}
void print() const {
cout << "Balance: " << balance << "\n";
}
private:
double balance = 100; // only the class's own functions can touch this
};
int main() {
Account acc;
acc.deposit(50);
acc.deposit(-999); // rejected by the class's own rule
// acc.balance = -1000000; // error: 'balance' is private
acc.print();
}Balance: 150
Classes and objects
A class is a blueprint; an object is a concrete thing built from that blueprint. One Car class can produce many car objects, each with its own colour and mileage.
| Class (blueprint) | Objects (instances) |
|---|---|
Car |
a red Volvo, a blue Fiat, a white Tesla |
Student |
Ana, Ben, Chloe |
BankAccount |
your savings account, your checking account |
The four pillars of OOP
| Pillar | Idea | C++ feature |
|---|---|---|
| Encapsulation | Keep data private; expose a safe interface | private, public, member functions |
| Abstraction | Users see what an object does, not how | Clean public methods, abstract classes |
| Inheritance | Build new classes on top of existing ones | class Dog : public Animal |
| Polymorphism | One interface, many implementations | virtual functions, overriding |
Each pillar gets its own lesson in this section.
A first taste of all four
Don't worry about understanding every line yet — this is a preview of where the section is heading:
#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;
class Shape { // abstraction
public:
virtual ~Shape() = default;
virtual double area() const = 0;
virtual string name() const = 0;
};
class Square : public Shape { // inheritance
public:
explicit Square(double s) : side(s) {}
double area() const override { return side * side; }
string name() const override { return "square"; }
private:
double side; // encapsulation
};
class Circle : public Shape {
public:
explicit Circle(double r) : radius(r) {}
double area() const override { return 3.14159 * radius * radius; }
string name() const override { return "circle"; }
private:
double radius;
};
int main() {
vector<unique_ptr<Shape>> shapes;
shapes.push_back(make_unique<Square>(3));
shapes.push_back(make_unique<Circle>(1));
for (const auto& s : shapes) { // polymorphism
cout << s->name() << ": " << s->area() << "\n";
}
}square: 9
circle: 3.14159
Why OOP?
- Safety — invariants (like "balance is never negative") are enforced in one place.
- Reuse — a well-designed class can be used across many programs.
- Modelling — objects map naturally to things in the problem domain.
- Maintainability — the inside of a class can change without breaking code that uses it.
Note: C++ is a multi-paradigm language. OOP is one tool among several — you will also use generic programming (templates) and functional ideas (lambdas). Good C++ code mixes them where each fits best.
Test yourself
What is the relationship between a class and an object?
- They are the same thing
- A class is a blueprint, an object is an instance of it
- An object is a blueprint, a class is an instance
- A class is a function
Which OOP pillar is about hiding data behind a public interface?
- Inheritance
- Polymorphism
- Encapsulation
- Recursion
Which C++ keyword enables run-time polymorphism?
- static
- virtual
- inline
- template