Introduction to OOP
Object-Oriented Programming (OOP) is a programming paradigm that is based on the concept of objects, which contain data (attributes) and methods (functions) that operate on the data. It is widely used for designing large, complex, and reusable software systems.
Why OOP?
Traditional programming approaches like procedural programming (C, Pascal) focus on functions and procedures. However, as software complexity increased, procedural programming had several limitations:
- Difficult to manage large codebases.
- Low code reusability.
- Higher chances of errors and maintenance issues.
To overcome these limitations, Object-Oriented Programming (OOP) was introduced, which helps in modular, maintainable, and reusable code development.
Basic Features of OOP
OOP is based on the following four key principles:
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
Letβs discuss each of them in detail:
1. Encapsulation
Encapsulation is the concept of hiding data and restricting direct access to it. Data is kept private and can only be accessed using public methods (getters and setters).
πΉ Example:
#include <iostream>
using namespace std;
class Student {
private:
int age; // Private variable
public:
void setAge(int a) { age = a; } // Setter function
int getAge() { return age; } // Getter function
};
int main() {
Student s1;
s1.setAge(21);
cout << "Student Age: " << s1.getAge() << endl;
return 0;
}
πΉ Advantages:
- Protects data from accidental modification.
- Maintains data integrity and security.
- Increases code maintainability.
2. Abstraction
Abstraction is the process of hiding unnecessary details from the user and only exposing the essential features of an object.
πΉ Example:
#include <iostream>
using namespace std;
class Car {
public:
void start() {
ignition();
cout << "Car started!" << endl;
}
private:
void ignition() { cout << "Engine ignited!" << endl; }
};
int main() {
Car myCar;
myCar.start(); // User does not need to know how ignition() works.
return 0;
}
πΉ Advantages:
- Simplifies complex systems by hiding implementation details.
- Reduces code complexity and increases reusability.
- Improves security by restricting access to certain functionalities.
3. Inheritance
Inheritance is a feature that allows a new class (child class) to inherit properties and methods from an existing class (parent class). This promotes code reusability and establishes a relationship between classes.
πΉ Example:
#include <iostream>
using namespace std;
// Parent class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// Child class
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks!" << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal class
myDog.bark();
return 0;
}
πΉ Advantages:
- Code reusability (No need to rewrite common functionalities).
- Establishes a hierarchical relationship between classes.
- Reduces code duplication.
4. Polymorphism
Polymorphism means “one name, multiple forms.” It allows functions or methods to behave differently based on the object that calls them.
There are two types of polymorphism:
- Compile-time Polymorphism (Function Overloading, Operator Overloading)
- Runtime Polymorphism (Function Overriding using Virtual Functions)
(a) Function Overloading (Compile-time Polymorphism)
πΉ Example:
#include <iostream>
using namespace std;
class Math {
public:
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; } // Function Overloading
};
int main() {
Math obj;
cout << "Sum (int): " << obj.add(5, 10) << endl;
cout << "Sum (double): " << obj.add(5.5, 10.5) << endl;
return 0;
}
(b) Function Overriding (Runtime Polymorphism)
πΉ Example:
#include <iostream>
using namespace std;
class Animal {
public:
virtual void makeSound() { cout << "Animal makes a sound." << endl; }
};
class Dog : public Animal {
public:
void makeSound() override { cout << "Dog barks!" << endl; } // Overriding
};
int main() {
Animal* animalPtr;
Dog myDog;
animalPtr = &myDog;
animalPtr->makeSound(); // Calls overridden method in Dog class
return 0;
}
πΉ Advantages:
- Increases flexibility and scalability of code.
- Allows a single interface to be used for different types of objects.
- Improves code readability and maintainability.
Other Features of OOP
Apart from the four main principles, OOP also provides:
- Classes and Objects: A class is a blueprint, and an object is an instance of a class.
- Constructor and Destructor: Special methods for object initialization and cleanup.
- Data Hiding: Restricting direct access to class members.
- Dynamic Binding: Function calls are resolved at runtime.
Advantages of OOP
β
Modular Code β Code is divided into classes and objects.
β
Reusability β Code can be reused using inheritance.
β
Scalability β Easy to expand programs without modifying existing code.
β
Security β Data is hidden using encapsulation and abstraction.
β
Flexibility β Polymorphism allows the same function to have different implementations.
Conclusion
Object-Oriented Programming (OOP) is a powerful approach that makes programming modular, reusable, scalable, and efficient. It is widely used in software development for real-world applications like game development, web applications, AI, and more.