Why Object-Oriented Programming?
So far, you’ve built two kinds of C++ programs:
- Procedural: The program moves through a linear series of instructions.
- Functional: The program moves from one function to another.
But there is another very common way to structure C++ code: object-oriented programming.
Let’s consider a physical object: a light bulb. A light (usually) has two possible states: on and off. It also has functionality that allows you to change its state: you can turn it on and you turn it off. Thankfully, you don’t need to know electrical engineering to use the light! You only need to know how to interact with it.
Object-oriented programming (OOP) is a programming paradigm that allows you to package together data states and functionality to modify those data states, while keeping the details hidden away (like with the lightbulb). As a result, code with OOP design is flexible, modular, and abstract. This makes it particularly useful when you create larger programs.
In C++, you can apply OOP in your code with classes and objects. And the C++ objects you create will have states and functionality.
There are four major benefits to object-oriented programming:
- Encapsulation: in OOP, you bundle code into a single unit where you can determine the scope of each piece of data.
- Abstraction: by using classes, you are able to generalize your object types, simplifying your program.
- Inheritance: because a class can inherit attributes and behaviors from another class, you are able to reuse more code.
- Polymorphism: one class can be used to create many objects, all from the same flexible piece of code.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Functional Programming in Python
An intro to functional programming in Python - Article
What Different Kinds of Programmers Do
Get quick insights into what different types of programmers do and what their work is like! - Article
What is Programming?
Programming is a collaboration between humans and computers.
Learn more on Codecademy
- Free course
Learn Intermediate Python 3: Object-Oriented Programming
Learn how to utilize one of the most popular programming paradigms - Object-Oriented Programming.Intermediate2 hours - Free course
Software Engineering in Python I
Learn about the fundamental principles that differentiate programming for data science from programming for engineering.Beginner Friendly8 hours - Free course
Learn PHP: Objects and Classes
Learn how to make your own classes and initialize objects based on the defined classes.Beginner Friendly1 hour