1

Function Overloading and Default Arguments

Question 1
Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++.
A
Inheritance
B
Polymorphism
C
Encapsulation
D
None of the above
Function Overloading and Default Arguments    
Discuss it


Question 1 Explanation: 
Both of the features allow one function name to work for different parameter.
Question 2
Output?
#include<iostream>
using namespace std;

int fun(int x = 0, int y = 0, int z)
{  return (x + y + z); }

int main()
{
   cout << fun(10);
   return 0;
}
A
10
B
0
C
20
D
Compiler Error
Function Overloading and Default Arguments    
Discuss it


Question 2 Explanation: 
All default arguments must be the rightmost arguments. The following program works fine and produces 10 as output.
#include <iostream>
using namespace std;

int fun(int x, int y = 0, int z = 0)
{  return (x + y + z); }

int main()
{
   cout << fun(10);
   return 0;
}
Question 3
Which of the following overloaded functions are NOT allowed in C++? 1) Function declarations that differ only in the return type
    int fun(int x, int y);
         void fun(int x, int y); 
2) Functions that differ only by static keyword in return type
    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 that differ only in their default arguments
int fun( int x, int y); 
int fun( int x, int y = 10); 
A
All of the above
B
All except 2)
C
All except 1)
D
All except 2 and 4
Function Overloading and Default Arguments    
Discuss it


Question 3 Explanation: 
Question 4
Predict the output of following C++ program?
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;
}
A
Compiler Error
B
fun()
fun() const
C
fun() const
fun() const
D
fun()
fun()
Function Overloading and Default Arguments    
Discuss it


Question 4 Explanation: 
The two methods ‘void fun() const’ and ‘void fun()’ have same signature except that one is const and other is not. Also, if we take a closer look at the output, we observe that, ‘const void fun()’ is called on const object and ‘void fun()’ is called on non-const object. C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer. See following for more details. Function overloading and const keyword
Question 5
Output of following program?
#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); }
A
Compiler Error
B
5
C
0
D
10
Function Overloading and Default Arguments    
Discuss it


Question 5 Explanation: 
The statement "int fun(int=0, int=0)" is declaration of a function that takes two arguments with default values as 0 and 0. The last statement is definition of fun(). When we make a call fun(5), x gets the value 5 and y gets 0. So the returned value is 5.
There are 5 questions to complete.
1

Company Wise Coding Practice    Topic Wise Coding Practice