What is OOP in C++?
Learn what OOP means in C++, including encapsulation, inheritance, polymorphism, and abstraction, with clear examples and interview-ready explanations.
Expected Interview Answer
Object-Oriented Programming (OOP) in C++ is a paradigm that organizes code around classes and objects, bundling data and the functions that operate on it together rather than treating data and logic separately.
C++ supports the four core OOP pillars: encapsulation (hiding internal state behind public interfaces), inheritance (deriving new classes from existing ones to reuse and extend behavior), polymorphism (calling the correct overridden function through a base pointer or reference via virtual functions), and abstraction (exposing only relevant details through interfaces or abstract classes). Classes define blueprints; objects are concrete instances with their own state. Constructors and destructors manage object lifetime, and access specifiers (`public`, `private`, `protected`) enforce encapsulation. OOP in C++ enables modular, reusable, and maintainable code, especially in large systems.
- Encapsulation hides implementation details behind clean interfaces
- Inheritance promotes code reuse across related classes
- Polymorphism allows flexible, extensible behavior via virtual functions
- Abstraction simplifies complex systems into manageable interfaces
- Improves maintainability and modularity of large codebases
AI Mentor Explanation
OOP is like how a cricket academy defines a general 'Player' blueprint with shared attributes, then batsmen and bowlers extend it with their own specialized skills through inheritance. Each player object encapsulates their own stats privately, exposing only a public scorecard.
Step-by-Step Explanation
Step 1
Define a class
A class bundles related data members and member functions into a single blueprint.
Step 2
Encapsulate state
Mark data members `private` and expose controlled access through `public` methods.
Step 3
Instantiate objects
Create concrete objects from the class, each with its own copy of the member data.
Step 4
Apply inheritance
Derive a new class from a base class using `: public Base` to reuse and extend behavior.
Step 5
Enable polymorphism
Declare base class methods `virtual` so a derived class's override runs through a base pointer or reference.
Step 6
Abstract the interface
Use abstract classes with pure virtual functions (`= 0`) to define a contract without an implementation.
What Interviewer Expects
- Names all four pillars: encapsulation, inheritance, polymorphism, abstraction
- Can explain virtual functions and dynamic dispatch
- Distinguishes a class (blueprint) from an object (instance)
- Knows access specifiers `public`/`private`/`protected`
- Gives a concrete C++ example of inheritance or polymorphism
Common Mistakes
- Listing the pillars without explaining what each actually does
- Confusing function overloading with polymorphism
- Forgetting `virtual` and expecting dynamic dispatch anyway
- Treating a struct and class as fundamentally different concepts in C++
- Not mentioning that abstraction can be achieved through abstract base classes
Best Answer (HR Friendly)
“OOP in C++ is a way of structuring code around real-world-like objects that bundle data and behavior together, similar to how a car has both properties and actions. It helps developers build software that's easier to reuse, extend, and maintain as it grows.”
Code Example
#include <iostream>
#include <memory>
class Shape {
public:
virtual double area() const = 0; // abstraction: pure virtual
virtual ~Shape() = default;
};
class Circle : public Shape {
private:
double radius; // encapsulation: private state
public:
explicit Circle(double r) : radius(r) {}
double area() const override { return 3.14159 * radius * radius; }
};
int main() {
std::unique_ptr<Shape> s = std::make_unique<Circle>(2.0);
std::cout << s->area() << "\n"; // polymorphism: 12.5664
return 0;
}Follow-up Questions
- What is the difference between compile-time and runtime polymorphism?
- Why do base class destructors need to be virtual?
- What is the difference between an abstract class and an interface in C++?
- How does multiple inheritance work and what problems can it cause?
- What is the difference between `public`, `private`, and `protected` inheritance?
MCQ Practice
1. Which of these is NOT one of the four pillars of OOP?
The four pillars are encapsulation, inheritance, polymorphism, and abstraction. Compilation is unrelated to OOP concepts.
2. What keyword enables runtime polymorphism in C++?
Declaring a base class method `virtual` allows derived class overrides to be selected at runtime through a base pointer or reference.
3. What does encapsulation primarily achieve?
Encapsulation hides a class's internal data and exposes only a controlled public interface to interact with it.
Flash Cards
What are the four pillars of OOP? — Encapsulation, inheritance, polymorphism, and abstraction.
What enables runtime polymorphism in C++? — The `virtual` keyword on base class member functions.
What is a class vs an object? — A class is a blueprint; an object is a concrete instance created from that blueprint.
How do you define an abstract class in C++? — Give it at least one pure virtual function, declared with `= 0`.