Programming Questions on Function Overloading in C++
What is Function Overloading?
In C++, a function with the same name but distinct parameters is referred to as function overloading. The C++ function overloading feature makes the code easier to read and saves the programmer from having to commit different function names to memory. A class is overloaded if it contains several functions with distinct parameters with the same name. We must overload the function if we must carry out a single operation with several numbers or kinds of parameters.
Function overloading occurs when several functions share the same name but have distinct parameters.
The program is easier to read if there is only one action to be done and all the functions have the same name.
Assuming you need to add the provided numbers. Still, you can pass in any number of arguments; you might need help comprehending the behavior of the function if you write it as a(int, int) for two parameters and b(int, int,int) for three. This is because the names of the functions differ.
Programming Questions:
1. What is the output of the following code?
#include<iostream>
using namespace std;
int function(int a = 5, int b = 10, int c)
{
return (a+b+c);
}
int main()
{
cout << function(20);
return 0;
}
Options:
1.10
2.20
3.35
4.40
Answer: 35
2. What is the output of the following code?
#include
using namespace std;
void square (int *a)
{
*a = (*a)++ * (*a);
}
void square (int *a, int *b)
{
*a = (*a) * --(*b);
}
int main ( )
{
int num = 30;
square(&num, &num);
cout << num;
return 0;
}
Options:
1.700
2.360
3.910
4.870
Answer: 870
3. Which of the following is supported by C++'s function overloading and default parameters capabilities in object-oriented programming?
Options:
1. Inheritance
2. Polymorphism
3. Encapsulation
4. None of the above
Answer: Polymorphism
4. Which of the following overloading functions in C++ is not permitted?
1.Declarations of functions with the same return type
int fun(int a, int y\b);
void fun(int a, int b);
2. Functions using only the static keyword in the return type as a difference
int fun(int x, int y);
static int fun(int x, int y);
3. Parameter declarations that differ only in a pointer * versus an array []
int fun(int *ptr, int n);
int fun(int ptr[], int n);
4. Two parameter declarations with the same default arguments as the only difference
int fun( int x, int y);
int fun( int x, int y = 10);
Options:
1. All except 1
2. All except 2 and 4
3. All
4. All except 2
Answer: All 4
5. What is the output of the following code?
#include <iostream>
using namespace std;
int fun(int=0, int = 0);
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) { return (x+y);
}
Options:
1. 0
2.10
3.5
4. compilation error
Answer: 5
An expression for a function that accepts two parameters with default values of 0 and 0 is "int fun(int=0, int=0)". Fun()'s definition is given in the final sentence. Upon calling fun(5), x receives 5, and y receives 0. Thus, 5 is the value that was returned.
6. What is the output of the following code?
include<iostream>
using namespace std;
class Test
{
Protected:
int x;
Public:
Test (int i):x(i) { }
void fun() const { cout << \"fun() const \" << endl; }
void fun() { cout << \"fun() \" << endl; }
};
int main()
{
Test t1 (10);
const Test t2 (20);
t1.fun();
t2.fun();
return 0;
}
Options:
1. fun()
fun() const
2. Cmpilation error
3. fun()
4. fun()
fun()
Answer: fun()
fun() const
Except for one being const and the other not, the signatures of the two methods, "void fun() const" and "void fun()," are the same. Furthermore, if we examine the output more closely, we can see that "void fun()" is called on non-const objects, and "const void fun()" is called on const objects. Member methods in C++ may be overloaded based on the const type. It can be helpful to overload a function based on its const type when it returns a reference or pointer. One function can be made const, returning a const reference or const pointer, and another non-const, returning a non-const reference or pointer.