Introduction to OOP in C++
Object-Oriented Programming (OOP) is a programming style that organizes code around objects — real-world entities — rather than just functions and logic. C++ is a multi-paradigm language that fully supports OOP, and it is one of the most important aspects of C++ programming.
What is OOP?
In the real world, everything is an object — a car, a bank account, a student. Each object has:
- Properties (Attributes) — What the object has. A car has a color, engine, and speed.
- Behaviors (Methods) — What the object does. A car can accelerate, brake, and turn.
OOP models software the same way — classes define the blueprint, and objects are the real instances created from that blueprint.
Four Pillars of OOP
| Pillar | Simple Meaning |
|---|---|
| Encapsulation | Bundle data and functions together; hide internal details |
| Inheritance | A class can reuse and extend another class's properties |
| Polymorphism | Same function name, different behavior depending on context |
| Abstraction | Show only what's necessary; hide complex implementation |
Procedural vs Object-Oriented Approach
Procedural approach (no OOP):
// All data and logic are separate
string studentName = "Asha";
int studentAge = 20;
void printStudent(string name, int age) {
cout << name << " - " << age << endl;
}
Object-Oriented approach:
class Student {
public:
string name;
int age;
void print() {
cout << name << " - " << age << endl;
}
};
In OOP, related data and functions are grouped inside a class. This leads to cleaner, more maintainable, and reusable code.
Class and Object — Quick Preview
A class is a blueprint. An object is an instance of that class — a real working entity.
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string brand;
int speed;
void showInfo() {
cout << brand << " runs at " << speed << " km/h" << endl;
}
};
int main() {
Car car1; // create an object
car1.brand = "Toyota";
car1.speed = 180;
car1.showInfo(); // use the object
Car car2;
car2.brand = "BMW";
car2.speed = 250;
car2.showInfo();
return 0;
}
Output:
Toyota runs at 180 km/h
BMW runs at 250 km/hBenefits of OOP
- Modularity — Code is divided into classes, each handling its own responsibility.
- Reusability — Existing classes can be extended and reused without rewriting.
- Maintainability — Changes in one class don't break unrelated parts of the program.
- Security — Encapsulation hides sensitive data from outside access.
- Real-world modeling — OOP naturally maps to how we think about the world.
Real-World OOP Examples
| Real World | Class | Objects |
|---|---|---|
| Bank | BankAccount | Account of Alice, Account of Bob |
| Library | Book | "C++ Primer", "Clean Code" |
| School | Student | student1, student2 |
| Game | Player | player1 (hero), player2 (villain) |
Key Takeaways
- OOP organizes code around objects that represent real-world entities.
- A class is a blueprint; an object is a concrete instance of it.
- The four pillars of OOP are: Encapsulation, Inheritance, Polymorphism, and Abstraction.
- OOP makes code more organized, reusable, and maintainable.
- C++ fully supports OOP alongside procedural programming.
