Object Oriented Programming: Chapter 2: Classes and Objects

Inline Function

C++ Programming | Object Oriented Programming

Questions: 1. Compare inline functions of C++ with ordinary functions and with macros. 2. What is dynamic binding? How is it achieved.

Inline Function

•  When we define a function normally the compiler makes a copy of that definition in the memory. And when a call to that function is made the compiler jumps to those copied instructions and when the function returns, the execution resumes from the next line in the calling function. Hence if we make a call to that function for 5 times then each time the copied block of function in the memory will be referred by the compiler. This also means that there is only one copy of the function definition in the memory and on each call to that function the same copy is referred.

•  Now if there are many calls to that function and function contains very few lines of code then such a jumping to memory becomes a performance overhead for the compiler. It ultimately slows down the execution of the program. Hence the solution is to make the function inline.

 

Definition:

The inline function is a function whose code is copied in place of each function call. The inline specifies that the compiler should insert the complete body of function in every context where the function is used.

 

C++ Program

 #include<iostream>

 using namespace std;

inline int sum(int x,int y)

{

     return (x+y);

}

void main()

{

     int a,b;

     a=10;

     b=20;

     cout<<"The sum is "<<sum(a,b);

}

Output

The sum is 30

 

Situations in which inline function may not work:

•  Following are the situations in which the inline functions may not work ‒

1. For the functions that contain the static variables.

2. For the functions returning values if ‒for loop, switch or goto exists.

3. For the functions not returning a function and if return statement exists.

4. If the inline functions are recursive function.


Inline functions Vs macros

• The major difference between inline functions and macros is the way they are handled. Inline functions are analyzed by the compiler, whereas macros are expanded by the C++ preprocessor.

• Macro invocation do not perform type checking or do not check that arguments are well‒formed whereas the inline function can do these tasks.

• Macro can not return any value whereas the inline function can return some  value.

•  Error correction is simpler in case of inline function as compared to Macro  function.

 

Example: 1

 Write a C++ program to find maximum of two numbers using inline functions

Solution:

#include<iostream>

using namespace std;

inline int max_element(int x,int y)

{

    if (x > y)

        return x;

    else

        return y;

}

void main()

{

    int a, b;

    cout << "\n Enter value of a:";

    cin >> a;

    cout << "\n Enter value of b;";

    cin >> b;

    cout << "\n The " << max_element(a, b) << " is maximum!!!";

}

Output

Enter value of a: 10

Enter value of b: 20

The 20 is maximum!!!

 

Review Questions

1. Compare inline functions of C++ with ordinary functions and with macros.

2. What is dynamic binding? How is it achieved.

 

Object Oriented Programming: Chapter 2: Classes and Objects : Tag: Oops, Computer Programming : C++ Programming | Object Oriented Programming - Inline Function


Object Oriented Programming: Chapter 2: Classes and Objects



Under Subject


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation



Related Subjects


English Essentials II

EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation



Linear Algebra

MA25C02 2nd Semester | 2025 Regulation


Applied Physics (CSIE) II

PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Digital Principles and Computer Organization

CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electrical and Electronics Engineering

EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs)

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Re-Engineering for Innovation

ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation


Object Oriented Programming (OOPs) - Laboratory

CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation