Write a C++ program to demonstrate inline function .
C++ inline function is powerful
concept that is commonly used with classes. If a function is inline, the
compiler places a copy of the code of that function at each point where the
function is called at compile time.
To inline a function, place the
keyword inline before the function name
inline Return type function_name()
{
//Function body
}
Why use
Inline function
Whenever we call any function
many time then, it take a lot of extra time in execution of series of
instructions such as saving the register, pushing arguments, returning to
calling function.
Advantage of
inline Function
The main advantage of Inline
function is it makes the program faster.
#include <iostream.h>
#include<conio.h>
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
// Main function for the program
int main( )
{
clrscr();
int n1=10,n2=20,ans;
cout << "Number 1 :- " << n1<< ", Number 2 :- " << n2<<endl;;
ans=Max(20,10);
cout << "Greater Number is :- " << ans ;
getch();
} */
/*
Output :-
Number 1 :- 10, Number 2 : 20
Greater Number is :- 20
*/
Output :-
Output :-
Number 1 :- 10, Number 2 : 20
Greater Number is :- 20
we can define menber function out side the calss definition and still make it inline by just using the qualifier inline in the header line of function definition.Code :-
#include <iostream.h>
#include<conio.h>
class math
{
int no;
public:
inline int cube(int a)
{
return (a*a*a);
}
} ;
void main()
{
clrscr();
int n1;
math m1;
cout<<"Enter The Nmuber :- ";
cin>>n1;
int ans=m1.cube(n1);
cout<<"Cube of Number is = "<<ans;
getch();
}
/*
**********************************************
Output :-
Enter The Number :- 2
Cube of Number is = 8
***********************************************/
Output :-