The keyword inline is used as a function specifier only in function declarations. The inline specifier is a hint to the compiler that inline substitution of the function body isto be preferred to the usual function call implementation.
The advantage of using inline member functions are:
1. The size of the object code is considerably reduced.
2. It increases the execution speed
3. The inline member functions are compact functions calls.
The general form is:
class user_name
{
//data variables;
public:
inline return_type function name (parameters);
//other member functions;
}
Whenever a function is declared with inline specifier, the C++compiler merely
replaces it with the code itself so the overhead of the transfer of control between the
calling portion of a program and a function is reduced.
// demonstration of inline function
# include <iostream.h>
class sample
{
int x;
public:
inline void getdata( );
inline void display ( );
}
inline void sample :: getdata ( )
{
cout<<" enter a number"<<endl;
cin>>x;
}
inline void sample:: display ( )
{
cout<<"entered number is = "<<x<<endl;
}
void main( )
{
sample ob;
ob.getdata ( );
ob.display ( );
}
0 Comments