Function Overloading Q&A
Function Overloading Q&A
Function Overloading Q&A
Question 1.
What is meant by function overloading?
Answer:
Function ovs erloading means two or more functions having same name but
different types of arguments or different number of arguments.
Question 2.
When is function overloading needed?
Answer:
Function overloading is normally used when several function of the same name
perform identical tasks on different data types.
Question 3.
Write an advantage of function overloading.
Answer:
The overloading function helps to reduce the use of different names for many
functions.
Question 4.
Name one condition for overloading of functions.
Answer:
The parameters/arguments should differ in the number of arguments or if they have
same number of argument then they should differ in the type of the arguments.
Question 5.
What is an inline function?
Answer:
Inline functions are those functions whose body is inserted in place of the function
call.
Question 6.
Write one advantage of inline function.
Answer:
The speed of execution of a program increases
Question 7.
The inline function is always converted to a code block. True/False.
Answer:
True
Question 8.
What is a friend function?
Answer:
A friend function is a non-member function and is a friend of a class. It is declared
inside a class with the prefix friend and defined outside the class line any other
normal func¬tion without the prefix friend.
Question 9.
Write an example for friend function declaration.
Answer:
Question 10.
Write one condition for using a friend function.
Answer:
A friend function is useful when a function to be shared between the two classes by
making a function as a friend to both the classes, thereby allowing a function to
access private and protected data members of both classes.
Question 11.
Can the key word friend be used while declaring a friend function?
Answer:
Yes, the keyword friend is used while declaring friend function. For example, friend
int Avg marks(PUC, cet);
Question 12.
Overloading a function means using different names to perform same operations.
True/ False.
Answer:
False
Question 1.
What is meant by overloading implements polymorphism?
Answer:
The overloading helps to apply polymorphism because Function overloading means
two or more functions having same name but different types of arguments or
different number of arguments. Whereas polymorphism refers to one name having
many forms of an object behaviour depending on situations.
Question 2.
Write example for definition and declaration of function overloading
Answer:
Question 3.
What are the restrictions on overloaded functions?
Answer:
• The parameters/arguments should differ in the number of arguments or if
they have same number of argument then they should differ in the type of the
arguments
• The use of typedef keyword for naming functions is not treated as different
type but is only a synonym for another type.
Question 4.
What are inline functions? Give an example.
Answer:
Inline functions are those functions whose body is inserted in place of the function
call.
For example:
Question 5.
Write the advantages of inline functions.
Answer:
Question 7.
When are friend functions used? Write syntax for declaration of friend function.
Answer:
A friend function is useful when a function to be shared between the two classes by
making a function as a friend to both the classes, thereby allowing a function to
access private and protected data members of both classes.
syntax for declaration of friend function:
class classname {
public:
friend returntype function name (arguments);
}
Question 8.
Write any two characteristics of friend function.
Answer:
The characteristics of friend function are
Question 1.
Write any three reasons for function overloading.
Answer:
a. The overloading function helps to reduce the use of different names for many
functions.
b. The developer of the program can use one function name to give function call
to one in many functions. This reduce the selecting a function block to give a
function call by the user.
c. It is easier to understand the flow of information that helps in faster debug.
d. Easy program maintenance.
Question 2.
What are the advantages of inline functions?
Answer:
The advantages of inline functions are
Question 3.
Write the advantages and disadvantages of inline functions.
Answer:
Advantages of inline functions
Question 4.
When is it possible that an inline function may not work?
Answer:
The inline function may not work under following situations
Question 5.
Write any three characteristics of friend function.
Answer:
The characteristics of friend function are
Question 1.
Explain the need for function overloading.
Answer:
The overloading helps to apply polymorphism because Function overloading means
two or more functions having same name but different types of arguments or
different number of arguments. Whereas polymorphism refers to “one name having
many forms of an object behavior depending on situations.
Function overloading is normally used when several function of the same name
perform identical tasks on different data types. The overloading function helps to
reduce the use of different names for many functions.
The developer of the program can use one function name to give function call to one
in many functions and C++ select the appropriate function by checking the number
of parameter and type of parameters. This reduces the selecting a function block to
give a function call by the user.
It is easier to understand the flow of information that helps in faster debug. Easy
program maintenance. There can be common interface between programs and real-
world objects.
Question 2.
Discuss overloaded function with syntax and example.
Answer:
The definition, declaration and calling of overloaded function is explained with a
program example is given below.
C++ program to add two or three numbers
Question 3.
Explain inline functions with syntax and example.
Answer:
Declaration and Definition of inline functions
inline returntype_specifier functioname (arguments)
{
….
}
The definition of inline function should be defined before all functions that call it.
The function definition should have the prefix inline.
Example:
Question 4.
Explain friend functions and their characteristics.
Answer:
A friend function is a non-member function and is a friend of a class. It is declared
inside a class with the prefix friend and defined outside the class like any other
normal function without the prefix friend. This friend function can access private
and protected data members if it is a friend function of that class.
The characteristics of friend function are