← Back to C++ Course | Chapter 8: OOP Core | Lesson 1 of 14

C++ OOP Introduction

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?

cpp
#include <iostream>

class Car {
public:
	void drive() { std::cout << "Driving" << std::endl; }
};

int main() {
	Car myCar;
	myCar.drive();
	return 0;
}

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

cpp
#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;
}

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

cpp
#include <iostream>

class Car {
public:
	void start() { std::cout << "Car started" << std::endl; } // hides engine details
};

int main() {
	Car car;
	car.start();
	return 0;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.