Wednesday, October 9, 2013

C++: What do you mean by function overloading in C++? Explain with examples.

Function Overloading in C++

A function name having several definitions which are differentiated by the number or types of their arguments is called function overloading.

Example:

//Source code in C++

//C++ source code
#include<iostream.h>
#include<conio.h>

class printing
{
public:
    void print(int i)
      {
      cout<<"Printing integer: " << i << endl;
      }

      void print(double f)
      {
      cout<<"Printing float: " << f << endl;
      }

      void print(char* c)
      {
      cout<<"Printing character: " << c <<endl;
      }
};

void main()
{
   printing p;

   cout<<"*** OUTPUT ***" << endl;
   p.print(5);
   p.print(500.55);
   p.print("Hello! alevelcomputer.blogspot.com");

   getch();
}

OUTPUT

*** OUTPUT ***
Printing integer: 5
Printing float: 500.55
Printing character: Hello! alevelcomputer.blogspot.com

No comments:

Post a Comment