Loading lessons...
The Object Oriented Programming Paradigm
The Object Oriented Programming Paradigm
A programming paradigm is a style of organising a program: what the building blocks are and how they work together. C was written in the procedural style, where a program is a list of functions acting on shared data. C++ adds the object oriented style, where a program is a set of objects that each hold their own data and the functions that work on it.
This lesson explains why the object oriented paradigm was invented, how it differs from procedure oriented programming, and what its main features are.
1. Why a New Paradigm?
Software keeps growing. A program of a few hundred lines is easy to manage in any style. A program of a million lines, changed by many people over many years, is not. Programmers found that the procedural style made large programs:
- hard to understand, because data and the functions using it are far apart;
- hard to change, because a change to one global data structure forces changes in every function that touches it;
- error-prone, because any function can modify any global data by mistake;
- hard to reuse, because functions depend on the exact layout of global data.
The object oriented paradigm tackles these problems by binding data and the functions that use it into one unit (an object) and hiding that data from the rest of the program.
2. Procedure Oriented Programming (POP)
In POP the problem is seen as a sequence of tasks such as reading, calculating and printing. Each task is written as a function. The design is top-down: the main task is broken into smaller and smaller functions.
Procedure oriented program: functions sharing global data
Characteristics of POP:
- Emphasis on doing things: the algorithm comes first, the data second.
- Large programs are divided into functions.
- Most functions share global data, which moves openly from function to function.
- Functions transform data from one form to another.
- Top-down design is used.
Its main weakness is the treatment of data. Global data can be changed by any function, so a bug in one function can corrupt data used by all the others. And when a new data structure is added, every function using the old one must be rewritten.
A short procedural program in C style:
#include <iostream>
using namespace std;
// Global data: any function can read or change it.
double balance = 1000;
void deposit(double amount) {
balance = balance + amount;
}
void withdraw(double amount) {
balance = balance - amount;
}
int main() {
deposit(500);
withdraw(200);
balance = -99999; // nothing stops this mistake
cout << "Balance: " << balance << endl;
return 0;
}Output:
Balance: -999993. Object Oriented Programming (OOP)
OOP treats data as the most important element and does not let it flow freely. Data is tied to the functions that operate on it, inside an object, and is protected from accidental change by outside functions.
Definition (Balagurusamy). Object oriented programming is an approach that provides a way of modularising programs by creating partitioned memory areas for both data and functions, which can be used as templates for creating copies of such modules on demand.
Object oriented program: objects with their own data and functions
Characteristics of OOP:
- Emphasis is on data rather than procedure.
- Programs are divided into objects.
- Data structures are designed to describe the objects.
- Functions that operate on an object's data are tied to it in the same data structure.
- Data is hidden and cannot be accessed by external functions.
- Objects communicate through functions (by sending messages).
- New data and functions can be added easily when needed.
- Bottom-up design: small, well-tested classes are combined into bigger ones.
The same bank account written in the object oriented style:
#include <iostream>
using namespace std;
class Account {
private:
double balance; // hidden data
public:
Account(double b) {
balance = b;
}
void deposit(double amount) {
balance = balance + amount;
}
void withdraw(double amount) {
if (amount <= balance) { // the object protects its own data
balance = balance - amount;
} else {
cout << "Insufficient funds" << endl;
}
}
double getBalance() {
return balance;
}
};
int main() {
Account a(1000);
a.deposit(500);
a.withdraw(200);
a.withdraw(5000);
// a.balance = -99999; // error: balance is private
cout << "Balance: " << a.getBalance() << endl;
return 0;
}Output:
Insufficient funds
Balance: 1300The data balance can now be changed only through deposit and withdraw, and those functions enforce the rules.
4. POP vs OOP
| Basis | Procedure oriented (POP) | Object oriented (OOP) |
|---|---|---|
| Building block | Function | Object |
| Emphasis | Algorithm (doing things) | Data |
| Design approach | Top-down | Bottom-up |
| Data | Mostly global, shared by all functions | Hidden inside objects |
| Data security | Low: any function can change data | High: access only through member functions |
| Communication | Function calls with shared data | Objects send messages to each other |
| Adding new data | Many functions must change | Add a new class or member |
| Reuse | Limited | Inheritance and composition |
| Access specifiers | None | private, protected, public |
| Overloading | Not supported (C) | Functions and operators can be overloaded |
| Examples | C, Pascal, FORTRAN, COBOL | C++, Java, C#, Python, Smalltalk |
5. Programming Paradigms at a Glance
Programming styles evolved as programs grew:
| Paradigm | Idea | Example language |
|---|---|---|
| Monolithic | the whole program is one block, using goto | assembly, early BASIC |
| Procedural | the program is a sequence of procedures | FORTRAN, COBOL |
| Structured | modules and functions, no goto; top-down | C, Pascal |
| Object oriented | objects bundle data and functions | Smalltalk, C++, Java |
C++ is a multi-paradigm language: it supports procedural, object oriented and generic (template) programming. That is why a C++ program can still contain plain functions like C.
Key points to remember
- POP: functions are the building blocks, data is global, design is top-down.
- OOP: objects are the building blocks, data is hidden, design is bottom-up.
- OOP's main gain is data security and reuse: data is changed only through the object's own functions.
- C++ supports both styles (it is a multi-paradigm language).
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is object oriented programming? How is it different from procedure oriented programming? (5 Marks)
- Write: the definition, the 8 characteristics of OOP, and the POP vs OOP table (at least 6 rows).
- Q2. Explain the drawbacks of procedure oriented programming that led to OOP. (5 Marks)
- Write: global data, top-down design, the four problems in section 1, with the bank account example.
- Q3. "Data is the most important element in OOP." Justify. (2.5 Marks)
- Write: data hiding inside objects; access only through member functions; show the private
balanceexample.