Inline functions are actual functions, which are copied everywhere during compilation, like preprocessor macro, so the overhead of function calling is reduced. All the functions defined inside class definition are by default inline, but you can also make any non-class function inline by using keyword inline with them. We must keep inline functions small, small inline functions have better efficiency. Inline functions do increase efficiency, but we should not make all the functions inline. Because if we make large functions inline, it may lead to code bloat, and might affect the speed too. Hence, it is adviced to define large functions outside the class definition using scope resolution :: operator, because if we define such functions inside class definition, then they become inline automatically.
Limitations of Inline Functions1. Large Inline functions cause Cache misses and affect performance negatively.
2. Compilation overhead of copying the function body everywhere in the code on compilation, which is negligible for small programs, but it makes a difference in large code bases.
3. Also, if we require address of the function in program, compiler cannot perform inlining on such functions. Because for providing address to a function, compiler will have to allocate storage to it. But inline functions doesn't get storage, they are kept in Symbol table.
WAP to findout the biggest number among two numbers.
#include <iostream.h>
#include <conio.h>
class sample
{
public:
inline float calprint(double x,double y)
{
if(x>y)
return x;
else
return y;
}
};
void main()
{
clrscr();
floata,b;
cout<<"Enter two numbers";
cin>>a>>b;
sample s;
cout<<"The biggest of two numbers is "<<s.calprint(a,b);
doublep,q;
cout<<"Enter two numbers";
cin>>p>>q;
cout<<"The biggest of two numbers is "<<s.calprint(p,q);
getch();
}
PROGRAM: Calculate the product and cube of numbers
#include <iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
lineobj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value is:" <<obj.mul(val1,val2);
cout<<"\n\nCube value is :" <<obj.cube(val1) <<"\t"<<obj.cube(val2);
getch();
}
Inline function with using classes.
#include<iostream.h>
#include<conio.h>
class sample
{
public:
float calprint(double x,double y);
};
inline float sample::calprint(double x,double y)
{
if(x>y)
return x;
else
return y;
}
void main()
{
clrscr();
floata,b;
cout<<"Enter two numbers";
cin>>a>>b;
sample s;
cout<<"The biggest of two numbers is "<<s.calprint(a,b);
doublep,q;
cout<<"Enter two numbers";
cin>>p>>q;
cout<<"The biggest of two numbers is "<<s.calprint(p,q);
getch();
}
#include<iostream.h>
#include<conio.h>
class sample
{
public:
float calprint(double x,double y);
};
inline float sample::calprint(double x,double y)
{
if(x>y)
return x;
else
return y;
}
void main()
{
clrscr();
floata,b;
cout<<"Enter two numbers";
cin>>a>>b;
sample s;
cout<<"The biggest of two numbers is "<<s.calprint(a,b);
doublep,q;
cout<<"Enter two numbers";
cin>>p>>q;
cout<<"The biggest of two numbers is "<<s.calprint(p,q);
getch();
}