C++ OOP Introduction
In this page:
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?
// 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;
}
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 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;
}
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 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;
}
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 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;
}
Login to try C/C++/Java/PHP code in the editor
- Making every data member public, which breaks encapsulation and lets any code corrupt the object's state.
- Confusing a class (the blueprint) with an object (a real instance created from it).
- Using inheritance only to reuse code, when it should model an is-a relationship.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: