Operator Overloading In C++

Operator Overloading :

For operator overloading it is important to have the knowledge of function overloading.
First of all the question that arises is what is overloading? Let's consider an example:
Consider a person doing his job then his boss came and assigned him the same job with a little difference. Means the job the person is doing is same but it also has some changing in it. Let's consider a program:
      
         void printNumber(int a) {  

  cout << a;
}

This is effective with integer arguments only. Overloading it will make it available for other types, such as char and float.

         void printNumber(char a) {
  cout << a;
}

Now the same printNumber function works for both int and char.

You can consider the function as the job and the data types int and char as difference in the same job.(Both functions are doing the same job ( printing the values ) but one is printing int value and the other is printing char value).

When overloading function, the definition of the function must differ from each other by the types and/or the number of arguments in the argument list.

#include <iostream>

using namespace std;

 void printNumber(int x) 

{ cout << "integer: " << x << endl; } 

void printNumber(float x) 

{ cout << "float: " << x << endl; } 

int main() {

 int a = 18; 

 float b = 34.267; 

 printNumber(a);

 printNumber(b); }


This program outputs as: 
Output:
 integer: 18 
  float: 34.267


These functions are based on arguments provided. int argument function will be called when the calling function has int argument as parameter.
Functions cannot be overloaded that forget only by return type. The following program will give an error:

int printName(int a) { }
 float printName(int b) { }
 double printName(int c) { }

But it is possible to use different return types as long as you have different arguments as well.

int printName(int a) { } 
 float printName(float b) { }
 double printName(double c) { }

Now let's move our attention towards operator overloading.

  Operator overloading is just like function overloading. Like an overloaded function that do multiple tasks an overloaded operator also do multiple tasks.

But why do we ever need to overload operators when we can use them in simple way?
So consider a program:


So what this program is doing. It is generating an error. And the error is:


There is no match for binary + operator as c1,c2,c3 are of type Demo which is a user defined data type. 
Compiler knows that if both the operands of the operator + are of primitive type (int,float,char etc) then how to work. But if they are of non-primitive ( user-defined ) in the present case of type Demo then compiler doesn't know how to work, so it generates error.
So for this case we need to overload the binary + operator. As the binary + operator will be used for more than one purpose so it is said to be overloaded.
Now it's time to overload our binary + operator.


Always remember that the calling variable can access the member variable directly.
The above expression at line 24 could also be written as "c3=c1.operator+(c2)" Here it is clearly shown that c1 called operator + and passed c2 as an argument and their result is stored in c3.
The above program results in the output :


Only those operators could be overloaded which were valid operators in c. And also it must be proceeded by operator keyword.

Operators that can't be overloaded include :: | .* | . | ?: and sizeof operator.

Operator Overloading of unary operator:

#include<iostream>
using namespace std;
class Demo{
private:
 int a,b;
public:
void setData(int x,int y){
a=x;b=y;
}
void showData(){
cout<<"a:"<<a<<"b:"<<b<<endl;
}
Demo operator-(){
Demo temp;
temp.a=-a;
temp.b=-b;
return temp;
}
};
int main(){
Demo c1,c2;
c1.setData(3,4);
c2=c1.operator-();   // can also be written as: "c2=-c1" means c1 called - operator and passed no parameter. some people say that using - first is not correct.
//but if we want negation of some value do we add - at first or last. we write like "x=-3" not "x=3-" so - will come first.
c2.showData();
return 0;
}

This program outputs as:

a:-3b:-4  

Operator overloading of unary operator ( increment operator ++ ) pre and post:

#include <iostream>
using namespace std;
           class Time{
        public:
        int hour,minute,seconds;
        // constructor to assign values
    Time(int h,int m,int s){
        hour=h;
        minute=m;
        seconds=s;
    }
    // To Show Time
    void show(){
        cout<<"Time is:"<<endl<<hour<<":"<<minute<<":"<<seconds<<endl;
    }
    // overloaded unary ++ operator
    int operator++(){
        minute=++minute;
        return minute;
    }
    // overloaded unary -- operator 
    int operator--(){
        minute=--minute;
        return minute;
    }
    };
int main() {
int x,p;
    Time t(12,23,20);
    cout<<"Before:"<<endl;
    t.show();
    x=t.operator++();
    t.minute=x;
    cout<<"Minute decrement:"<<endl;
    t.show();
    p=t.operator--();
    t.minute=p;
    cout<<"Minute again increment:"<<endl;
    t.show();
    
    return 0;
}

Output:


If you have any query related to this topic then you can ask.






    

Comments

Post a Comment

Popular posts from this blog

Virtual function in c++

Multiple, Multilevel, Hybrid and Hierarchical inheritance

Practice problem classes and objects.