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

Chapter 8 Function Overloading and Member Functions

This document discusses function overloading, inline functions, and friend functions in C++. It begins with one mark questions that define these concepts and provide examples. It then provides two, three, five, and seven mark questions that require explanations and programming examples to further illustrate function overloading, inline functions, and friend functions.

Uploaded by

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

Chapter 8 Function Overloading and Member Functions

This document discusses function overloading, inline functions, and friend functions in C++. It begins with one mark questions that define these concepts and provide examples. It then provides two, three, five, and seven mark questions that require explanations and programming examples to further illustrate function overloading, inline functions, and friend functions.

Uploaded by

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

CHAPTER 8 -FUNCTION OVERLOADING

CHAPTER 8 FUNCTION OVERLOADING AND MEMBER FUNCTIONS

One mark questions:


1. What is function overloading? (U)
ANS: Function Overloading means two or more functions have same name, but differ in the
number of arguments or data types of arguments.
2. What is polymorphism? (U) .
ANS: The ability of an operator and function to take multiple forms is known as Polymorphism.
3. Mention any one advantage of function overloading. (U)
ANS:
o Code is executed faster.
o It is easier to understand the flow of information and debug.
o Code Maintenance is easy.
4. How does the compiler distinguish between overloaded functions? (U)
ANS: The number of arguments or data types of arguments.
5. Define compile-time polymorphism. (U)
6. What is the other name for function overloading? (U)
7. Mention any one limitation of function overloading. (U)
ANS: Each function in a set of overloaded functions must have different argument list.
8. What is inline function? (U)
ANS: 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.
OR
An Inline function is a special type of function in which the function call replaces the body of
function, instead of transferring the control to the function.
9. Write any one advantage of inline functions. (U)
ANS:
The size of the object code is considerably reduced.
The speed of execution of a program increases.
10. What is the disadvantage of an inline function? (U)
ANS:
May increase the size of the executable file

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 1


CHAPTER 8 -FUNCTION OVERLOADING

11. Mention any one limitation of inline function. (U)


ANS:
The inline function definition is too long or too complicated.
The inline function is recursive.
12. What is a friend function? (U)
ANS:
A friend function is a non-member function of a class has the access permission to the private
member of the class.
13. How do you declare a friend function? (U)
ANS:
class className
{
public:
friend return_type function_name ( arguments );
};
14. How do you identify whether the function is inline function? (U)
ANS:
Inline function definition starts with keyword inline.
15. Write an example to declare friend function. (U)
ANS:
class ABC
{
Private: int a;
public:
friend void getdata ();
};
16. How does a friend function access the data member? (U)
ANS: friend function can be invoked like any normal function, without using object.
17. Where do you declare a friend function? (U)
ANS: The friend function is declared within a class with the prefix friend.
Two marks questions:
1. Write any two needs of function overloading. (A)
ANS:

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 2


CHAPTER 8 -FUNCTION OVERLOADING

o Code is executed faster.


o Code Maintenance is easy.
2. What are the limitations of overloading? (U)
ANS:

nsidered as different type.


3. Give two reasons when inline functions may not work? (S)
ANS:

4. Write any two characteristics of friend functions. (U)


ANS:

protected members of the class.


ither in public or private part of a class.

function.

either keyword friend or : : operator.

ame.membername.
violates the rule of encapsulation and data hiding.
5. Write the syntax of a friend function. (U)
ANS:
class className
{
public:
friend returnType functionName ( arguments);
};

6. When does the inline function not process? (A)

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 3


CHAPTER 8 -FUNCTION OVERLOADING

ANS:
The inline function definition is too long or too complicated.
The inline function is recursive.

Three marks questions:


1. Why do we need function overloading? (A)
ANS:
The advantage of function overloading are:
o Code is executed faster.
o It is easier to understand the flow of information and debug.
o Code Maintenance is easy.
o Easier interface between programs and real world objects.

2. Briefly explain inline function. (U)


ANS:
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 keyword inline is used to define inline function.
Rules:
o Inline function definition starts with keyword inline.
o The inline function should be defined before all function that call it.
o The compiler replaces the function call statement with the function code itself (expansion) and
then compiles the entire code.

3. What are the advantages of inline functions? (U)


ANS:

The speed of execution of a program increases.


Very efficient code can be generated.
The readability of the program increases.
4. What is friend function? Write the syntax of a friend function. (U)
ANS:

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 4


CHAPTER 8 -FUNCTION OVERLOADING

