C++ OOP Introduction
In this page:
What is OOP?
Object-Oriented Programming (OOP) organizes code around objects — bundles of data and behavior — rather than around a sequence of actions. By modeling real-world entities as classes and objects, OOP encourages you to write programs that are modular, reusable, and easier to reason about as they grow.
Example: What is OOP?
#include <iostream>
class Car {
public:
void drive() { std::cout << "Driving" << std::endl; }
};
int main() {
Car myCar;
myCar.drive();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Encapsulation
Encapsulation bundles a class's data variables together with the methods that operate on them into a single unit, and restricts direct outside access to that internal state. This protects an object's data from being changed in ways that would leave it in an invalid or inconsistent condition.
Example: Encapsulation
#include <iostream>
class Account {
private:
double balance = 0;
public:
void deposit(double amount) { balance += amount; }
double getBalance() { return balance; }
};
int main() {
Account acc;
acc.deposit(100);
std::cout << acc.getBalance() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Abstraction
Abstraction hides the complex inner implementation of an object and exposes only the essential features a user actually needs. This simplifies how other code interacts with your classes, letting callers focus on what an object does rather than how it does it internally.
Example: Abstraction
#include <iostream>
class Car {
public:
void start() { std::cout << "Car started" << std::endl; } // hides engine details
};
int main() {
Car car;
car.start();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Inheritance
Inheritance lets a new class acquire the properties and methods of an existing class, establishing a parent-child relationship between them. This reduces redundant code by letting related classes share a common base instead of each reimplementing the same behavior from scratch.
Example: Inheritance
#include <iostream>
class Animal {
public:
void eat() { std::cout << "Eating" << std::endl; }
};
class Dog : public Animal {
public:
void bark() { std::cout << "Barking" << std::endl; }
};
int main() {
Dog d;
d.eat();
d.bark();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Polymorphism
Polymorphism literally means 'many forms' — it lets you call the same function name on objects of different classes and have each one respond in its own specific way. This is what allows generic code written against a base type to work correctly with any derived type, present or future.
Example: Polymorphism
#include <iostream>
class Animal {
public:
virtual void speak() { std::cout << "Some sound" << std::endl; }
};
class Cat : public Animal {
public:
void speak() override { std::cout << "Meow" << std::endl; }
};
int main() {
Animal *a = new Cat();
a->speak();
delete a;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: