Posted in C++

Overloading operator’s (in C++)

Here I will talk about overloading. Let’s start by formally defining what overloading is :

Specifying more than one definition’s for a function is called overloading.

Why do we need it ?

Now let’s say I have made a sort function. This function is sufficient for sorting an integer array, but what about an array with decimal number’s or a string. Again for sorting these data types we need more functions consisting of the same algorithm. Suppose we wanted to sort 4 different types of data types, we will have to make four different functions, and every-time I call the function I will need to decide what function to call. Wouldn’t it be nice if the compiler itself decided which function to call or if there were a self adjusting mechanism within the function to adjust according to input. This is why we need overloading. There is another mechanism to achieve the same and that is called a template, but overloading makes it easier to read the code at many occasions.

How it works ?

If you’ve been using standard c++ input methods then the familiar command cin and cout use operator >> and << respectively for functioning. Now these operators are actually overloaded. << and >> are the right shift and left shift operator’s originally, but the iostream file that we include in our programs has overloaded these operators to the extraction and insertion operators and thus now they function like that when used with cin and cout. These operators function as normal when not used with cin and cout.

This is how overloading works. Now to get to the implementation part, we will take up an example to get this better.

Let us make our own class for representing a fraction. A fraction has two parts, a numerator and a denominator, so our class will also have these two information. Here is the class prototypes :

class fraction
{
private:
    int num,den;
public:
    fraction(int f=0,int i=0){num=f; den=i;} //constructor
    fraction(const fraction &a); //copy constructor
    void reduce(); //function for reducing fraction to lowest terms
//************* Overloaded operator definition *****************//
    fraction operator+(const fraction &f);
    fraction operator-(const fraction &f);
    fraction operator*(const fraction &f);
    fraction operator/(const fraction &f);
    friend bool operator >>(istream& inp,fraction &f);
    friend ostream& operator <<(ostream &out,fraction f);
};

In the above code I have overloaded the basic operators : +,-,*,/,<< and >>.

With the appropriate definitions for each function above we can achieve a very simple and easily readable code :

int main()
{
    fraction f1,f2,res;
    cout<<"Enter your fractions :\n";
    if((cin>>f1)&&(cin>>f2))
    {
        res=f1+f2;
        cout<<res;
    }
    return 0;
}

We could also have achieved the same effect by using functions as follows:

int main()
{
    fraction f1,f2,res;
    cout<<"Enter your fractions :\n";
    if((input(f1))&&((input(f2))
    {
        res=sum(f1,f2);
        display(res);
    }
    return 0;
}

where display, input and sum are some defined functions.

One may prefer using functions over overloading but we should understand that both have their own importance.

A simple example would be to sum three or more fractions together. Making a function for the job is possible but overloading is a more obvious and user friendly solution.

We can overload several operators but there are a few that we can’t or shouldn’t overload.

Full code for the fraction class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s