Q:

Explain some ways of doing function overloading in C++?

0

Explain some ways of doing function overloading in C++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

Function overloading can be done by changing:

1.The number of parameters in two functions.

#include <iostream>
using namespace std;
void Display(int i, char const *c)
{
    cout << " Here is int " << i << endl;
    cout << " Here is char* " << c << endl;
}
void Display(double f)
{
    cout << " Here is float " << f << endl;
}
int main()
{
    Display(5,"Five");
    
    Display(5.5);
    
    return 0;
}

Output:

Here is int 5
Here is char* Five
Here is float 5.5

2. The data types of the parameters of functions.

#include <iostream>
using namespace std;
void Display(int i)
{
    cout << " Here is int " << i << endl;
}
void Display(double f)
{
    cout << " Here is float " << f << endl;
}
void Display(char const *c)
{
    cout << " Here is char* " << c << endl;
}
int main()
{
    Display(5);
    
    Display(5.5);
    
    Display("Five");
    
    return 0;
}

Output:

Here is int 5
Here is float 5.5
Here is char* Five

3. The order of the parameters of functions.

#include <iostream>
using namespace std;
void Display(int i, char const *c)
{
    cout << " Here is int " << i << endl;
    cout << " Here is char* " << c << endl;
}
void Display(char const *c,int i)
{
    cout << " Here is int " << i << endl;
    cout << " Here is char* " << c << endl;
}
int main()
{
    Display(5,"Five");
    
    Display("Five",5);
    
    return 0;
}

Output:

Here is int 5
Here is char* Five
Here is int 5
Here is char* Five

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is operator overloading?... >>
<< What is function overloading in C++?...