Object-Oriented Programming (OOP) is based on a set of concepts and principles that allow for designing software that is modular, reusable, and easier to maintain. These principles help in structuring the code around objects, which can interact with one another while hiding implementation details.
Here are the core Object-Oriented Concepts and Principles:
An object is a basic unit in OOP, representing a real-world entity or concept. It is an instance of a class and has two main components:
Example: In a "Car" object:
A class is a blueprint or template for creating objects. It defines the structure and behavior of the objects but does not itself hold data (that’s the job of the object instances). A class encapsulates data and methods into a single unit.
Example:
class Car {
public:
string color;
string model;
void start_engine() {
// Logic to start the engine
}
void stop_engine() {
// Logic to stop the engine
}
};
In this example, the Car class defines the structure for all car objects.
Encapsulation refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class) and restricting direct access to some of the object’s components. This is typically achieved by using access specifiers (like public, private, protected in C++), ensuring that the internal state of the object is hidden from the outside world.
Benefits:
Example:
class BankAccount {
private:
double balance; // Private data, cannot be accessed directly
public:
void deposit(double amount) {
if (amount > 0)
balance += amount;
}
double get_balance() {
return balance;
}
};
Here, balance is private and cannot be directly accessed from outside the BankAccount class. Methods like deposit() and get_balance() provide controlled access to it.
Inheritance is a mechanism where a new class (called a derived class or child class) acquires the attributes and methods of an existing class (called a base class or parent class). This allows for code reuse and the creation of more specialized classes.
Example:
class Vehicle {
public:
void start() {
cout << "Vehicle started" << endl;
}
};
class Car : public Vehicle {
public:
void drive() {
cout << "Car is driving" << endl;
}
};
In this example, the Car class inherits from the Vehicle class and can access the start() method while adding its own method, drive().
Types of Inheritance:
Polymorphism allows objects of different classes to be treated as objects of a common base class. The term means “many forms,” and it refers to the ability to call the same method on different objects, and each object can respond in a way that is appropriate to its type.
There are two types of polymorphism in OOP:
Example of Method Overriding:
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
class Cat : public Animal {
public:
void sound() override {
cout << "Cat meows" << endl;
}
};
int main() {
Animal* animal1 = new Dog();
Animal* animal2 = new Cat();
animal1->sound(); // Dog barks
animal2->sound(); // Cat meows
delete animal1;
delete animal2;
}
Here, the method sound() is overridden in the Dog and Cat classes, and the correct version of sound() is called based on the object type at runtime.
Abstraction is the concept of hiding the complex implementation details and exposing only the essential features or interfaces to the user. It helps in reducing complexity and allows focusing on high-level mechanisms.
Example:
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle" << endl;
}
};
In this example, Shape is an abstract class, and draw() is a pure virtual function that must be implemented in any derived class.
These are relationships between objects that represent how one object is related to another.
The main Object-Oriented Principles—Encapsulation, Inheritance, Polymorphism, and Abstraction—form the foundation for designing robust, maintainable, and reusable code. They allow developers to break down complex systems into manageable objects and enable the development of scalable and flexible applications. Understanding and applying these principles is crucial for mastering OOP in languages like C++.
Open this section to load past papers