What is OOP?
Object-Oriented Programming (OOP) is a way of writing programs by organizing code around objects.
An object combines two things:
- State: the data the object stores.
- Behavior: the functions that operate on that data.
In C++, we usually create objects from classes.
class Student {
private:
std::string name; // state
int marks; // state
public:
void display(); // behavior
};
Simple Definition
OOP means:
Model a program as a collection of objects that store data and perform actions.
Instead of thinking only:
What functions do I need?
OOP asks:
What objects exist in this problem, what data do they own, and what actions can they perform?
Real-World Example
Think about a BankAccount.
| Part | Example |
|---|---|
| State | account number, holder name, balance |
| Behavior | deposit, withdraw, show balance |
In a procedural program, balance might be passed around to many separate functions.
deposit(balance, 500);
withdraw(balance, 200);
In OOP, the object controls its own data.
account.deposit(500);
account.withdraw(200);
This is easier to understand because the behavior belongs to the object.
Class and Object
A class is a blueprint.
An object is a real instance made from that blueprint.
Example:
Studentis a class.riaz,mina, andkarimcan be objects of theStudentclass.
Student riaz;
Student mina;
Why OOP Is Useful
OOP helps when a program becomes large.
1. Organization
Related data and functions stay together.
2. Data Protection
Objects can hide internal data and expose safe public functions.
3. Reuse
Existing classes can be reused through composition and inheritance.
4. Easier Change
If one class is designed well, changes inside it do not need to break the rest of the program.
Important Warning
OOP does not automatically make code good.
Bad OOP can still create:
- huge classes
- confusing inheritance
- unnecessary complexity
- poor encapsulation
Good OOP means designing classes with clear responsibilities.
Viva Answer
OOP is a programming paradigm where programs are designed using objects. An object combines data and behavior. In C++, classes are used as blueprints for creating objects. OOP helps organize large programs through encapsulation, inheritance, polymorphism, and abstraction.
Quick Check
Answer these without looking:
- What is state?
- What is behavior?
- What is the difference between a class and an object?
- Why is
account.deposit(500)more object-oriented thandeposit(balance, 500)?