0% found this document useful (0 votes)
8 views

Function Overloading

Uploaded by

clpatil2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Function Overloading

Uploaded by

clpatil2007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Ch.

8 Function Overloading and Member functions


One mark questions:

1. What is meant by function overloading?


A. Function overloading means two or more functions have same name, but differ in the
number of arguments or data type of arguments.
2. When is function overloading needed?
A. When different functions are created for different operations, then user has to call
respective function depending on the situation. Instead, for different situations if the same
function is called with different arguments using function overloading, then the compiler
automatically decides about the appropriate function by comparing the argument types
used in the call to the function and calls the required function. Thus the code is executed
faster.
3. Write an advantage of function overloading.
A. By using function overloading, it is easier to understand the flow of information and
debug.
4. Name one condition for overloading of functions.
A. Each function in a set of overloaded functions must have different argument list.
5. What is an inline function?
A. An inline function is a special type of function which replaces a function call with the body
of the function.
6. Write one advantage of inline function.
A. An advantage of inline function is that the speed of execution of a program increases.
7. The inline function is always converted to a code block. True/False.
A. False.
8. What is a friend function?
A. Friend function is a non member function of a class that has access to both private and
protected access members of the class.
9. Write an example for friend function.
A. class myclass
{
private:
int a,b;
public:
void set_val(int i, int j);
friend int add(myclass obj);
};
void myclass::set_val(int i, int j)
{
a=i;
b=j;
}
int add(myclass obj)
{
return (obj.a+obj.b);
}
void main( )
{
myclass object;
object.set_value(34,56);
cout<<”Sum of 34 and 56 is:”<<add(object);
}
10. Write one condition for using a friend function.
A. When two classes must share a common function, C++ allows the common function to be
shared between the two classes by making the common function as a friend to both the
classes. This allows the function to have access to the private data of both of these classes.
11. Can the keyword friend be used while declaring a friend function?
A. Yes, the keyword friend should be used while declaring a friend function
12. Overloading a function means using different names to perform the same operations.
True/False.
A. False
Two marks questions:

1. What is meant by overloading implements polymorphism?


A. Polymorphism means one name having many forms, different behaviour of an instance of
an object depending on situations. C++ implements polymorphism through function
overloading and operator overloading.

2. Write example for definition and declaration of function overloading.


A. In the example below, the function product() is overloaded two times. The compiler
chooses the right type of function depending on the number and type of arguments.
Example:
int product(int p, int q, int r); // function declaration
float product(float x, float y, float z); // function declaration
int product(int p, int q, int r) //function definition
{
cout<<”product=”<<p*q*r;
}
float product(float x, float y, float z) //function definition
{
cout<<”product=”<<x*y*z;
}

3. What are the restrictions on overloaded functions?


A. The restrictions on overloaded functions are:
 Each function in a set of overloaded functions must have different argument list.
 If typedef is used for naming functions, then the function is not considered as
different type.
4. What are the inline functions? Give an example.
A. An inline function is a special type of function whose body is inserted at the place where
it is called instead of transferring the control to the function. The inline function is defined
using the keyword inline.
Example:
#include<iostream.h>
inline int square(int a) //inline function
{
return (a*a);
}
void main()
{
int x,y;
x=square(5);
cout<<”Square of 5=”<<x;
y=square(10);
cout<<”Square of 10=”<<y;
}
5. Write the advantages of inline functions.
A. The advantages of inline functions are:
 The inline member functions are compact function calls. Therefore the size of the
object code is considerably reduced.
 The speed of execution of a program increases.
 Very efficient code can be generated.
 The readability of the program increases.
