C++ Operator Overloading

This article provides a guide to operator overloading in C++. Operator overloading is a useful and powerful feature of the C++ programming language. C++ allows overloading of most built-in operators. In this tutorial, we will use several examples to demonstrate the operator overloading mechanism.

What is Operator?

An operator is a symbol that indicates to the compiler to perform a particular operation. For example, there are various types of operators in C++, such as Arithmetic Operators, Logical Operators, Relational Operators, Assignment Operators, Bitwise Operators, and more.

What is Operator Overloading?

The C++ language allows programmers to give special meanings to operators. This means that you can redefine the operator for user-defined data types in C++. For example, “+” is used to add built-in data types, such as int, float, etc. To add two types of user-defined data, it is necessary to overload the “+” operator.

Syntax for Operator Overloading

C++ provides a special function called “operator” for operator overloading. The following is the syntax for operator overloading:

class sampleClass
{
    ..............
    Public:
       returnType operator symbol (arguments) {
           ..............
       }
     ..............
};

Here, “operator” is a keyword, and “symbol” is the operator that we want to overload.

Examples

Now that you understand the overall concept of operator overloading, let us go through a couple of working example programs for you to understand this idea more concretely. We will cover the following examples:

  1. Example 1: Unary Operator Overloading (1)
  2. Example 2: Unary Operator Overloading (2)
  3. Example 3: Binary Operator Overloading
  4. Example 4: Relational Operator Overloading

Example 1: Unary Operator Overloading (1)

In this example, we will demonstrate how a unary operator can be overloaded in C++. We have defined the class, “Square_Box,” and the public functions, “operator ++ ()” and “operator ++ (int),” to overload both the prefix and the postfix increment operators. In the “main()” function, we have created the object, “mySquare_Box1.” We have then applied the prefix and postfix increment operators to the “mySquare_Box1” object to demonstrate the unary operator overloading.

#include <iostream>
using namespace std;

class Square_Box
{
   private:
    float length;
    float width;
    float height;

   public:
    Square_Box() {}
    Square_Box(float l, float w, float h)
    {
        length = l;
        width = w;
        height = h;
    }

    // Operator Overloading  - "++" prefix operator
    void operator ++ ()
    {
        length++;
        width++;
        height++;
    }

    // Operator Overloading  - "++" postfix operator
    void operator ++ (int)
    {
        length++;
        width++;
        height++;
    }

    void output()
    {
        cout << "\tLength = " << length << endl;
        cout << "\tWidth = " << width << endl;
        cout << "\tHeight = " << height << endl;
        cout << endl;
    }
};

int main()
{
    Square_Box mySquare_Box1(3.0, 5.0, 6.0);

    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();
   
    mySquare_Box1++;
   
    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();
   
    ++mySquare_Box1;
   
    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();    

    return 0;
}

Example 2: Unary Operator Overloading (2)

This is another example in which we will demonstrate how a unary operator can be overloaded in C++. We have defined the class, “Square_Box,” and the public functions, “operator — ()” and “operator — (int),” to overload both the prefix and postfix decrement operators. In the “main()” function, we have created the “mySquare_Box1” object. We have then applied the prefix and postfix decrement operators to the “mySquare_Box1” object.

#include <iostream>
using namespace std;

class Square_Box
{
   private:
    float length;
    float width;
    float height;

   public:
    Square_Box() {}
    Square_Box(float l, float w, float h)
    {
        length = l;
        width = w;
        height = h;
    }

    // Operator Overloading  - "--" prefix operator
    void operator -- ()
    {
        length--;
        width--;
        height--;
    }

    // Operator Overloading  - "--" postfix operator
    void operator -- (int)
    {
        length--;
        width--;
        height--;
    }

    void output()
    {
        cout << "\tLength = " << length << endl;
        cout << "\tWidth = " << width << endl;
        cout << "\tHeight = " << height << endl;
        cout << endl;
    }
};

int main()
{
    Square_Box mySquare_Box1(3.0, 5.0, 6.0);

    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();
   
    mySquare_Box1--;
   
    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();
   
    --mySquare_Box1;
   
    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();    

    return 0;
}

Example 3: Binary Operator Overloading

Now, we will look at an example of binary operator overloading. The syntax for binary operator overloading will be somewhat different from unary operator overloading. In this example, we will overload the “+” operator to add two “Square_Box” objects.

#include <iostream>
using namespace std;

class Square_Box
{
   private:
    float length;
    float width;
    float height;

   public:
    Square_Box() {}
    Square_Box(float l, float w, float h)
    {
        length = l;
        width = w;
        height = h;
    }

    // Operator Overloading  - "+" operator
    Square_Box operator + (const Square_Box& obj)
    {
        Square_Box temp;
        temp.length = length + obj.length;
        temp.width = width + obj.width;
        temp.height = height + obj.height;
        return temp;
    }

    void output()
    {
        cout << "\tLength = " << length << endl;
        cout << "\tWidth = " << width << endl;
        cout << "\tHeight = " << height << endl;
        cout << endl;
    }
};

int main()
{
    Square_Box mySquare_Box1(3.0, 5.0, 6.0), mySquare_Box2(2.0, 3.0, 5.0), result;

    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();

    cout << "Dimensions of mySquare_Box2 = " << endl;
    mySquare_Box2.output();
   
    result = mySquare_Box1 + mySquare_Box2;
   
    cout << "Dimensions of resultant square box = " << endl;
    result.output();

    return 0;
}

Example 4: Relational Operator Overloading

Now, we will look at an example of relational operator overloading. The syntax for relational operator overloading is just like that of the binary operator overloading. In this example, we will overload the “<” and “>” operators to apply to the “Square_Box” objects.

#include <iostream>
using namespace std;

class Square_Box
{
   private:
    float length;
    float width;
    float height;

   public:
    Square_Box() {}
    Square_Box(float l, float w, float h)
    {
        length = l;
        width = w;
        height = h;
    }

    // Operator Overloading  - "<" operator
    bool operator < (const Square_Box& obj)
    {
        if(length < obj.length)
            return true;
        else
            return false;
    }

   
    // Operator Overloading  - ">" operator
    bool operator > (const Square_Box& obj)
    {
        if(length > obj.length)
            return true;
        else
            return false;
    }
    void output()
    {
        cout << "\tLength = " << length << endl;
        cout << "\tWidth = " << width << endl;
        cout << "\tHeight = " << height << endl;
        cout << endl;
    }
};

int main()
{
    Square_Box mySquare_Box1(2.0, 3.0, 5.0), mySquare_Box2(4.0, 6.0, 8.0);
    bool result;

    cout << "Dimensions of mySquare_Box1 = " << endl;
    mySquare_Box1.output();

    cout << "Dimensions of mySquare_Box2 = " << endl;
    mySquare_Box2.output();
   
    result = mySquare_Box1 < mySquare_Box2;
    cout << "mySquare_Box1 < mySquare_Box2 = " << result < mySquare_Box2;
    cout < mySquare_Box2 = " << result << endl;    

    return 0;
}

Conclusion

C++ is a general-purpose and flexible programming language that is widely used in a variety of domains. This programming language supports both compile-time and run-time polymorphism. This article showed you how to perform operator overloading in C++. This is a very useful feature of C++ that adds some extra effort for the developer to define the operator for overloading, but it definitely makes life easier for the user of the class.



from Linux Hint https://ift.tt/39PNFsl

Post a Comment

0 Comments