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 namedRectangle
.private:
: Thelength
andbreadth
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
andb
, which are used to initialize the private member variableslength
andbreadth
. - 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 multiplyinglength
bybreadth
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 theRectangle
class namedrect
. The constructor is called with10
and5
as arguments, settinglength
to10
andbreadth
to5
.cout << "Area: " << rect.area() << endl;
: This line calls thearea()
function on therect
object to calculate the area. Sincearea()
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.