AppDividend
Latest Code Tutorials

Function Overloading in C++ Tutorial With Example

0

Function Overloading in C++ Tutorial With Example. Before understanding function overloading, we first need to understand what is a function. A function is a segment of codes which is used to perform specific tasks single or multiple times. Function eases the work of writing similar code multiple times. Function refers to the segment that groups code to perform a particular task. In C++ programming, two functions can have the same name if the number and type of arguments passed are different.

Function Overloading in C++

Function overloading in C++ is when two or more function has similar names but have different parameters. Parameters can be different at times, and it can be the different return type of the function, the number of arguments in the function. We cannot only overload the function only the basis of return type at declaration. Merely changing the return type won’t overload the function.

In function overloading, we can apply different function definitions for the same function names in a particular scope.

Function overloading strictly follows polymorphism feature.

When function overloading is there, then the compiler determines the most appropriate definition to use, by comparing an argument types you have used to call the function with parameter types specified in the definitions. The process of selecting the most overloaded function or operator is called an overload resolution.

#Advantages

  1. The primary use of function overloading is to save memory.
  2. It increases the readability of the program.
  3. It makes the execution of the program faster and decreases execution time.

#Disadvantages

  1. The functions which only have different return type cannot be overloaded.
  2. The function which is declared static member function cannot be overloaded.

#For example

void  function1 (int a);
void  function1 (float b);
int  function1 (int a, float b);

Here, we can see that all the three functions are having the same name, which is function1 and are overloaded because they have different parameters and return types. In the first line, the function’s parameter is of integer data type, and in the second line it is of float data type, and in the third line, it has two different parameters one of integer data type and the other of float datatype.

Hence the functions are overloaded, and they will be used to perform different tasks. They run on compile-time polymorphism.

#Examples for function overloading

Write a program using function  calculation() for 3 different purposes. 1st for adding two numbers of integer datatype, 2nd for multiplying three float numbers, and the 3rd one should return a division of 2 numbers respectively.

#include <iostream>
using namespace std;
class FunctionOverloading
{
public:
  void calculation(int a, int b, int k) /*addition of two numbers*/
  {
    cout << "The sum is = " << a + b + k << endl;
  }
  void calculation(double c, double d, double e) /*Multiplication of two numbers*/
  {
    cout << "Multiplication is = " << c * d * e << endl;
  }
  int calculation(int f, int g)
  {
    return (f / g);
  }
};
int main()
{
  FunctionOverloading obj1;
  obj1.calculation(5, 6, 7);
  obj1.calculation(2.34, 4.54, 6.72);
  cout << "The division is=" << obj1.calculation(10, 5) << endl;
}

See the following output.

 

Function Overloading in C++

Example 2: Write a program to show the data of a student and his teacher of the school using a single member function with the same name.

#include <iostream>
using namespace std;
class functionoverloading
{
public:
  void data(int roll_no, int class_no, char student_name[6])
  {
    cout << "Name of the student= " << student_name << endl;
    cout << "Roll no. of the student= " << roll_no << endl;
    cout << "Class number of the student= " << class_no << endl;
    cout << "\n";
  }
  void data(int teacher_id, int teacher_teaching_class, char teacher_name[10], int salary)
  {

    cout << "Name of the teacher= " << teacher_name << endl;
    cout << "Teacher id = " << teacher_id << endl;
    cout << "Teacher teaching class" << teacher_teaching_class << endl;
    cout << "Salary of the teacher = " << salary << endl;
  }
};
int main()
{
  functionoverloading obj1;
  obj1.data(5, 10, "Rohit");
  obj1.data(11701275, 10, "Sachin Mehta", 50000);
  return (0);
}

See the following output.

 

Advantages of function overloading

Finally, Function Overloading in C++ Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.