Don’t Overload Your Brain Just Chill


In this blog part we are going to go through understanding what is overloading. We will study the concepts of overloading because in the following blogs to come which describe the working of the code, we have particularly used it.
So just Sit Back and Relax and keep scrolling!
Basically, Overloading is divided into two parts:


 Function Overloading


Function overloading or method overloading is a concept of using functions of the same name again and again in. The main requirement of function overloading is that you need to pass different number of arguments in the function itself. Or else you can pass the same arguments in function but they need to be of a different data type. Also, the return type of the function can be different. Generally, function overloading is often confused with different forms of polymorphism where the choice is made at runtime. Now let’s move on to an example to get a more hand on experience on what actually function overloading means. Check out the following code:
#include <iostream>
using namespace std;

// Volume of a cube.
int Volume(int s)
{
  return s * s * s;
}

// Volume of a cylinder.
float Volume(float r, int h) {
  return 3.14 * r * r * h;
}

// Volume of a cuboid.
long Volume(long l, int b, int h) {
  return l * b * h;
}

int main()
{
  cout << Volume(6);
  cout << Volume(3.4, 4);
  cout << Volume(100l, 75, 15);
}

So basically, the prior code has 3 functions with the same name called Volume. The first function is basically finding out the volume of a cube and is of return type int and has only 1 argument passed in it that is the side of the cube. The second function is used to calculate the volume of a cylinder and has a return type of float and has 2 arguments passed in it and they are the radius and the height. The third function is used to calculate the volume of a cuboid which has a return type long and has 3 arguments passed in it that are length, breadth and height. So, during the code execution when the function Volume is called first and as we are only passing 1 argument, the first function will be called and the volume of the cube will be returned. In the next cout statement when Volume is called as we are passing 2 arguments the second function will be called where we will get directed to the Volume of the cuboid. And likewise is the execution when you call Volume in the third cout statement and pass 3 arguments.
So, this was all about how function overloading takes place. But sometimes students get confused between function overloading and function overriding. So, let us discuss about function overloading in brief and then move on to operator overloading.

·         Function overriding

Function overriding is a concept in which the same function is present in base class as well as derived class. To override a function, you need to mention the function with the same name in derived class as well. So, basically the execution of overriding takes place like in the image mentioned below:
So, clearly when we override the getData() function, the function in the derived class is called. But if you want the function in the base class to be called you need to use the scope resolutor “::”. So, this was a brief display of what overriding is all about, now let us move to the concept of Operator overloading.
This image will help u differentiate between overloading and overriding.


Operator Overloading

In C++, we can make operators to work for user defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.
For example, we can overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +.
Other example classes where arithmetic operators may be overloaded are Complex Number, Fractional Number, Big Integer, etc.Operator overloading is an important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type. 

A simple and complete example:
#include<iostream>
using namespace std;

class Complex {
private:
        int real, imag;
public:
        Complex(int r = 0, int i =0)
        {
               real = r;
               imag = i;
        }
       
        // This is automatically called when '+' is used with
        // between two Complex objects
        Complex operator + (Complex const &obj)
        {
               Complex res;
               res.real = real + obj.real;
               res.imag = imag + obj.imag;
               return res;
        }
        void print() { cout << real << " + i" << imag << endl; }
};

int main()
{
        Complex c1(3,5), c2(4, 6);
        Complex c3 = c1 + c2; // An example call to "operator+"
        c3.print();
}


The output of the code is 7 + i11
In this code we are overloading the operator ‘+’.
‘+’ is a binary operator. Similarly you can also overload unary operators like ‘++’ or ‘--’

Can we overload all operators?
Almost all operators can be overloaded except few. Following is the list of operators that cannot be overloaded.
. (dot) 
   :: 
   ?: 
   sizeof 

Restrictions on Operator Overloading in C++

Following are some restrictions to be kept in mind while implementing operator overloading.
  • Precedence and Associativity of an operator cannot be changed.
  • Arity (numbers of Operands) cannot be changed. Unary operator remains unary, binary remains binary etc.
  • No new operators can be created, only existing operators can be overloaded.
  • Cannot redefine the meaning of a procedure. You cannot change how integers are added.

Sagar Potnis
K-63

 References of images

Comments

Post a Comment

Popular posts from this blog

Do you have a ticket?

It's moderator's Office

Dynamic Memory Allocation in C++