0
0
C++programming~10 mins

OOP principles overview in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - OOP principles overview
Start
Encapsulation
Inheritance
Polymorphism
Abstraction
End
This flow shows the four main OOP principles in order: Encapsulation, Inheritance, Polymorphism, and Abstraction.
Execution Sample
C++
#include <iostream>

class Animal {
  public:
    virtual void sound() { }
};

class Dog : public Animal {
  public:
    void sound() override { std::cout << "Woof"; }
};
Defines a base class Animal and a derived class Dog that overrides the sound method.
Execution Table
StepActionEvaluationResult
1Create Animal objectAnimal a;Object a created
2Call a.sound()a.sound();No output (empty method)
3Create Dog objectDog d;Object d created
4Call d.sound()d.sound();Outputs: Woof
5Assign Dog to Animal pointerAnimal* ptr = &d;Pointer ptr points to Dog object
6Call ptr->sound()ptr->sound();Outputs: Woof (polymorphism)
7EndExecution stops
💡 Program ends after demonstrating polymorphism with virtual function call
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
anoneAnimal objectAnimal objectAnimal objectAnimal object
dnonenoneDog objectDog objectDog object
ptrnonenonenonePoints to Dog objectPoints to Dog object
Key Moments - 3 Insights
Why does calling sound() on Animal pointer to Dog object output 'Woof'?
Because sound() is declared virtual in Animal, the call uses Dog's overridden method (see execution_table step 6). This is polymorphism.
What is encapsulation in this example?
Encapsulation means bundling data and methods inside classes like Animal and Dog, hiding internal details from outside (shown by class definitions).
How does inheritance work here?
Dog inherits from Animal, so Dog has Animal's interface and can override methods like sound() (see class Dog : public Animal).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does step 4 output?
ANo output
BWoof
CError
DAnimal sound
💡 Hint
Check the 'Result' column in execution_table row for step 4
At which step does polymorphism happen?
AStep 2
BStep 4
CStep 6
DStep 3
💡 Hint
Look for virtual function call through base class pointer in execution_table
If sound() was not virtual, what would ptr->sound() output at step 6?
ANo output
BWoof
CCompile error
DAnimal sound
💡 Hint
Without virtual, base class method is called regardless of pointer type
Concept Snapshot
OOP Principles Overview in C++:
- Encapsulation: Bundle data and methods in classes
- Inheritance: Derive new classes from existing ones
- Polymorphism: Use virtual functions to override behavior
- Abstraction: Hide complex details behind simple interfaces
Use virtual keyword for polymorphism.
Full Transcript
This visual execution shows the four main OOP principles using C++ code. We start by creating an Animal object with an empty sound method. Then we create a Dog object that inherits Animal and overrides sound to print 'Woof'. Calling sound on Dog outputs 'Woof'. Assigning Dog to an Animal pointer and calling sound demonstrates polymorphism because the Dog's method runs. Encapsulation is shown by bundling data and methods inside classes. Inheritance lets Dog reuse Animal's interface. Polymorphism uses virtual functions to call the right method at runtime. The quiz checks understanding of outputs and polymorphism behavior.