Inline function and classes
It is also possible to define the inline function inside the class. In fact, all the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly declare an inline function in the class then just declare the function inside the class and define it outside the class using the inline keyword.
Syntax:
class S { public: inline int square(int s) // redundant use of inline { // this function is automatically inline // function body } };
The above style is considered a bad programming style. The best programming style is to just write the prototype of the function inside the class and specify it as an inline in the function definition.
For Example:
class S { public: int square(int s); // declare the function }; inline int S::square(int s) // use inline prefix { }
Example:
C++
// C++ Program to demonstrate inline functions and classes #include <iostream> using namespace std; class operation { int a, b, add, sub, mul; float div ; public : void get(); void sum(); void difference(); void product(); void division(); }; inline void operation ::get() { cout << "Enter first value:" ; cin >> a; cout << "Enter second value:" ; cin >> b; } inline void operation ::sum() { add = a + b; cout << "Addition of two numbers: " << a + b << "\n" ; } inline void operation ::difference() { sub = a - b; cout << "Difference of two numbers: " << a - b << "\n" ; } inline void operation ::product() { mul = a * b; cout << "Product of two numbers: " << a * b << "\n" ; } inline void operation ::division() { div = a / b; cout << "Division of two numbers: " << a / b << "\n" ; } int main() { cout << "Program using inline function\n" ; operation s; s.get(); s.sum(); s.difference(); s.product(); s.division(); return 0; } |
Output:
Enter first value: 45 Enter second value: 15 Addition of two numbers: 60 Difference of two numbers: 30 Product of two numbers: 675 Division of two numbers: 3
Inline Functions in C++
C++ provides inline functions to reduce the function call overhead. An inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of the inline function call. This substitution is performed by the C++ compiler at compile time. An inline function may increase efficiency if it is small.
Syntax:
inline return-type function-name(parameters) { // function code }
Remember, inlining is only a request to the compiler, not a command. The compiler can ignore the request for inlining.
The compiler may not perform inlining in such circumstances as:
- If a function contains a loop. (for, while and do-while)
- If a function contains static variables.
- If a function is recursive.
- If a function return type is other than void, and the return statement doesn’t exist in a function body.
- If a function contains a switch or goto statement.
Why Inline Functions are Used?
When the program executes the function call instruction the CPU stores the memory address of the instruction following the function call, copies the arguments of the function on the stack, and finally transfers control to the specified function. The CPU then executes the function code, stores the function return value in a predefined memory location/register, and returns control to the calling function. This can become overhead if the execution time of the function is less than the switching time from the caller function to called function (callee).
For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run. However, for small, commonly-used functions, the time needed to make the function call is often a lot more than the time needed to actually execute the function’s code. This overhead occurs for small functions because the execution time of a small function is less than the switching time.