×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

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

This section contains the C++ find output programs with their explanations on C++ Function Overloading (set 1).
Submitted by Nidhi, on June 25, 2020

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;
}

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".

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;
}

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.

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;
}

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".

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;
}

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,

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




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.