WRITE A PROGRAM FOR FUNCTION OVERLOADING IN C++

 Que- what is function overloading?

Ans- Function overloadig is use in most of the object oriented programming language like C++  and Java . It allows us to make functions with same name by giving different parameters. Function overloading can be considered as an example of a polymorphism feature in C++. 

Advantages of function overloading:-

  • The main advantage of function overloading is that it improves code readability and allows code reusability.
  • The use of function overloading is to save memory space, consistency, and readability.
  • It speeds up the execution of the program
  • Code maintenance also becomes easy.
  • Function overloading brings flexibility to code.
The function can perform different operations and hence it eliminates the use of different function names for the same kind of operations.

Disadvantages of function overloading:-

  • Function declarations that differ only in the return type cannot be overloaded.
  • Member function declarations with the same name and the same parameter types cannot be overloaded if any of them is a static member function declaration.
  • The main disadvantage is that it requires the compiler to perform name mangling on the function name to include information about the argument types.

Example of function overloading:-

#include<iostream>
using namespace std;

class a
{
  public:
   void getdata (char name[20])
   {
        cout<<"your name is :"<<name ;
   }
   void getdata (int age)
   {
        cout<<"your age is :"<<age ;
   }
   void getdata (long phone)
   {               
        cout<<"your contact number is :"<<phone;
   }
   void getdata (double roll)
   {             
        cout<<"your roll no. is :"<<roll ;
   }
};
int main()
{
    a ob ;
    ob. getdata ("krishna"); // give data
    return 0;
}

Result:-

Post a Comment

Previous Post Next Post