Back to blog
Design March 8, 2025 · 7 min read

Understanding Object-Oriented Programming

A beginner-friendly guide to classes, objects, constructors, encapsulation, inheritance, polymorphism, abstraction, and how these core OOP concepts are used in C++.

Sabokh

Sabokh

C++ Python & SQL & Web Technologies

C++ code editor showing Object-Oriented Programming concepts

What is Object-Oriented Programming?

Object-Oriented Programming, commonly called OOP, is a programming approach that organizes a program around objects and the data and functions that belong to those objects. Instead of writing one large sequence of instructions, we divide a program into smaller and reusable parts.

OOP makes large programs easier to understand, maintain, reuse, and expand. C++ is one of the most popular programming languages that supports object-oriented programming.

"OOP helps us model software around real-world things such as students, cars, bank accounts, employees, and products."

Class

A class is a blueprint or template for creating objects. It defines what data an object will contain and what actions that object can perform.

For example, if we want to create a Student system, a Student class can contain a student's name, age, and roll number along with functions for displaying or updating that information.

#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int age;

    void display() {
        cout << name << " " << age << endl;
    }
};

Object

An object is an actual instance of a class. The class is the blueprint, while the object is the real thing created from that blueprint.

For example, Student s1 creates an object named s1 from the Student class.

Student s1;

s1.name = "Ali";
s1.age = 20;

s1.display();

Constructor

A constructor is a special member function of a class that is automatically called when an object is created. It is commonly used to initialize the object's data.

A constructor has the same name as the class and does not have a return type.

class Student {
public:
    string name;
    int age;

    Student(string n, int a) {
        name = n;
        age = a;
    }

    void display() {
        cout << name << " " << age << endl;
    }
};

Student s1("Ali", 20);
s1.display();

Encapsulation

Encapsulation means keeping data and the functions that operate on that data together inside a class. It also allows us to control access to data using access modifiers such as private, public, and protected.

A common example is a bank account. The account balance should not be changed directly by anyone. Instead, functions such as deposit and withdraw can control how the balance changes.

class BankAccount {
private:
    double balance;

public:
    BankAccount(double amount) {
        balance = amount;
    }

    void deposit(double amount) {
        balance += amount;
    }

    double getBalance() {
        return balance;
    }
};

Inheritance

Inheritance allows one class to acquire the properties and functions of another class. The existing class is called the base class, while the new class is called the derived class.

Inheritance promotes code reuse. For example, a Car and a Bike can inherit common features from a Vehicle class.

class Vehicle {
public:
    void start() {
        cout << "Vehicle started" << endl;
    }
};

class Car : public Vehicle {
public:
    void drive() {
        cout << "Car is driving" << endl;
    }
};

Car c;
c.start();
c.drive();

Polymorphism

Polymorphism means "many forms". It allows the same function or interface to behave differently depending on the object or situation.

In C++, polymorphism can be achieved through function overloading, operator overloading, and virtual functions. Runtime polymorphism is commonly implemented using virtual functions.

class Animal {
public:
    virtual void sound() {
        cout << "Animal makes a sound" << endl;
    }
};

class Dog : public Animal {
public:
    void sound() override {
        cout << "Dog barks" << endl;
    }
};

Animal* a = new Dog();
a->sound();

delete a;

Abstraction

Abstraction means showing only the important details and hiding unnecessary implementation details from the user.

For example, when we use an ATM, we choose options such as withdraw or deposit. We do not need to know all the internal operations that happen inside the banking system.

class Shape {
public:
    virtual void draw() = 0;
};

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing a circle" << endl;
    }
};

Circle c;
c.draw();

Real-life examples

OOP concepts can be understood easily through everyday objects. A Car can be represented as an object with properties such as color, model, and speed, and functions such as start, stop, and accelerate.

A Bank Account can contain account number and balance, with functions for deposit and withdrawal. A Student can contain a name, roll number, and GPA, with functions for displaying or updating academic information.

  • Class: Car is a blueprint for creating cars.
  • Object: A specific Toyota Corolla can be an object of the Car class.
  • Encapsulation: A bank account protects its balance from direct modification.
  • Inheritance: Car and Bike can inherit common features from Vehicle.
  • Polymorphism: Different animals can implement the same sound() function differently.
  • Abstraction: A user drives a car without needing to understand its complete internal mechanism.

C++ examples

Here is a small example that combines several OOP concepts into one simple C++ program.

#include <iostream>
using namespace std;

class Person {
protected:
    string name;

public:
    Person(string n) {
        name = n;
    }

    virtual void introduce() {
        cout << "My name is " << name << endl;
    }
};

class Student : public Person {
private:
    int age;

public:
    Student(string n, int a) : Person(n) {
        age = a;
    }

    void introduce() override {
        cout << "My name is " << name << " and I am " << age << " years old." << endl;
    }
};

int main() {
    Student s("Ali", 20);
    s.introduce();

    return 0;
}

This example demonstrates a class, object, constructor, encapsulation through private data, inheritance from Person, and polymorphism through the overridden introduce() function.

Conclusion

Object-Oriented Programming is a powerful way to organize and structure software. The main concepts of OOP are classes, objects, constructors, encapsulation, inheritance, polymorphism, and abstraction.

Once these concepts become clear, larger C++ programs become much easier to design and maintain. OOP is especially useful for projects where many different entities interact with each other, such as student management systems, airline systems, banking applications, and e-commerce platforms.


Understanding OOP is an important step toward writing structured, reusable, and maintainable C++ programs. 🧡