• Register
31,070 points
9 4 2
Welcome to Kodlogs, programming questions and answer website.

C ++ is an object-oriented language, and as such, we can add types of operations to our program to simplify some tasks. For example, we create an Obj object that has two variables of type integer inside.

 We can make that when adding two of those objects, operations are performed between them to return a new object. This is done by overloading operators.

We are going to create an object that contains two data: x and y. It will be a vector.

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class vct {</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

    public:</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

        int x, y;</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

        vct (int xVal, int yVal);</font></font><font></font>

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        vct operator + (vct b);</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

};</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

vct :: vct (int xVal, int yVal) {</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

    x = xVal;</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

    y = yVal;</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

}</font></font>

Now we want to add two vectors, that is, operate with two objects vct. The operation could be done using a function that has two variables of type vct as parameters, but with the operator overload we will have a cleaner and more elegant code. Generally, you can do it in two ways:

Method 1: outside the classroom:

<font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">vct operator + (vct a, vct b) {</font></font><font>

</font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    vct result (a.x + bx, a.y + by);</font></font><font>

</font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    return result;</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

}</font></font>

The sum (+) operator is overloaded for the case in which two objects vct are involved. A variable of type vct is created that will have as values ​​the sum of the " x"  and the "y" of each object. There is a faster and more efficient way, which is to avoid creating that result variable:

<font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">vct operator + (vct a, vct b) 

{</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    return * (new vct (a.x + bx, a.y + by));</font></font><font></font>

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

}</font></font>

Method 2: inside the class.

A statement is defined within the class. In this way, the first object that intervenes will be the one that is being used. In other words, within the operator's overload function you can make use of the object's own variables.

<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">
class vct {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    public:</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        int x, y;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        vct (int xVal, int yVal);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        vct operator + (vct b); </font><font style="vertical-align: inherit;">

// Statement within the class.</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

};</font></font><font></font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

vct :: vct (int xVal, int yVal) {</font></font><font>

</font><font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">

    x = xVal;</font></font><font></font>

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    y = yVal;</font></font><font></font>

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

}</font></font><font></font>

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

vct vct :: operator + (vct b) 

{// Operator overload function.</font></font><font></font>

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    return * (new vct (x + bx, y + by));</font></font><font></font>

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
}</font></font>

With the operator overload, you can work in the following way:

<font style="vertical-align: inherit;">

<font style="vertical-align: inherit;">vct a (3,6), b (5, -2);</font></font><font>

</font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

vct c = a + b;</font></font>

The variable c will be an object vct that in this situation will contain x = 3 + 5 = 8, y = 6 + (- 2) = 4.

Now, you can experiment with more complex objects and overloading multiplication, division, or even remainder operators.

31,070 points
9 4 2