DEV Community

Ian
Ian

Posted on

Really understanding "objects" in object-oriented programming.

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));
}
Enter fullscreen mode Exit fullscreen mode

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.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay