Get and Set Functions, Const and Object-Oriented Information Security

In this tutorial of our C++ course, we will use one more example of Class and Object, this time to better understand the use of setters and getters functions, as well as making our code more secure, through the keyword const.

Object-Oriented Get and Set Functions

Let's do another class example! Now we are going to work with a rectangle, which is different from the square that only has one side of equal value, the precise rectangle of the value of two sides. Let's call it length and width.

The function that takes the value of the area, let's call getArea() and the one that returns the perimeter value of getPerimeter() ... from get, which here means to get, access ...

As our variables are private, after all it is a security issue that no one from outside touches or sees them, we will need functions to set the value of these variables, they will be setLength() and setWidth() ... from set ...which means to define, to change.

See how our code looks:

#include <iostream>
using namespace std;

class Rect
{
    private:
        double length, width;

    public:
        double getArea();
        double getPerimeter();
        void setLength(double);
        void setWidth(double);
};

double Rect::getArea()
{
    return length * width;
}
double Rect::getPerimeter()
{
    return 2*(length+width);
}
void Rect::setLength(double l)
{
    length = l;
}
void Rect::setWidth(double w)
{
    width = w;
}

int main()
{
    Rect r;
    double var;

    cout<<"Length: ";
    cin >> var;
    r.setLength(var);

    cout<<"Width: ";
    cin >> var;
    r.setWidth(var);

    cout<<"Area: " << r.getArea() << endl;
    cout<<"Perimeter: " << r.getPerimeter() << endl;

    return 0;
}

In main(), we instantiate an object 'r' of type Rect, and declare a variable 'var' that will receive the values that the user will type, for the values of length and width.

First he types the value for the length, and we set that value using setLength().
Then he types the value for the width, and we set that value using setWidth().

Then just display the area and perimeter value, using getArea() and getPerimeter().

That is, we use getters and setters to access and change variables. Look again: no one in the outside world, not even the best hacker on the planet, will have direct access to these variables, only through the set and get functions.

Information Security with Object Orientation

"Well, we don't have access to variables directly, but we have through the functions of Set and Get, which is the same thing".

Well, almost, but there is a reason. You would still need to guess the name of these functions, to have access to them, but I do not disagree with you, this security has not yet been tested.

An example: a naughty hacker could very well set negative values for the sides of the rectangle, which is absurd! The sides of a rectangle must be positive!

That's where the magic of object orientation comes in! Let's change our setLength() and setWidth() functions, they will look like this:

void Rect::setLength(double l)
{
    while(l<=0){
        cout << "Negative length! Type a valid value: ";
        cin >> l;
    }
    length = l;
}
void Rect::setWidth(double w)
{
    while(w<=0){
        cout << "Negative widh! Type a valid value: ";
        cin >> w;
    }
    width = w;
}

Friends, as long as the value of l or w is negative, it will fall into the while and only leave that while when the value entered is greater than 0, and there is no chat, it may be the best hacker in the world, it will not circumvent it.

The keyword const in Object Orientation

As we said, the purpose of the getters functions is simply to take values, access this information, never, never, change it.

You can work in the treasury of a company and make the function getPaycheck() available to the rest of the company, for each one to see the salary amount ... but under no circumstances should these people be able to change the amount presented (in this case, the salary. ..imagine how wonderful it would be to be able to change your salary?)

One way to ensure that these functions are access only, is to use the keyword const, both in the header within the class definition and in the function declaration.

Now see how our code is complete, safe, beautiful and wonderful:

#include <iostream>
using namespace std;

class Rect
{
    private:
        double length, width;

    public:
        double getArea() const;
        double getPerimeter() const;
        void setLength(double);
        void setWidth(double);
};

double Rect::getArea() const
{
    return length * width;
}
double Rect::getPerimeter() const
{
    return 2*(length+width);
}
void Rect::setLength(double l)
{
    while(l<=0){
        cout << "Negative length! Type a valid value: ";
        cin >> l;
    }
    length = l;
}
void Rect::setWidth(double w)
{
    while(w<=0){
        cout << "Negative widh! Type a valid value: ";
        cin >> w;
    }
    width = w;
}
int main() { Rect r; double var; cout<<"Length: "; cin >> var; r.setLength(var); cout<<"Width: "; cin >> var; r.setWidth(var); cout<<"Area: " << r.getArea() << endl; cout<<"Perimeter: " << r.getPerimeter() << endl; return 0; }

Object Oriented Exercise

Increment the previous example. Make it also display the sides of the rectangle, to do this define and use the getLength() and getWidth() functions, use the const keyword to make these functions more secure and hacker-proof.

No comments:

Post a Comment