Monday, January 25, 2021
Home C / C++ Creating Overloaded operator in C++ with example

Creating Overloaded operator in C++ with example

As we know The variables of primitive data types can perform a number of different operations (functions) using operators like (+, – , /, *)/

For Example: a + b * c or if ( a < b )

However in used defined datatypes we cannot do such operations

For Example: class obj1,obj2;

this is not possible in this case, we need the concept of operator overloading here.
Now for the syntax we first have to create a function for the class, for example:

operator+ for the addition operator ‘+’
operator> for the comparison operator ‘>’

In operator overloading the Operators are mostly like functions, they can take arguments and can return values, the only thing that separates them from ordinary functions is their specified name and syntax at the time of definition and we cannot create our own operator, otherwise we can overload it.

Lets have a look at the below example:

  1. // Operator overloading in C++
  2. //assignment operator overloading
  3. #include<iostream>
  4. using namespace std;
  6. class Employee
  7. {
  8. private:
  9.     int idNum;
  10.     double salary;
  11. public:
  12.     Employee ( ) {
  13.         idNum = 0, salary = 0.0;
  14.     }
  16.     void setValues (int a, int b);
  17.     void operator= (Employee >emp );
  19. };
  21. void Employee::setValues ( int idN , int sal )
  22. {
  24.     salary = sal; idNum = idN;
  26. }
  28. void Employee::operator = (Employee >emp)  // Assignment operator overloading function
  29. {
  30.     salary = emp.salary;
  31. }
  33. int main ( )
  34. {
  36.     Employee emp1;
  37.     emp1.setValues(10,33);
  38.     Employee emp2;
  39.     emp2 = emp1; // emp2 is calling object using assignment operator
  41. }

Now lets explain this code for overloading “=” operator, we assigned the values to “idN” and “sal” by using the function “setValues” which is a public member function of the class “Employee”, Now our main point is the overloaded function which is defined as “operator =”. Inside this function we assigned the value of salary into the other variable that of the same class, Such that we could use the assignment operator directly in our main() function. Now as you can see in main(), we have two variables emp1 and emp2 of type Employee and we can use the assignment operator directly in our last line of code, It is all because of operator overloading of operator “ = ”.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....