OOP Concepts Overview
Object-Oriented Programming (OOP) is a paradigm built around objects rather than functions. C++ supports four core OOP pillars: encapsulation, inheritance, polymorphism, and abstraction.
Encapsulation
Bundling data and methods inside a class while restricting direct access to internal state.
This is achieved with private members and public getters/setters.
Inheritance
A derived class inherits members from a base class, promoting code reuse.
Use the colon syntax: class Dog : public Animal.
Polymorphism
The ability to present the same interface for different underlying types. Virtual functions allow a derived class to override a base class method.
Abstraction
Hiding complex implementation details and showing only essential features. Abstract base classes with pure virtual functions enforce this contract.
#include <iostream>
using namespace std;
class Animal {
public:
virtual void speak() {
cout << "Animal speaks\n";
}
};
class Dog : public Animal {
public:
void speak() override {
cout << "Dog barks\n";
}
};
int main() {
Animal* a = new Dog();
a->speak();
delete a;
return 0;
}
Try it Yourself →