Accessing members of Classes and Objects: private and public

In the tutorial past our course, we learned how to create classes and objects in C++. Now, let's see how to use this knowledge, accessing the members (variables and functions) of an object / class.

Accessing members of Classes and Objects: .

At the end of our previous tutorial, we had the code that created a Square class and the object 'q'. Do the following, run this code. Obviously, nothing happened. We define a class, instantiate an object, and that's it.

We will now learn how to access these members of objects, using the dot operator: .

See that we have the object 'q'. It has the variable 'side' and the function 'area', which can be accessed:

  • q.side;
  • q.area();

Just put the name of the object, followed by the dot operator and the name of the member, defined in the class, just as we do with structs. Let's create a program that defines a value for the side, and then call the area() function to print the value of the area of that square:

#include <iostream>
using namespace std;

class Square
{
    double side;
    double area();
};

double Square::area()
{
    return side*side;
}

int main()
{
    Square q;
    q.lado = 2;
    cout<<"Area: " << q.area() << endl;

    return 0;
}

Run this code and see the result. The error will appear:
‘Double Square :: side’ is private within this context |

In other words, you're saying that the 'side' variable is private! What is it? And now, Jose?

Access specifiers: public and private

Remember that when we talk about class and objects, we said that it has a special power: to make some things public for all the code and other things private, which can be accessed only by some elements? Yeah, that's it.

As we have not defined how these members will access, C++ has put them by default as private (ie secret).

Since everything is private, only the internal members of the objects can access this data. That is, only one function of the object can access and modify the value of that object's variable.

But, we want to access this variable in the main() function, an outside function, nothing to do with the object / class. So, we need to define these members as public.

To do this, just type "public:" before what we want to define as public, see how our class looks:

class Square
{
    public:
        double side;
        double area();
};

Make that small change to your code, and run it. The result will be:


Now, if I defined the side as having a value of 2, the area will be 2x2 = 4, that's correct!
See how wonderful this object-oriented programming ... I created an object named 'q', and it automatically came with the variable 'side' and the function 'area()'.
Information security in Classes and Objects

Nice ... very beautiful ... but there is a problem there ... this 'side' variable, it is visible and accessible to any part of the program. Imagine if it is part of your company's Treasury code, would you find it interesting that someone from another sector had access to this variable?
It's a security breach, do you agree?

How about if only one function of class Square had access to the variable 'side'. It would make more sense, wouldn't it?

It would also make sense for the 'side' variable to be private, no one could tamper with it outside the object's scope.

We will then create a function called 'define(double)' that receives a variable the arrow that value for the variable 'side'.

Our more secure and professional code will look like this:
#include <iostream>
using namespace std;

class Quadrado
{
    public:
        void define(double);
        double area();
    private:
        double side;
};

void Square::define(double l)
{
    lado = l;
}
double Square::area()
{
    return side*side;
}

int main()
{
    Square q;
    q.define(2);
    cout<<"Area: " << q.area() << endl;

    return 0;
}

That is, we now define the value of the side of the square using the define() function, and no longer having direct access to the 'side' variable. In fact, you can create 1 billion objects now, you will not be allowed to modify or directly access the 'side' variable of any of them!

Do you want to change the side value? Asks the function Square.define()
Want to know the area of that square? Asks the function Square.area()

The communication of objects with the external world is only by functions, and this is very interesting because we can have greater control over some information.

Object Oriented Exercises

1. You need to know the side of an object of type Square, create a function that returns the value of the side of that square, incrementing the previous code.

2. When defining the side of the square, obviously it cannot be 0 or less, make the function define() only accept positive values from the side to the square.

No comments:

Post a Comment