C++ - Friend Function and Inline Function




Friend Function And Inline Function



Friend Function

We know that private member function can not access outside the class but many times it happens that the member function of private  section ay need by other class member function so that we use friend function.
Because of friend function we use that particular function without using class.

 Syntax:

friend return_type function_name (argument list);

The friend function definition always created by outside the class .

Eg:

class emp
{
------
------
Public:
void getdata ();
friend int printdata();
};
 void emp :: getdata()            //normal function.
{
----
-----

}

int printdata()               //friend function definition.
{
---
---

}

Friend function act  like normal function .
It has object as argument
Lets see one program with using friend function.

Simple addition program using friend function .



#include<iostream>
using namespace std;

class  addd
{
    int a,b;
public:
    void accept();
    friend void add(addd ob);
};

void addd :: accept()
{
   
    cout<<"enter a: ";
    cin>>a;
    cout<<"enter b: ";
    cin>>b;
   
}
void add(addd ob)
{
    int  c= ob.a+ob.b;
    cout<<c;
}
int main(int argc, char** argv) {
    addd ob;
    ob.accept();
    add(ob);
   
    return 0;
}

**************************
Output
enter a: 5
enter b: 3
8


Inline Function

If normal function call many times it may takes more time for execution science they take time to expand their functionality many times .so inline function is very useful method if function is large in size.
Inline function provide type checking they are act like real function. So next thing came to your mind what is type checking. Type checking use to find out the errors at compile time and runtime. Basically type checking has two types  static and dynamic type checking so static will done at compile time and dynamic is done at run time.


Ex of Static type checking

int a=10;
so static type checking check the datatype of  variable. It is used in C, C++ , JAVA ,C#.


Ex of Dynamic type checking

a=10;
str=”Rohan”
so in Dynamic type checking compiler itself know what is data type of variable .
it used in  Python, VBScript programming language.

Below is  program of Normal function.

Eg:


void add (int a, int b)
{
c=a+b;
}
int main()
{
add(10,20);
add(20,210);
add(20,39);
}

So when we run above program to much time is wasted.

So we use inline function.

Syntax of inline function:

inline return type function name(arguments);

Below is program use to find square of number using inline function.

#include <cstdlib>
#include<iostream>
using namespace std;
 inline int squre(int n)
{
    return(n*n);
}
int main(int argc, char** argv) {
    int num;
    cout<<"enter the number:: ";
    cin>>num;
    int sq=squre(num);
    cout<<"squre is : "<< sq;
    return 0;
}

****************************************
Output
enter the number:: 5
squre is : 25




Difference between normal function and inline function


                 Normal Function


                    Inline Function
Code is large in size.
There is small size of code.

Return as prototype declaration .

Keyword Inline prefix is used.
Compile and execution time is High

Compile and execution time is low or less as compare to normal function.

Looping is done in for,while,do-while etc.


Looping facility will not used.




Comments