Procedural Programming vs OOP
C++ supports both procedural programming and object-oriented programming. You need to understand both because many real C++ programs use a mixture.
Procedural Programming
Procedural programming organizes code around functions.
The main question is:
What steps should the program perform?
Example:
double balance = 1000;
void deposit(double& balance, double amount) {
balance += amount;
}
This style is simple and useful for small programs.
Problem as Programs Grow
When a program becomes large, procedural code can become harder to manage.
Common problems:
- Many functions may change the same data.
- Data and behavior may be far apart.
- Changing a data structure can break many functions.
- Global data can be modified accidentally.
Procedural programming is not bad. It just becomes harder when the problem has many interacting entities.
Object-Oriented Programming
OOP organizes code around objects.
The main question is:
What entities exist, what data do they own, and what behavior should they expose?
Example:
class BankAccount {
private:
double balance = 1000;
public:
void deposit(double amount) {
balance += amount;
}
};
Now the balance belongs to the account object, and outside code cannot change it directly.
Direct Comparison
| Topic | Procedural Programming | Object-Oriented Programming |
|---|---|---|
| Main focus | functions and steps | objects and responsibilities |
| Data | often passed between functions | stored inside objects |
| Protection | depends on discipline | access specifiers help protect data |
| Reuse | function reuse | class, composition, inheritance reuse |
| Good for | small tasks, algorithms, simple scripts | systems with many related entities |
| Risk | scattered data changes | overdesigned classes if used badly |
C++ Is Multi-Paradigm
C++ does not force one style.
You can write:
- procedural C++ using functions
- object-oriented C++ using classes
- generic C++ using templates
- functional-style C++ using lambdas and algorithms
Good C++ programmers choose the style that fits the problem.
Viva Answer
Procedural programming focuses on functions and step-by-step logic. OOP focuses on objects that combine data and behavior. Procedural programming is simple for small tasks, while OOP helps organize larger systems with many related entities. C++ supports both styles.
Quick Check
- Is procedural programming always bad?
- Why can global data be dangerous?
- What does OOP group together?
- Why is C++ called multi-paradigm?