-
Hajipur, Bihar, 844101
Object-Oriented Programming, often called OOP, is one of the most powerful features of C++. It helps organize code into reusable structures that model real-world entities like people, cars, students, or bank accounts. Instead of focusing only on functions and logic, OOP makes the program revolve around objects—the building blocks that combine both data and behavior.
Let’s explore the core concepts of OOP in C++ and understand how they make programs more structured, reusable, and easy to maintain.
Object-Oriented Programming is a programming approach based on objects. These objects contain both data members (variables) and member functions (methods) that work together to represent real-world entities.
For example, think about a Car.
A car has:
Data: color, model, year, engine type
Functions: start(), stop(), accelerate()
In C++, a class defines the structure (blueprint) of such an object, while an object is the actual instance of that class.
Before OOP, most programs were written using procedural programming, where the focus was on writing functions and instructions to manipulate data. As programs grew larger, managing and maintaining them became harder.
C++ introduced OOP to solve this problem. The key benefits include:
Reusability – Once a class is written, it can be reused in other programs.
Scalability – Large programs can be managed by dividing them into objects.
Maintainability – Changes in one part of the code don’t break the whole program.
Security – Data is protected through encapsulation.
Flexibility – Through inheritance and polymorphism, new features can be added without rewriting the existing code.
C++ is built on four major principles that define object-oriented programming:
Encapsulation
Abstraction
Inheritance
Polymorphism
Let’s look at each in detail.
Encapsulation means wrapping data and functions together into a single unit—a class.
It prevents outside interference and misuse of data. Only the class’s own methods can modify its data, unless access is explicitly allowed.
For example:
#include <iostream>
using namespace std;
class Student {
private:
int age; // data is hidden from outside
public:
void setAge(int a) {
age = a;
}
int getAge() {
return age;
}
};
int main() {
Student s1;
s1.setAge(20);
cout << "Student Age: " << s1.getAge();
return 0;
}
Here, the variable age is private, which means it can’t be accessed directly outside the class. The public functions setAge() and getAge() are used to modify and view the value safely. This is encapsulation in action.
Abstraction means showing only the necessary details to the user and hiding complex internal logic.
When you drive a car, you only use the steering and pedals—you don’t worry about how the engine works internally. Similarly, in programming, abstraction hides complex implementation details.
Example:
#include <iostream>
using namespace std;
class BankAccount {
private:
double balance;
public:
BankAccount(double bal) {
balance = bal;
}
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (amount <= balance)
balance -= amount;
else
cout << "Insufficient balance!" << endl;
}
double getBalance() {
return balance;
}
};
int main() {
BankAccount acc(1000);
acc.deposit(500);
acc.withdraw(300);
cout << "Final Balance: " << acc.getBalance();
return 0;
}
The user doesn’t need to know how deposit or withdraw works internally; they just call the functions. This is abstraction.
Inheritance allows one class to derive properties and behaviors from another class.
It helps reuse existing code and establish a relationship between parent and child classes.
Example:
#include <iostream>
using namespace std;
class Person {
public:
string name;
void introduce() {
cout << "My name is " << name << endl;
}
};
class Student : public Person {
public:
string course;
void showCourse() {
cout << "I am studying " << course << endl;
}
};
int main() {
Student s1;
s1.name = "Riya";
s1.course = "Computer Science";
s1.introduce();
s1.showCourse();
return 0;
}
Here, the Student class inherits from Person, so it automatically gains the introduce() function and the name variable.
Polymorphism means one name, many forms.
It allows the same function name to behave differently depending on the context.
There are two types:
Compile-time polymorphism (achieved by function overloading or operator overloading)
Run-time polymorphism (achieved using inheritance and virtual functions)
Example (Run-time polymorphism):
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() {
cout << "Drawing a shape" << endl;
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a circle" << endl;
}
};
class Rectangle : public Shape {
public:
void draw() override {
cout << "Drawing a rectangle" << endl;
}
};
int main() {
Shape* s1;
Circle c;
Rectangle r;
s1 = &c;
s1->draw();
s1 = &r;
s1->draw();
return 0;
}
Here, even though s1 is a pointer of type Shape, it calls the appropriate version of draw() depending on which object it points to. That’s polymorphism.
Imagine you’re building a Library Management System.
You can create:
A Book class with title, author, and ISBN.
A Member class for users with name and ID.
A Librarian class that can add or remove books.
All these classes can interact. Member might borrow a Book, and Librarian can manage the list.
This kind of system organization is possible and clean because of OOP principles.
| Benefit | Description |
|---|---|
| Reusability | Classes can be reused across multiple programs. |
| Data Security | Private data prevents misuse. |
| Flexibility | Easily update or expand programs. |
| Maintainability | Easier to debug and modify. |
| Productivity | Reduces code repetition and development time. |
Object-Oriented Programming is the foundation of modern C++ programming.
By focusing on objects and classes, it mirrors real-world systems in code.
The four core concepts—Encapsulation, Abstraction, Inheritance, and Polymorphism—make your code modular, secure, and easier to maintain.
As you move ahead, you’ll see how these ideas work in detail through topics like Classes/Objects, Methods, Constructors, and Inheritance. Understanding OOP is the first step toward mastering C++ programming.
Write a C++ program to create a Student class with data members and member functions to input and display student details.
Create an Employee class that uses encapsulation to protect data members and includes a method to calculate total salary.
Write a program to implement a BankAccount class with deposit and withdraw functions demonstrating abstraction.
Design two classes, Book and Library, where Library can store and display multiple Book objects.
Create a base class Vehicle and derive Car and Bike classes from it using inheritance.
Write a C++ program that demonstrates run-time polymorphism using a base class Shape and derived classes Circle and Rectangle.
Implement a Person class that displays messages in its constructor and destructor to show object creation and destruction.
Create a class Account that demonstrates encapsulation with validation checks in deposit and withdraw functions.
Implement multilevel inheritance with classes Person, Employee, and Manager to display hierarchical relationships.
Write a C++ program using virtual functions where a base class Animal and derived classes Dog and Cat produce different sounds.