C++ ยท Chapter 30 of 49

C++ OOP Introduction

Object-Oriented Programming (OOP) organises code around 'objects' that bundle data (attributes) and behaviour (methods) together. C++ was one of the earliest mainstream languages to bring OOP to systems programming.

The four pillars of OOP are encapsulation, inheritance, polymorphism, and abstraction โ€” each covered in upcoming topics, building toward writing well-structured, reusable C++ programs.

Objects and classes

A class is a blueprint; an object is a concrete instance created from that blueprint. For example, a `Car` class might describe attributes like speed and methods like accelerate().

Why OOP?

OOP models real-world entities naturally, encourages code reuse via inheritance, and hides internal details via encapsulation, making large codebases more maintainable.

Example 1 (cpp)
class Dog {
public:
    std::string name = "Rex";
    void bark() { std::cout << name << " says Woof!"; }
};
int main() {
    Dog d;
    d.bark();
}
Output
Rex says Woof!

Dog is a class; d is an object (instance) of that class.

Key points

  • OOP bundles data and behaviour into objects.
  • A class is a blueprint; an object is an instance.
  • The four pillars: encapsulation, inheritance, polymorphism, abstraction.
  • OOP improves reuse and maintainability in large programs.
๐Ÿ’ก Note: C++ supports both procedural and object-oriented styles โ€” you choose the right tool for each part of a program.

๐Ÿ“ Quick Quiz

1. A class is best described as:

2. Which is NOT one of the four OOP pillars?

3. An object is: