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

C++ OOP Introduction

Object-oriented programming organizes code like real-world things, each with facts and actions, such as a dog with a name that can bark. It keeps large programs neat.
Syntax
cpp
class ClassName {
private:
  data_type member_variable;
public:
  return_type method_name() { }
};

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 std::cout and std::cin
#include <iostream>

// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
	void drive() { std::cout << "Driving" << std::endl; }
};

// Program execution starts in main()
int main() {
	Car myCar;
	myCar.drive();
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Define the type Account
class Account {
// Members below are only accessible inside the class
private:
	double balance = 0;
// Members below can be accessed from outside the class
public:
	void deposit(double amount) { balance += amount; }
	double getBalance() { return balance; }
};

// Program execution starts in main()
int main() {
	Account acc;
	acc.deposit(100);
	// Print to the console with cout
	std::cout << acc.getBalance() << std::endl;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	void eat() { std::cout << "Eating" << std::endl; }
};

// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
	void bark() { std::cout << "Barking" << std::endl; }
};

// Program execution starts in main()
int main() {
	Dog d;
	d.eat();
	d.bark();
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
	virtual void speak() { std::cout << "Some sound" << std::endl; }
};

// Define the type Cat
class Cat : public Animal {
// Members below can be accessed from outside the class
public:
	void speak() override { std::cout << "Meow" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Animal *a = new Cat();
	a->speak();
	// Free the heap memory with delete
	delete a;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Making every data member public, which breaks encapsulation and lets any code corrupt the object's state.
  2. Confusing a class (the blueprint) with an object (a real instance created from it).
  3. Using inheritance only to reuse code, when it should model an is-a relationship.

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.