Inline function in c++


INLINE FUNCTION

INLINE FUNCTION:

The inline function is the concept used commonly with classes. When the function is inline, the compiler places a copy of the code of that function at each point.


Conditions for the Inline function:
         Inline functions should not contain any loops, recursion.
-            The inline function is only applicable for a smaller function.
-           
Sample Example:
void display();
main()
{
  display();
}
void display()
{
  cout<<”Hello world”;
}


Syntax:
Inline return-type function-name (parameters)
{
   -------------------
   -------------------
}


Working Examples:

Squaring of the given value (Single function, Square):

#include <iostream>                                                       //Header file.
using namespace std;
inline int square(int a)
{
    return a*a;                                                        //Multiplying of the value is taking place
}
int main()
{
    cout << "The value is: " << square(4) << "\n";       // 4 is the value that gets squared.
    return 0;
}

Output: 16, the square of value 4.





Two function using Inline:

#include <iostream>                           //header file
using namespace std;
inline void hello()                                //function hello.
{
  cout<<"hello \n";
}
inline void world()                              //function world.
{
    cout<<"world";
}
int main()                                             //main function.
{
  hello();                                              //declaration of hello function.
  world();                                             //declaration of world function.
  cin.get();
}

Output: hello
 .            world



Demonstration of the concept:

#include <iostream>
using namespace std;
class Example                                                 //class name.
{
    int x,y,add,sub,mul;                                    //declarations.
public:
    void get();
    void addition();                                            //functionalities which are public.
    void Substract();
    void multiply();
};

inline void Example::get()                                     //function get()
{
    cout << "Please enter first value:";
    cin >> x;
    cout << "Please enter second value:";
    cin >> y;
}
inline void Example::addition()                                                //function to add two numbers
{
    add = x+y;
    cout << "Addition of two numbers: " << x+y << "\n";
}
inline void Example ::Substract()                                           //function to subtract two numbers
{
    sub = x-y;
    cout << "Difference of two numbers: " << x-y << "\n";
}
inline void Example:: multiply()                                               //function to multiply two numbers
{
    mul = x*y;
    cout << "Product of two numbers: " << x*y << "\n";
}
int main()                                                                                          //Main-function
{
    cout << "Program using inline function\n";
    Example s;
    s.get();
    s.addition();
    s.Substract();
    s.multiply();
    return 0;
}
 
Input: Please enter first value: 2 (random value)
            Please enter second value: 3 (random value)

Output: Addition of two numbers: 5
               Difference of two numbers: -1
               Product of two numbers: 6


No comments:

Powered by Blogger.