Inline and Friend Operators

Inline and Friend Operators


 Friend Function

  • A function which is not a member function of a class, but can still access the private data member of a class is called as friend function.
  • To declare a function as friend, we precede the function name with the keyword friend.
  • Friend function may pass an object as its argument
Example of Friend Function
class abc
{
private:
int x;
public:
friend void fun(); //fun function is now friend function of class abc
};
void fun() /*friend function defined outside class without scope resolution operator (::) -
       fun is not a member function of class abc*/
{
abc ob;
ob.x=10; //friend function accessing private data member x of class abc
cout<<ob.x;
}
void main()
{
fun(); //friend function called (without object and dot operator)
}
Output: 10

Inline Function


When a function gets called, it takes a lot of extra time in executing instructions for tasks like jumping to the function, saving registers, pushing arguments into the stack and returning to the calling function. When a function is small, a substantial percentage of execution time may be spent in such overheads. To eliminate the cost of calls to small functions, C++ proposes a new feature called as inline function. An inline function is a function that is expanded in line when it is invoked. That is, the compiler replaces the function call with the corresponding function code (something similar to macro expansion. But in macros, usual error checking does not occur during compilation). To make the function as inline, we precede the function name with the keyword inline, in the function definition. Inline functions must be defined before they are called.

Example:
inline double cube(double a)
{
return (a*a*a);
}
This inline function can be invoked by statements like
c = cube(3.0);
d = cube(2.5+1.5);

On the execution of these statements, the values of c and d will be 27 and 64 respectively.

Example of inline function

#include<iostream.h>
#include<conio.h>
inline void fun() //inline function must be defined before it is called
{
int a,b;
cin>>a>>b;
cout<<a+b;
}
void main()
{
clrscr();
fun();//inline function called
getch();
}

Output: 
10
20
30

The speed benefits of inline functions diminish as the function grows in size. At some point, the overhead of the function call becomes small compared to the execution of the function, and the benefits of inline function may be lost. In such cases, the use of normal functions will be more meaningful. Usually, when a function is small enough to be defined in one or two lines, then such a function can be made as inline. The inline keyword merely sends a request, not a command, to the compiler. The compiler may ignore this request if the function definition is too long or too complicated and compile the function as a normal function.

Some of the situations where inline expansion may not work are:

  1. For functions returning values, if a loop, switch or goto exists.
  2. For functions not returning values, if a return statement exists.
  3. If functions contain static variables.
  4. If inline functions are recursive.


Comments

Popular posts from this blog

Function overloading and Operator overloading

Exceptional Handling