OOP
Encapsulation
Encapsulation is the practice of bundling data (attributes) and methods (functions) together and restricting access to certain components through access specifiers (private
, protected
, public
).
cpp
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance; // Private: Not directly accessible from outside
public:
BankAccount(double initial_balance) : balance(initial_balance) {}
void deposit(double amount) {
if (amount > 0) balance += amount;
}
void withdraw(double amount) {
if (amount > 0 && amount <= balance) balance -= amount;
}
double getBalance() const { // Public method to access private data
return balance;
}
};
int main() {
BankAccount account(1000);
account.deposit(500);
account.withdraw(300);
cout << "Balance: $" << account.getBalance() << endl;
return 0;
}
Inheritance
Inheritance allows a class (child or derived) to inherit properties and methods from another class (parent or base).
cpp
#include <iostream>
using namespace std;
// Base Class
class Animal {
public:
void eat() { cout << "This animal eats food.\n"; }
};
// Derived Class
class Dog : public Animal {
public:
void bark() { cout << "The dog barks.\n"; }
};
int main() {
Dog dog;
dog.eat(); // Inherited from Animal
dog.bark(); // Defined in Dog
return 0;
}
Polymorphism
Polymorphism means "many forms" and allows methods to behave differently based on the object that calls them. It is typically implemented using method overriding and achieved via virtual functions.
cpp
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { // Virtual function
cout << "Some generic animal sound.\n";
}
};
class Dog : public Animal {
public:
void sound() override { // Overriding base class method
cout << "Dog barks.\n";
}
};
class Cat : public Animal {
public:
void sound() override {
cout << "Cat meows.\n";
}
};
int main() {
Animal* animal1 = new Dog();
Animal* animal2 = new Cat();
animal1->sound(); // Dog's sound()
animal2->sound(); // Cat's sound()
delete animal1;
delete animal2;
return 0;
}
Abstraction
Abstraction focuses on hiding implementation details while exposing essential functionalities. This is typically achieved using abstract classes or interfaces (in C++, achieved via pure virtual functions).
cpp
#include <iostream>
using namespace std;
// Abstract Class
class Shape {
public:
virtual double area() const = 0; // Pure virtual function
virtual void display() const = 0; // Pure virtual function
virtual ~Shape() {} // Virtual destructor
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override { // Implementation of abstract method
return 3.14 * radius * radius;
}
void display() const override {
cout << "Circle Area: " << area() << endl;
}
};
class Rectangle : public Shape {
private:
double length, width;
public:
Rectangle(double l, double w) : length(l), width(w) {}
double area() const override {
return length * width;
}
void display() const override {
cout << "Rectangle Area: " << area() << endl;
}
};
int main() {
Shape* shapes[] = {new Circle(5), new Rectangle(4, 6)};
for (Shape* shape : shapes) {
shape->display();
delete shape;
}
return 0;
}
Summary of Concepts
Concept | Description | Key Features |
---|---|---|
Encapsulation | Bundles data and methods, restricts access via access specifiers. | private , protected , public |
Inheritance | Allows a derived class to reuse and extend functionality of a base class. | public , protected , private inheritance modes |
Polymorphism | Allows functions to behave differently based on the object type. | Virtual functions, function overriding |
Abstraction | Hides implementation details while exposing essential functionalities via abstract classes. | Pure virtual functions (= 0 ) |