Lesson 1 +25 XP

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 dataProcedure oriented program: functions sharing global data

Characteristics of POP:

  1. Emphasis on doing things: the algorithm comes first, the data second.
  2. Large programs are divided into functions.
  3. Most functions share global data, which moves openly from function to function.
  4. Functions transform data from one form to another.
  5. 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:

C++
#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:

OUTPUT
Balance: -99999

3. 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.

ℹ️ Note

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 functionsObject oriented program: objects with their own data and functions

Characteristics of OOP:

  1. Emphasis is on data rather than procedure.
  2. Programs are divided into objects.
  3. Data structures are designed to describe the objects.
  4. Functions that operate on an object's data are tied to it in the same data structure.
  5. Data is hidden and cannot be accessed by external functions.
  6. Objects communicate through functions (by sending messages).
  7. New data and functions can be added easily when needed.
  8. Bottom-up design: small, well-tested classes are combined into bigger ones.

The same bank account written in the object oriented style:

C++
#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:

OUTPUT
Insufficient funds
Balance: 1300

The data balance can now be changed only through deposit and withdraw, and those functions enforce the rules.

4. POP vs OOP

BasisProcedure oriented (POP)Object oriented (OOP)
Building blockFunctionObject
EmphasisAlgorithm (doing things)Data
Design approachTop-downBottom-up
DataMostly global, shared by all functionsHidden inside objects
Data securityLow: any function can change dataHigh: access only through member functions
CommunicationFunction calls with shared dataObjects send messages to each other
Adding new dataMany functions must changeAdd a new class or member
ReuseLimitedInheritance and composition
Access specifiersNoneprivate, protected, public
OverloadingNot supported (C)Functions and operators can be overloaded
ExamplesC, Pascal, FORTRAN, COBOLC++, Java, C#, Python, Smalltalk

5. Programming Paradigms at a Glance

Programming styles evolved as programs grew:

ParadigmIdeaExample language
Monolithicthe whole program is one block, using gotoassembly, early BASIC
Proceduralthe program is a sequence of proceduresFORTRAN, COBOL
Structuredmodules and functions, no goto; top-downC, Pascal
Object orientedobjects bundle data and functionsSmalltalk, 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.

💡 Tip

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).
🎯 END-TERM EXAM PREP

📝 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 balance example.