Inline Member Functions
In situations, where a small
function is getting called several times in a program, there are certain
overheads involved while calling a function. Time has to be spent on passing
values, passing control, returning value and returning control. In such a
situations to save time, we can instruct the C++ compiler to put the code in
the function body directly inside the code in the calling program. Such
functions are called inline functions.
Member functions are made inline
by either of the following two methods:
1.
By defining the function within the class itself.
2.
By only prototyping and not defining the function
within the class. The function is defined outside the class using scope
resolution operator. The definition of the function is prefixed by the keyword
“Inline”.
EXAMPLE:
#include<iostream.h>
#include<conio.h>
inline int rectangle(int y) //declaring an inline function
{
return 5*y;
}
void main()
{
int x;
clrscr();
cout<<”enter
the input value: ”;
cin>>x;
x =
rectangle(x); // Calling an inline
function. This statement will be replaced by
the code of function rectangle.
cout<<”\n
the output is:”<<x;
}
Output:
Enter the input value: 5
The output is 25
No comments:
Post a Comment