6. Create an inline function to check if a number is prime or not.
A.
#include<iostream.h>
inline int PRIME(int x)
{
int i, flag=1;
for(i=2;i<x;i++)
{
if(x % i==0)
{
flag=0;
break;
}
}
return(flag);
}
void main( )
{
int p, flag;
cout<<”Enter a number:”;
cin>>p;
flag=PRIME(p);
if(flag)
cout<<”Prime number”;
else
cout<<”Not a prime number”;

7. When are friend functions used? Write syntax for declaration of friend function.
A. Friend functions are used in the following situation: When two classes must share a
common function, C++ allows the common function to be shared between the two classes
by making the common function as a friend to both the classes. This allows the function to
have access to the private data of both of these classes.
The friend function is declared within a class with the prefix friend.
Syntax:
class class_name
{
public:
friend void function1(void);
}

8. Write any two characteristics of friend function.


A. The characteristics of friend function are:
 A friend function although not a member function, has full access right to the private
and protected members of the class.
 A friend function cannot be called using the object of that class. It can be invoked
like any normal function.

Three mark questions:

1. Write any three reasons for function overloading.


A. The reasons for function overloading are:
1. When different functions are created for different operations, then user has to call
respective function depending on the situation. Instead, for different situations if the
same function is called with different arguments using function overloading, then
the compiler automatically decides about the appropriate function by comparing the
argument types used in the call to the function and calls the required function. Thus
the code is executed faster.
2. It is easier to understand the flow of information.
3. Code maintenance is easy.
2. What are the advantages of inline functions?
A. The advantages of inline functions are:
 The inline member functions are compact function calls. Therefore the size of the
object code is considerably reduced.
 The speed of execution of a program increases.
 Very efficient code can be generated.
 The readability of the program increases.
3. Write the advantages and disadvantages of inline functions.
A. The advantages of inline functions are:
 The inline member functions are compact function calls. Therefore the size of the
object code is considerably reduced.
 The speed of execution of a program increases.
 Very efficient code can be generated.
 The readability of the program increases.
The disadvantages of inline functions are:

 As the body of the inline function is substituted in place of a function call, the size of
the executable file increases and more memory is needed.

4. When is it possible that an inline function may not work?


A. The inline function may not work for the reasons:
 The inline function definition is too long or too complicated.
 The inline function is recursive.
 The inline function has looping constructs.
 The inline function has a switch or goto.

5. Write any three characteristics of friend function.


A. The characteristics of friend function are:
 A friend function although not a member function, has full access right to the private
and protected members of the class.
 A friend function cannot be called using the object of that class. It can be invoked
like any normal function.
 A friend function is declared by the class that is granting access. The friend
declaration can be placed anywhere in the class declaration. It is not affected by the
access control keywords (public, private and protected).

Five mark questions:


1. Explain the need for function overloading.
A. Function overloading means two or more functions have same name, but differ in the
number of arguments or data type of arguments. The need for function overloading is as
follows:
1. When different functions are created for different operations, then user has to call
respective function depending on the situation. Instead, for different situations if the
same function is called with different arguments using function overloading, then
the compiler automatically decides about the appropriate function by comparing the
argument types used in the call to the function and calls the required function. Thus
the code is executed faster.
2. It is easier to understand the flow of information.
3. Code maintenance is easy.
4. Easier interface between programs and real world objects.

2. Discuss overloaded functions with syntax and example.


A. Function overloading means two or more functions have same name, but differ in the
number of arguments or data type of arguments.
Example:
#include<iostream.h>
class funoverload
{
public:
int volume(int a) //volume of cube
{
return (a*a*a);
}
double volume(double r, double h) //volume of cone
{
return (0.33*3.14*r*r*h);
}
double volume(double r, int h) //volume of cylinder
{
return(3.14*r*r*h);
}
Double volume(double l, double b, double h) //volume of cuboid
{
return(l*b*h);
}
};
int main( )
{
funoverload f1;
cout<<”Volume of the Cube:”<<f1.volume(10);
cout<<”Volume of the Cone:”<<f1.volume(2.0,3.0);
cout<<”Volume of the Cylinder:”<<f1.volume(2.0,3);
cout<<”Volume of the Cuboid:”<<f1.volume(5.0,6.0,7.0);
return 0;
}

3. Explain inline functions with syntax and example.


A. An inline function is a special type of function whose body is inserted at the place where
it is called instead of transferring the control to the function. The inline function is defined
using the keyword inline.
Syntax: inline returntype functionname(arguments)
{
//body of function
}
Example:
#include<iostream.h>
inline int square(int a) //inline function
{
return (a*a);
}
void main()
{
int x,y;
x=square(5);
cout<<”Square of 5=”<<x;
y=square(10);
cout<<”Square of 10=”<<y;
}

4. Explain friend functions and their characteristics.


A. Friend function is a non member function of a class that has access to both private and
protected access members of the class. The friend function is declared within a class with
the prefix friend.
Syntax:
class class_name
{
public:
friend void function1(void);
}

The characteristics of a friend function are:


 A friend function although not a member function, has full access right to the private
and protected members of the class.
 A friend function cannot be called using the object of that class. It can be invoked
like any normal function.
 A friend function is declared by the class that is granting access. The friend
declaration can be placed anywhere in the class declaration. It is not affected by the
access control keywords (public, private and protected).
 They are normal external functions that are given special access privileges.
 It cannot access the member variables directly and has to use an
objectname.membername.
 The function is declared with keyword friend. But while defining friend function it
does not use either keyword friend or :: operator.

You might also like