Loading lessons...
Introduction to Object-Oriented Programming
Introduction to Object-Oriented Programming
Object-Oriented Programming (OOP) is a way of writing programs around objects. An object bundles data (its attributes) together with the behavior that acts on that data (its methods). Instead of scattering data across free functions, you group related things into one self-contained unit.
Modeling the real world
OOP looks at the world in terms of things and what they can do. A Car has attributes like brand, model, and color, and it can do things like start() and drive(). The car is one cohesive object, not dozens of disconnected variables.
Classes and objects
- A class is the blueprint.
- An object is a concrete instance built from that blueprint.
class Car {
public:
string brand;
};
The class defines the shape; each object you create gets its own copy of the members.
Attributes and methods
An attribute (data member) stores information. A method (member function) is behavior the object can perform. Together they form the two halves of OOP.
Benefits of OOP
- Organization: related data and code live in one place.
- Reuse: you create objects from the same class over and over.
- Maintainability: a change is local, so a fix rarely breaks distant parts.
- Protection: a class decides what the outside world may see.
Why C++ is OOP-capable
C++ supports classes so well that almost every modern C++ program builds things around classes and objects. Nothing forces you to use them - you can write procedural C++ too - but classes give you a proven way to keep complex programs organized.
TL;DR
- OOP bundles data (attributes) and behavior (methods) into objects.
- A class is the blueprint; an object is the instance.
- Benefits: organization, reuse, maintainability, and protection.
- C++ is designed to work well with classes.