Object Oriented Programming in C++ Complete Guide with Examples
OOP in C++ is a coding style built on 4 pillars — Encapsulation, Inheritance, Polymorphism, and Abstraction — that makes your code reusable, secure, and easier to maintain.
Object Oriented Programming in C++ (OOP in C++) is a programming style that organizes code around real-world objects and their behaviors. Instead of writing a long sequence of instructions, OOP in C++ lets you style your program using classes and objects, making code more structured, readable, and maintainable
It is not necessarily mandatory to use OOP while coding you can write functional programs without it. But for large scale software, object oriented programming concepts in C++ offer a clear advantage over procedural programming.
What is the Difference Between OOP and Procedural Programming?
Understanding procedural programming vs object oriented programming is the first step:
Procedural Programming
Object Oriented Programming in C++
Top-down approach
Bottom-up approach
Data and functions are separate
Bundled together in classes
Harder to reuse code
Code reusability via inheritance
Less data security
Data hiding in C++ via encapsulation
No real-world modeling
Direct real world modeling in OOP
e.g., C, Pascal
e.g., C++, Java, Python
Benefits of OOP in C++
OOP is one of the most important coding styles. Here's why developers prefer C++ OOP concepts:
Code Reusability — The DRY principle in programming (Don't Repeat Yourself) is enforced naturally.
Improved Code Readability — Code becomes self-documenting and easier to maintain.
Data Security — Data hiding in C++ through encapsulation protects sensitive information.
Modular Programming in C++ — Break complex programs into smaller, manageable classes.
Flexibility — Polymorphism allows one interface to work with many data types.
Object Identity, State, and Behavior — Every object has a unique identity, a state (data), and behavior (methods).
Four Pillars of OOP in C++
The four pillars of OOP in C++ are the foundation of all object oriented programming concepts in C++. Let's explore each one with examples.
1. Encapsulation in C++
Encapsulation in C++ means wrapping all data variables and member functions inside a class and restricting direct access using access specifiers. Think of it as putting all your data inside a jar — you control who can access it.
Access specifiers in C++ — public, private, and protected in C++ — define the visibility of class members. Getter and setter methods in C++ are used to safely read and modify private data, which is a core practice of data hiding in C++.
cpp
class Student {private: int marks; // Data hidden from outsidepublic: // Setter method void setMarks(int m) { marks = m; } // Getter method int getMarks() { return marks; }};
2. Inheritance in C++
Inheritance in C++ allows a child (derived) class to access data variables and C++ member functions from a parent (base) class. It promotes code reusability in OOP — instead of declaring variables repeatedly, you inherit them from the parent class.
Think of it like getting your surname from your parents. The child inherits properties without having to redefine them — a direct application of the DRY principle in programming.
cpp
class Animal {public: void eat() { cout << "Animal eats\n"; }};// Dog inherits from Animalclass Dog : public Animal {public: void bark() { cout << "Dog barks\n"; }};
Dog is a C++ instance derived from Animal. It inherits the eat() function without rewriting it — this is exactly what code reusability in OOP means.
3. Polymorphism in C++
Polymorphism (Poly = Many + Morphism = Form) is a feature of C++ OOP concepts that allows a function or method to behave differently in different situations. Think of it like how you behave politely around your parents but differently with your friends — same person, multiple behaviors.
Polymorphism in C++ is of two types:
Type 1 — Function Overloading (Compile Time Polymorphism)
Resolved at compile time. The same function name handles different parameter types or counts:
cpp
class MathOp {public: int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; }};
Type 2 — Function Overriding (Run Time Polymorphism)
Resolved at runtime. A child class provides its own implementation of a parent class method:
cpp
class Animal {public: void sound() { cout << "Animal sound\n"; }};class Dog : public Animal {public: void sound() { // Overrides parent method cout << "Bark\n"; }};
Compile time vs run time polymorphism — Function overloading is resolved at build time by the compiler. Function overriding is resolved while the program runs, enabling dynamic behavior based on the actual object type.
4. Abstraction in C++
Abstraction means showing only the required information and hiding the implementation details. Think of it as when you play Free Fire — you only interact with the UI and don't need to know the underlying engine code.
cpp
abstract class Shape { abstract void draw(); // Only the interface is exposed}class Circle extends Shape { void draw() { System.out.println("Drawing Circle"); }}
Class in C++ and Object in C++
A class in C++ is a blueprint that defines the structure and behavior of objects. A C++ instance (object) is a specific realization of that blueprint.
Every object in OOP has three characteristics:
Object Identity — A unique identifier for each object.
Object State — The current values of its C++ instance variables.
Object Behavior — The C++ member functions it can perform.
cpp
class Car {public: string brand; // C++ instance variable int speed; // C++ instance variable // C++ member function void accelerate() { speed += 10; cout << brand << " accelerates to " << speed << " km/h\n"; }};int main() { Car myCar; // Creating a C++ instance (object) myCar.brand = "Toyota"; myCar.speed = 0; myCar.accelerate(); // Calling C++ member functions return 0;}
Advantages of OOP in C++
The advantages of OOP over procedural programming are significant:
Code Reusability — Inheritance eliminates redundancy via the DRY principle in programming.
Data Security — Data hiding in C++ through private access specifiers prevents unauthorized access.
Flexibility — Polymorphism allows one interface to work with many data types.
Modular Programming in C++ — Each class is self-contained, making debugging simpler.
Real World Modeling in OOP — Map real-world entities directly to classes and objects.
Disadvantages of OOP in C++
Like all paradigms, the advantages and disadvantages of OOP in C++ must be weighed:
More Memory Usage — Objects carry overhead compared to simple variables.
Slower Execution — Virtual function calls in polymorphism add a slight runtime cost.
Complex for Smaller Programs — OOP patterns on small scripts can be overkill.
Steeper Learning Curve — Mastering all four pillars of OOP in C++ takes time.
Frequently Asked Questions
What type of questions are asked in SDE interviews about OOP?
SDE interviews typically test definitions and conceptual understanding of OOP in C++. Prepare for:
What are the four pillars of OOP in C++?
Explain data hiding in C++ and the difference between encapsulation vs abstraction.
What is the difference between OOP and procedural programming?
Explain function overloading vs function overriding (compile time vs run time polymorphism).
What are access specifiers in C++? Explain public, private, protected in C++.
How does the DRY principle in programming relate to object oriented programming concepts in C++?
Give a C++ class and object example demonstrating all four pillars of OOP in C++.
Is it mandatory to use OOP in C++?
No — it is not necessarily mandatory to use OOP while coding. C++ supports both procedural and object-oriented styles. However, for large, maintainable applications, OOP in C++ is strongly recommended due to its advantages in modular programming in C++ and code reusability in OOP.
What is the difference between compile time and run time polymorphism?
Compile time polymorphism (function overloading) is resolved at build time — the compiler picks the right function version based on the parameters. Run time polymorphism (function overriding) is resolved when the program executes, enabling dynamic dispatch based on the actual object type.
Summary
Object oriented programming in C++ is a powerful paradigm built on four pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction. Understanding these OOP concepts in C++ with examples — from class in C++ and object in C++ basics to advanced features like function overloading and data hiding in C++ — is essential for any developer.
Whether you're preparing for SDE interviews or building production software, mastering C++ OOP concepts will make you a significantly better programmer.