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.
class Dog {
public:
std::string name = "Rex";
void bark() { std::cout << name << " says Woof!"; }
};
int main() {
Dog d;
d.bark();
}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.
