1 Encapsulation:
Encapsulation is the concept of bundling data (attributes) and methods (functions) that operate on that data into a single unit, known as a class. It allows us to hide the internal state of an object from the outside world and only expose a public interface for interacting with the object.
- It is achieved by access specifiers (public, private, protected) to control the visibility of class members.
Example:
Consider a Car
class:
class Car {
private:
string make;
string model;
int year;
public:
Car(string make, string model, int year) {
this->make = make;
this->model = model;
this->year = year;
}
string getMake() {
return make;
}
void accelerate() {
// Acceleration logic
}
};
In this example, the internal details of the Car class (make, model, year) are encapsulated within the class, and only the public methods (getMake()
, accelerate()
) provide access to the object's behavior.
2 Abstraction:
Abstraction is the process of simplifying complex systems by focusing on essential properties while hiding unnecessary details. It involves defining a clear and concise public interface for interacting with objects, while hiding the implementation details.
Example:
Consider a Shape
class:
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
In this example, Shape is an abstract class that defines a common interface for all shapes. Each specific shape (e.g., Circle, Rectangle) will provide its own implementation of the draw() method, hiding the details of how the shape is drawn.
3 Inheritance:
Inheritance is a mechanism that allows a class (derived or child class) to inherit attributes and behaviors from another class (base or parent class). It promotes code reuse and facilitates the creation of hierarchical relationships between classes.
Example:
Consider a Vehicle
class:
class Vehicle {
protected:
string make;
string model;
public:
Vehicle(string make, string model) {
this->make = make;
this->model = model;
}
void drive() {
// Driving logic
}
};
class Car : public Vehicle {
public:
Car(string make, string model) : Vehicle(make, model) {
// Constructor
}
};
In this example, the Car class inherits the make and model attributes from the Vehicle class, allowing it to reuse common functionality like drive().
4 Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common base class type. It enables code to operate on objects of different types in a uniform manner, promoting flexibility and extensibility.
There are two forms of polymorphism: compile-time (function overloading) and runtime (function overriding).
Example:
Consider a Shape
hierarchy:
class Shape {
public:
virtual void draw() {
// Default implementation
}
};
class Circle : public Shape {
public:
void draw() override {
// Draw a circle
}
};
class Rectangle : public Shape {
public:
void draw() override {
// Draw a rectangle
}
};
In this example, polymorphism allows us to treat Circle and Rectangle objects as Shape objects, enabling us to call the draw() method on them in a uniform manner.