What is object oriented programming and why is it used?
Reviewing the reason for the popularity and widespread usage of object oriented programming is important for everyone interested in languages that favor such a programming style.
Simply put, object oriented programming is the idea that your code is broken down into uniquely controlled and accessible sections of data.
These "sections" are objects; data types with uniquely modified traits that allow program parts to interact with less overhead.
But why are they used?
The most common reason would be using sets of data in different parts of a program without having a developer making special calls or functions to execute what can be re-used.
What is an example of an object in a program?
Think of a simple button.
In most situations, a button has a singular purpose tied to it's overall function. If someone presses the "home" button on a web browser, they expect their browser to take them to the webpage they set as their "home" website.
Another example is a "New Game" button in a videogame. A player pressing the "New Game" button knows exactly what the button is supposed to do; it's supposed to start the sequence of code that allows a player to start a game from the beginning.
Here is a sample of some actual C++ code:
// New game
if (this->buttons["GAME_STATE"]->isPressed()) {
this->states->push(new GameState(this->stateData));
}
The "Game State" button is pressed, but the button itself says "New Game" and is meant to start the events that begin the game. There's a complex unseen event sequence happening here where the program will load in a bunch of relevant data; this includes players, enemies, map data, in-game objects, and more!
All from the press of the 'button' object.
This should help clarify for anyone who might not fully understand the true nature of "object oriented" programming.
It is a complex but highly modular way to design your programs to design code to execute in highly specialized ways.
Top comments (0)