Inline Functions in C++
January 16, 2021
Inline functions are small functions defined with the inline keyword. The advantage of using inline functions are –
- They do not involve the usual stack operations as with ordinary function calls and returns, thus speeds up execution
- Usually used within classes which involves a lot of operations (small) on the private data
- It is professional to define the inline functions within their class declarations. In this case, all such member functions are automatically inline irrespective of the declaration inline.
An example C++ program using inline functions is shown below –
//inline1.cpp
#include <iostream>
#include <string>
using namespace std;
class sum{
int a,b;
public:
void set_num(int i, int j){
a=i;
b=j;
}
int get_sum(){
return(a+b);
}
};
main(){
sum s;
int k,l;
cin >> k >> l;
s.set_num(k,l);
cout << "Sum is>>";
cout << s.get_sum();
return 0;
}
Output:
2
4
Sum is>>6