Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Inline Function

Introduction

An Inline function is a function where the compiler is instructed to insert the function’s code directly at the point where the function is called, rather than performing a traditional function call.

Purpose: The main goal is to reduce the overhead of function calls, especially for small, frequently used functions.

Where Defined: Usually defined inside the class definition.

For example:

#include <iostream>
using namespace std;

class Rectangle {
private:
    int length;
    int breadth;

public:
    // Constructor to initialize the rectangle
    Rectangle(int l, int b){
        length = l;
        breadth = b;
    }

    // Inline function to calculate the area
    inline int area() const {
        return length * breadth;
    }
};

int main() {
    Rectangle rect(10, 5);
    cout << "Area: " << rect.area() << endl;
    return 0;
}

Output:

Area: 50

Explanation:

Step 1: Include Necessary Libraries:

#include <iostream>
using namespace std;
  • Explanation: This includes the standard input-output stream library, which allows the program to use cout for printing output to the console.

Step 2: Defining the ‘Rectangle’ Class:

class Rectangle {
private:
    int length;
    int breadth;
  • Explanation:
    • class Rectangle {}: This defines a class named Rectangle.
    • private:: The length and breadth variables are declared as private, meaning they cannot be accessed directly from outside the class. This is part of the concept of data hiding.

Step 3: Constructor to Initialize the Rectangle:

public:
    // Constructor to initialize the rectangle
    Rectangle(int l, int b){
        length = l;
        breadth = b;
    }
  • Explanation:
    • public:: The following members are accessible from outside the class.
    • Constructor:Rectangle(int l, int b) is a constructor that takes two parameters, l and b, which are used to initialize the private member variables length and breadth.
    • When you create an object of the Rectangle class, this constructor is called automatically to set up the initial state of the object.

Step 4: Inline Function to Calculate the Area:

    // Inline function to calculate the area
    inline int area() const {
        return length * breadth;
    }

Explanation:

  • Inline Function:
    • inline: This keyword suggests to the compiler that the function should be expanded inline at the point of each call. It means that instead of calling the function, the code inside the function replaces the function call.
    • int area() const {}: This function calculates and returns the area of the rectangle.
    • return length * breadth;: This calculates the area by multiplying length by breadth and returning the result.

Step 5: Main Function:

int main() {
    Rectangle rect(10, 5);
    cout << "Area: " << rect.area() << endl;
    return 0;
}

Explanation:

  • Rectangle rect(10, 5);: This line creates an object of the Rectangle class named rect. The constructor is called with 10 and 5 as arguments, setting length to 10 and breadth to 5.
  • cout << "Area: " << rect.area() << endl;: This line calls the area() function on the rect object to calculate the area. Since area() is an inline function, the compiler may replace this call with the code inside the function. The result, 50 (since 10 * 5 = 50), is then printed to the console.

    Leave a Reply

    Your email address will not be published.

    Need Help?