-member function of a class has the access permission to the private


member of the class.
class className
{
public:
friend returnType functionName ( [arguments] );
};

5. What is the need of having a friend function? (A)


ANS: „friend function‟ provides full access permission to private and protected members of the
class.

6. Why inline function may not work sometimes? (A)


ANS:
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.

Five marks questions:


1. What is function overloading? Mention its advantages. (U)
ANS:
Function Overloading means multiple functions have same name, but differ in the number of
arguments or data types of arguments.
on overloading are:
o Code is executed faster.
o It is easier to understand the flow of information and debug.
o Code Maintenance is easy.
o Easier interface between programs and real world objects.

2. What is function overloading? Write the need for function overloading. (U)
3. What is function overloading? Explain with a programming example. (U)

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 5


CHAPTER 8 -FUNCTION OVERLOADING

ANS:
Function Overloading means multiple functions have same name, but differ in the number of
arguments or data types of arguments.
4. Explain with a programming example to overload a function with different number of arguments.
(U)
ANS:
class overload
{
public:
int area ( int l, int b) // area of rectangle
{
return (l * b);
}
float area ( float r) // area of circle
{
return (3.14 * r * r);
}
float area (float b, float h) // area of triangle
{
return (0.5 * b * a);
}
};
void main( )
{
overload f;
clrscr( );
cout<<” Area of Rectangle:”<<f.area(4,6)<<endl; // area of rectangle
cout<<”Area of Circle:”<<f.area(10)<<end; // area of circle
cout<<”Area of Triangle:”<<f.area(3.0, 7.0)<<endl; // area of triangle
getch();
}

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 6


CHAPTER 8 -FUNCTION OVERLOADING

OUTPUT:
Area of Rectangle: 24
Area of Circle: 314.2857
Area of Triangle: 10.5

5. Explain with a programming example to overload a function with different data type of
arguments.(U)
ANS:
int product ( int p, int q, int r);
float product ( float x, float y,float z);
int product ( int p, int q, int r) // Integer
{
cout<<”Product = “<<p * q * r << endl;
}
float product ( float x, float y, float z) // Float
{
cout<< “Product = “ << x * y*z <<endl;
}

6. What is an inline function? Explain with programming example. (U)


ANS:
Inline function is a special type of function in which the compiler replaces the function call
statement with the function code itself (expansion), instead of transferring the control to the
function.
inline is used to define inline function.

o Inline function definition starts with keyword inline.


o The inline function should be defined before all function that call it.
o The compiler replaces the function call statement with the function code itself (expansion) and
then compiles the entire code.

inline Returntype FunName ( [Argument] )


{

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 7


CHAPTER 8 -FUNCTION OVERLOADING

……….. ;
return expression ;
}

Program to find the cube of a number using inline function:


#include<iostream.h>
#include<conio.h>
inline int cube ( int a )
{
return a * a * a;
}
void main( )
{
int n ;
clrscr( );
cout<<”Enter the input number”<<endl;
cin>>n;
cout<<”Cube of “ <<n<<” = “<<cunbe(n);
getch();
}

OUTPUT:
Enter the input number 4
Cube of 4 = 64

7. What is inline function? Write the advantages of inline functions. (U)


ANS: An Inline function is a special type of function in which the compiler replaces the function
call statement with the function code itself (expansion), instead of transferring the control to the
function.
Advantage of inline function:

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 8


CHAPTER 8 -FUNCTION OVERLOADING

burden on the system for function calling.

8. What is a friend function? What are the characteristics of a friend function? (U)
ANS: A friend function is a non-member function of a class has the access permission to the
private member of the class.
The friend functions have the following properties:
o private and
protected members of the class.

function.
keyword friend. But while defining friend function it does not use
either keyword friend or : : operator.

n object
name.membername.

9. Explain friend function with syntax and example. (U)


ANS: A friend function is a non-member function of a class has the access permission to the
private member of the class.

friend function is given below:


class className
{
public:
friend returnType functionName ( [arguments] );
}

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 9


CHAPTER 8 -FUNCTION OVERLOADING

Example:

class modulas
{
private:
int a;
public:
void readdata( )
{
cout<<”Enter the Number”<<endl;
cin>>a;
}
friend int even(modulas);
};

Int even(modulas n)
{
if(n.a % 2 = = 0) //friend function can access the private data a of the object n
return 1;
else
return 0;
}

Prepared by: Sanil Abraham, KMWA Deeksha Mahalakshmipuram Page 10

You might also like