Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects,” which contain both data (attributes) and functions (methods) that operate on the data. OOP helps in designing modular, reusable, and scalable software systems.
OOP is based on four fundamental principles: Encapsulation, Abstraction, Inheritance, and Polymorphism. Letβs discuss each in detail:
1. Encapsulation
Definition:
Encapsulation is the process of bundling data (variables) and methods (functions) that operate on the data into a single unit called a class. It also restricts direct access to some of the object’s components, which helps prevent accidental modification of data.
Example:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Private data, cannot be accessed directly
public:
BankAccount(double initialBalance) {
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
cout << "Insufficient funds!" << endl;
}
}
void displayBalance() {
cout << "Balance: " << balance << endl;
}
};
int main() {
BankAccount myAccount(1000);
myAccount.deposit(500);
myAccount.withdraw(200);
myAccount.displayBalance();
return 0;
}
Key Points:
- The
balance
variable is private, meaning it cannot be accessed directly from outside the class. - Methods like
deposit()
,withdraw()
, anddisplayBalance()
provide controlled access to the private data. - This helps in data security and integrity.
2. Abstraction
Definition:
Abstraction is the process of hiding implementation details from the user and only exposing the essential features of an object. It helps reduce complexity by allowing the user to interact with the object without knowing its internal workings.
Example:
#include <iostream>
using namespace std;
class Car {
public:
void startCar() {
igniteEngine(); // Private function called inside public function
cout << "Car started!" << endl;
}
private:
void igniteEngine() { // Hidden from the user
cout << "Engine ignited!" << endl;
}
};
int main() {
Car myCar;
myCar.startCar(); // User only calls startCar() without knowing about igniteEngine()
return 0;
}
Key Points:
- The function
igniteEngine()
is private, meaning the user cannot directly access it. - The user only needs to call
startCar()
without knowing how the engine starts. - This simplifies the interface and hides unnecessary implementation details.
3. Inheritance
Definition:
Inheritance allows a new class (child or derived class) to acquire the properties and behavior of an existing class (parent or base class). This promotes code reusability and hierarchical relationships.
Example:
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "This animal eats food." << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void bark() {
cout << "The dog barks." << endl;
}
};
int main() {
Dog myDog;
myDog.eat(); // Inherited from Animal class
myDog.bark(); // Specific to Dog class
return 0;
}
Key Points:
- The
Dog
class inherits theeat()
function from theAnimal
class. - This avoids code duplication and allows extending the functionality.
- Different types of inheritance exist:
- Single Inheritance: One child class inherits from one parent class.
- Multiple Inheritance: A child class inherits from multiple parent classes.
- Hierarchical Inheritance: Multiple child classes inherit from a single parent class.
- Multilevel Inheritance: A class is derived from another derived class.
4. Polymorphism
Definition:
Polymorphism means “one name, many forms.” It allows the same function or operator to behave differently in different situations. Polymorphism is mainly of two types:
- Compile-time (Static) Polymorphism β Achieved using function overloading and operator overloading.
- Runtime (Dynamic) Polymorphism β Achieved using function overriding with virtual functions.
Example of Function Overloading:
#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;
}
Example of Function Overriding (Dynamic Polymorphism):
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
virtual void makeSound() { // Virtual function
cout << "Animal makes a sound." << endl;
}
};
// Derived class
class Dog : public Animal {
public:
void makeSound() override { // Function Overriding
cout << "Dog barks!" << endl;
}
};
int main() {
Animal* animalPtr;
Dog myDog;
animalPtr = &myDog;
animalPtr->makeSound(); // Calls overridden method in Dog class
return 0;
}
Key Points:
- Function overloading allows multiple functions with the same name but different parameters.
- Function overriding allows a child class to provide a specific implementation of a function from the parent class.
- Virtual functions ensure runtime polymorphism, allowing function calls to be resolved at runtime rather than compile-time.
Conclusion
Object-Oriented Programming (OOP) in C++ helps in writing structured, modular, reusable, and scalable code. The four main principles (Encapsulation, Abstraction, Inheritance, and Polymorphism) provide a strong foundation for developing complex applications efficiently.
By following these principles, programmers can create software that is easier to maintain, extend, and debug. π