0
0
C++programming~10 mins

Procedural vs OOP approach in C++ - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Procedural vs OOP approach
Start
Procedural Approach
Functions operate on data
Data and functions separate
OOP Approach
Objects combine data + functions
Encapsulation & reuse
End
This flow shows how procedural code runs functions on separate data, while OOP bundles data and functions into objects for better organization.
Execution Sample
C++
#include <iostream>

// Procedural
int add(int a, int b) { return a + b; }

// OOP
class Calculator {
public:
  int add(int a, int b) { return a + b; }
};
This code shows a simple addition function in procedural style and the same addition method inside a Calculator class in OOP style.
Execution Table
StepActionProcedural CodeOOP CodeOutput
1Call add functionadd(2, 3)Create Calculator object
2Execute additionReturn 2 + 3 = 5Call obj.add(2, 3)
3Return result5Return 2 + 3 = 55
4Print resultstd::cout << 5std::cout << 55
5EndProgram ends
💡 Program ends after printing the sum 5 in both approaches
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
aN/A2222
bN/A3333
resultN/AN/AN/A55
Calculator objectN/ACreatedExistsExistsExists
Key Moments - 3 Insights
Why do we create an object in OOP but not in procedural?
In the execution_table row 1, the procedural code calls a function directly because functions and data are separate. In OOP, we create an object to bundle data and functions together before calling methods.
Is the addition calculation different in procedural and OOP?
No, as shown in execution_table rows 2 and 3, both approaches perform the same addition operation and return the same result.
Why do we say OOP bundles data and functions?
Because in variable_tracker, the Calculator object holds both the method add and any data it might have, unlike procedural where data and functions are separate.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after step 4?
A2
B5
C3
D0
💡 Hint
Check the Output column at step 4 in the execution_table.
At which step is the Calculator object created in the OOP approach?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the OOP Code column in execution_table row 1.
If we add a data member to Calculator, which variable_tracker row would change?
ACalculator object
Ba
Cb
Dresult
💡 Hint
Data members belong to the object, see variable_tracker row for Calculator object.
Concept Snapshot
Procedural approach: Separate functions and data.
Call functions directly with data as arguments.
OOP approach: Bundle data and functions into objects.
Create objects, then call methods on them.
OOP helps organize and reuse code better.
Full Transcript
This visual execution compares procedural and object-oriented programming in C++. Procedural code uses separate functions like add(a,b) called directly. OOP code creates a Calculator object that bundles the add method. The execution table shows steps calling add in both styles, returning 5, and printing it. Variable tracking shows inputs a=2, b=3, and the Calculator object created in OOP. Key moments clarify why OOP needs an object and how both approaches compute the same result. The quiz tests understanding of output, object creation step, and object data members. The snapshot summarizes the main difference: procedural separates data and functions, OOP bundles them into objects for better structure.