Differences Between Procedural Programming and Object-Oriented Programming
Aspect | Procedural Programming | Object-Oriented Programming (OOP) |
---|---|---|
Concept | Focuses on functions and procedures to operate on data. | Focuses on objects, which encapsulate data and behavior. |
Structure | Programs are structured as a sequence of instructions. | Programs are structured around classes and objects. |
Data Handling | Data is usually exposed and shared across functions. | Data is encapsulated within objects and accessed via methods. |
Reusability | Limited; requires duplicating code for similar functions. | High; encourages code reuse through inheritance and polymorphism. |
Examples | C, Pascal | Java, Python, C++ |
Flexibility | Less flexible, harder to modify large programs. | Highly flexible, easier to modify and extend. |
—
Example Programs
1. Procedural Programming Example
This example calculates the area of a rectangle using functions.
#include <stdio.h>
// Function to calculate area of a rectangle
float calculateArea(float length, float width) {
return length * width;
}
int main() {
float length, width, area;
// Input length and width
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
// Calculate area
area = calculateArea(length, width);
// Output area
printf("Area of the rectangle: %.2fn", area);
return 0;
}
Key Features in Procedural Approach:
- Functions (
calculateArea
) operate on global/local variables. - Data (
length
,width
) and functions are separate entities. - No encapsulation or abstraction.
—
2. Object-Oriented Programming Example
This example calculates the area of a rectangle using classes and objects.
#include <iostream>
using namespace std;
// Class to represent a rectangle
class Rectangle {
private:
float length;
float width;
public:
// Constructor to initialize length and width
Rectangle(float l, float w) {
length = l;
width = w;
}
// Method to calculate the area
float calculateArea() {
return length * width;
}
// Method to display dimensions and area
void display() {
cout << "Length: " << length << ", Width: " << width << endl;
cout << "Area: " << calculateArea() << endl;
}
};
int main() {
float length, width;
// Input length and width
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
// Create an object of the Rectangle class
Rectangle rect(length, width);
// Display details and area
rect.display();
return 0;
}
Key Features in Object-Oriented Approach:
- Encapsulation: Data (
length
,width
) is private and accessed via public methods (calculateArea
). - Reusability: The
Rectangle
class can be reused in other programs. - Abstraction: Hides internal implementation details from the user.
—
Conclusion
Procedural programming organizes code into functions operating on shared data, while object-oriented programming organizes code into objects that encapsulate both data and behavior. OOP offers better modularity, code reuse, and scalability, making it more suitable for complex systems.