Q:

C++ Function Overloading | Find output programs | Set 1

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Function Overloading (set 1).

Program 1:

#include <iostream>
using namespace std;

void fun()
{
    cout << "1. fun() called" << endl;
}

void fun(int A, float B)
{
    cout << "2. fun() called " << A << " " << B << endl;
}

void fun(int A, double B)
{
    cout << "3. fun() called " << A << " " << B << endl;
}

int main()
{
    fun();
    fun(10, 3.14);

    return 0;
}

Program 2:

#include <iostream>
using namespace std;

void fun()
{
    cout << "1. fun() called" << endl;
}

void fun(int A, float B)
{
    cout << "2. fun() called " << A << " " << B << endl;
}

int fun(int A, float B)
{
    cout << "3. fun() called " << A << " " << B << endl;
}

int main()
{
    fun();
    fun(10, 3.14);

    return 0;
}

Program 3:

#include <iostream>
using namespace std;

void fun()
{
    cout << "1. fun() called" << endl;
}

void fun(int A, float B)
{
    cout << "2. fun() called " << A << " " << B << endl;
}

int fun(int A, double B)
{
    cout << "3. fun() called " << A << " " << B << endl;
}

int main()
{
    fun();
    fun(10, 3.14);

    return 0;
}

Program 4:

#include <iostream>
using namespace std;

void print()
{
    cout << "****" << endl;
}

void print(int NUM, char ch)
{
    for (int i = 0; i < NUM; i++)
        cout << ch;
    cout << endl;
}

void print(char ch, int NUM)
{
    for (int i = 0; i < NUM; i++)
        cout << ch;
    cout << endl;
}

int main()
{
    print();
    print(4, '#');
    print('@', 4);

    return 0;
}

All Answers

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

Answer Program 1:

Output:

1. fun() called
3. fun() called 10 3.14

Explanation:

In the main() function, we called fun() without any argument then it will print "1. fun() called" on the console screen. 

And then we call fun(10,3.14), then it will call function void fun(int A,double B). Because "3.14" is a double value. That's why it will call the function with the argument double. If we want to call the function with argument float then we need to pass "3.14F" instead of "3.14".

Answer Program 2:

Output:

main.cpp: In function ‘int fun(int, float)’:
main.cpp:14:5: error: ambiguating new declaration of ‘int fun(int, float)’
 int fun(int A, float B)
     ^~~
main.cpp:9:6: note: old declaration ‘void fun(int, float)’
 void fun(int A, float B)
      ^~~

Explanation:

This program will generate an error because a function cannot be overloaded on the basis of the return type.

void fun(int A,float B);
int fun(int A,float B);

In the above declarations, the only return type is different. That's why it will generate an error

Answer Program 3:

Output:

1. fun() called
3. fun() called 10 3.14

Explanation:

Here, we overloaded function fun(),

void fun()
void fun(int A,float B)
int fun(int A,double B)

Note: If arguments are different in the functions, then the return type of the function may be different for function overloading.

Now come to the main() function, we are calling fun() without any argument then it will print "1. fun() called" on the console screen.

And then, we are calling fun(10,3.14), then it will call function "void fun(int A,double B)". Because "3.14" is a double value. That's why it will call the function with the argument double. If we want to call the function with argument float then we need to pass "3.14F" instead of "3.14".

Answer Program 4:

Output:

****
####
@@@@

Explanation:

Here, we overloaded print() function, declarations for overload print() functions are given below,

void print()
void print(int NUM,char ch)
void print(char ch,int NUM)

Here, each overloaded function called one by one in the main() function and print below texts,

****
####
@@@@

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

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Function Overloading | Find output programs | ... >>
<< C++ Friend Function | Find output programs | Set 2...