c++ operator overloading

C++ operator overloading

In this article, you will learn about why or how to the operator overloading and complete the concept of operator overloading with examples.

The operators are used for the primitive data type like +,-, and / same enables the usage of these operators for user-defined data type known as operator overloading.

C++ allows the programmer to use operators for the user-defined data type. In order to add the two objects of the class, usage of the operator overloading makes it very easy to add the two objects.

Why operator overloading used?

The c++ language gives the facility to programmer to use the operators with the objects of the class rather than the usage of the difficult code to perform operations.

The operator overloading give a lot of easy to programmer to perform different operation with objects of the classes.

How to operator overload?

The following is the syntax of the operator overloading.
Syntax


Operator keyword followed by the symbol of the operator.

=>  In the parenthesis, implement the operation programmer want to perform from the specific operator.

=>  usually operator function writes in the public part of the class.
=>  symbol of the operator used.
=>  pass parameter as pass through in the regular function in the c++.

Difference between operator overload and normal function


Operator overloading function and normal function have only difference operator keyword and operator symbol otherwise both perform a specific function. Must be not confused in both operator overload function work when operator use with the object and normal function work when it is called.

Example operator overloading on c++

#include<iostream>
using namespace std;

class myhouse{ //define the myhouse class


public:
int numberofroom;
int numberfloor;
myhouse(int room, int floor){ //constructor
numberofroom = room;
numberfloor = floor;
}


void display(myhouse &obj) //passing the object in the function
{
cout<<"number of floor ="<< obj.numberfloor <<endl;
cout<<" number of room ="<< obj.numberofroom <<endl;
}

void operator++()
{
 
numberfloor++;
numberofroom++;
}
};


int main()
{

myhouse obj2(20,30); //create object
//get new object
obj2.display(obj2);                //display function call before increment
++obj2;                //use increment operator
obj2.display(obj2);                    //display function after usage of operator
return 0;
}

output:
number of floor =30
number of room =20
number of floor =31
number of room =21



In the above example, pre-increment operator overload .The operator function defined inside the class void operator ++(). This function increment in the number of floors and number of rooms of the object of the class myhouse  by count 1.

Can we overload all the operators?

No, we can not overload all the operators available in the c++ language.
The following are the operators that are not overload in the cpp.
=>  ::
=>  ?:
=> .(dot)
=> sizeof


Take care during the overloading the operators

=>  The operator is binary or unary.
=>   Return data type.
=>   Which Object pass into the function.
=>   Under parentheses perform the operation.

Advantages of overloading the operator


It makes the user-defined data type easy to use with the operator. e.g string catenation with help of + operator in the same we can do with the user-defined data type. It makes the interface intuitive for your class. Operator overloading allows operators in c++ user-defined meaning on the user-defined data type.


we can add all the features of the primitive data type in the user-defined data type. operator overloading is the powerful feature of the user-defined data type in the c++.

Please write the comment, if you find anything incorrect and any problem in the above topic or you want to share more information about the above topic.

Comments

Popular posts from this blog

how to cout in cpp

Flutter layout

Diferrence between flutter and react native