Object Oriented in C++: Introduction

 Before going into detail about a new programming paradigm (object oriented), let's understand a little what we were going to do and what we are going to do differently.


Functional Programming

So far, we have only used one programming style: functional, also called procedural. As the name says, it is a method that uses procedures, or as we call it: functions.

Basically, it is a script, a routine of procedures and commands. In other words, we only use variables and functions. Let's take an example.

Suppose you want to calculate the arithmetic average of two numbers, in C++. Necessarily, you will have to have two variables to store the values and an average calculation. There, there is a 'script' of what should be done.

What you can do differently, is to put the average in a function, so that it can be invoked indefinitely:

#include <iostream>
using namespace std;

float average(float num1, float num2)
{
    return (num1+num2)/2;
}

int main()
{
    float num1, num2;

    cout<<"Number 1: ";
    cin >> num1;

    cout<<"Number 2: ";
    cin >> num2;

    cout<<"Average: "<< average(num1,num2) << endl;

    return 0;
}

See how this program is just a procedure, it starts running from the beginning of main() and goes to the end of it. Always. Everything we did was always like this. It has a beginning (usually variable declaration), a middle (calling functions to do various things) and an end (showing results).

The purpose of functional programming is to create functions that do things. It may seem simple and fool right? But incredible things were done using this. The Linux Kernel, for example, does not use C++, only C, that is, it has no object oriented, only procedural programming.

Over the years and decades, software has become more and more and more and more, and a little more, complex. And some problems were emerging.


Oriented Object Programming

So far, in our programs, any function could work on any data. This over time became a security issue. It would be ideal if certain functions could act only on some data.

For example, in functional programming, functions that work with a company's Treasury data could work with any data, such as that of Employees. But it would be better if the treasury had its own functions and to work with employee data, they had their own functions as well. And for the sake of security, none could touch the things of others.

Another problem: you assign a function to receive an integer. Someone accidentally uses this function and sends a float. If you do this test, it will give an error, the wrong answer will come out and it can even simply close the program by running. Can you imagine 'closing' the program of an airplane, in mid-flight? It's not cool, right?

Hence the blessed and beautiful OOP: Object Oriented Programming. It solves these problems, and in an incredibly simple and easy way. Its secret is: it starts working with something called an object.

Each object will have its own variables and only a few functions can see it act on it.

If you have a Sound object in your game, it will have specific variables, characteristics and functions acting on it. Character type objects will have specific variables, characteristics and functions for them. A function that affects Sound cannot act on a Character type object. And, wow, that avoids many bugs and potential problems.

That of each object having its data and procedures, is the so-called encapsulation, the basis of the OOP logic. It is as if there is a specific code for each 'thing'. A function only sees data for that 'thing' and can only act on that 'thing'. You can even hide information, say clearly: 'Hey, this variable here, which stores the password, can only be seen here inside the server, it is inaccessible to users outside'. And that brings incredible security.

If you are developing a game in C++ and use object orientation, you will create data and procedures that will only act and make sense in the Logic part. It will create information and functions that will only be visible and will only act on Graphics, you will create specific things for the Scenario (which are not even visible outside this scope). You encapsulate, you divide, you organize things ... are you getting the idea of OOP?

It is no longer that mess of functions and variables that everyone can see and use. A declared variable should generally only be used by the X() function. But the Y() function can indeed see and act on this variable, this is an error, a problem, and it would be interesting if this were naturally impossible, if the language itself made this separation. And it is this separation that Object Oriented does.

If you make a Web system, you want only a few functions to be available to users, such as the function Display(), which will show the grades of an Distance Education student. But if you used functional programming, the evil student can create code that calls the ChangeGrade() function, to change your grades, to hack the system. Well, he may well "guess" the name of the functions, and he will get it right...

With object-oriented programming, we can make it very clear: "Hey system, only this function and these variables can be accessed by students". When he tries to invade the system, calling other functions that are not part of that 'object' (his grades, for example), he will be summarily blocked.

This object orientation is cool, right? But let's stop talking and go to the next tutorial and start learning how to use this awesome thing for good.

Study resources

Programming Paradigm
Functional Programming

No comments:

Post a Comment