Inline Functions and Inline Member Functions in C++ with Examples

An Inline function is a function declared using the inline keyword. It allows a function to be defined in a way that permits multiple identical definitions across translation units, subject to the One Definition Rule (ODR). It also traditionally serves as a request to the compiler to consider replacing a function call with the function's code. The compiler is not required to perform inline substitution. It decides whether to inline a function based on optimization and other factors.

Inline Function वह function होता है जिसे inline keyword की सहायता से declare किया जाता है। यह function को इस प्रकार define करने की सुविधा देता है कि ODR के नियमों के अंतर्गत इसकी समान definitions multiple translation units में हो सकती हैं। Traditionally, यह compiler को function call की जगह function का code रखने का सुझाव भी देता है। Compiler के लिए inline substitution करना अनिवार्य नहीं होता। वह optimization और अन्य factors के आधार पर निर्णय लेता है।

1. Defining an Inline Function Using the inline Keyword

In C++, the inline keyword is used before the function's return type to declare an inline function.

C++ में inline function declare करने के लिए function के return type से पहले inline keyword का उपयोग किया जाता है।

Syntax

inline return_type functionName(parameters)
{
// Function Body
}

Example: Inline Function in C++

#include <iostream>
using namespace std;
inline int square(int n)
{
return n * n;
}
int main()
{
int num = 5;
cout << "Number: " << num << endl;
cout << "Square: " << square(num);
return 0;
}

Output:

Number: 5
Square: 25

Explanation:

  • inline is the keyword used to declare an inline function.

  • square() is an inline function.

  • It accepts an integer argument and returns its square.

  • The function is called using square(num).

व्याख्या:

  • inline एक keyword है जिसका उपयोग inline function declare करने के लिए किया जाता है।

  • square() एक inline function है।

  • यह एक integer argument accept करता है और उसका square return करता है।

  • Function को square(num) की सहायता से call किया गया है।

2. Advantages of Inline Functions in C++

Inline functions offer several benefits when used appropriately, especially for small functions.

Inline Functions का सही तरीके से उपयोग करने पर कई फायदे मिलते हैं, विशेष रूप से छोटे functions के लिए।

  1. Reduced Function Call Overhead: When the compiler performs inline substitution, the overhead of a normal function call may be avoided.

    Function Call Overhead में कमी: जब compiler inline substitution करता है, तो सामान्य function call का overhead कम हो सकता है।

  2. Suitable for Small Functions: Inline functions are commonly useful for short functions that perform simple operations.

    छोटे Functions के लिए उपयोगी: Inline functions छोटे और simple operations करने वाले functions के लिए उपयोगी हो सकते हैं।

  3. Code Organization: Small functions can be defined clearly while keeping the program modular.

    Code Organization: छोटे functions को define करके program को modular और व्यवस्थित बनाया जा सकता है।

  4. Type Checking: Unlike macros, inline functions follow normal C++ function type-checking rules.

    Type Checking: Macros के विपरीत, inline functions सामान्य C++ function type-checking rules follow करते हैं।

3. Inline Member Functions in C++

An Inline member function is a member function of a class that is inline. A member function defined inside the class definition is implicitly inline, subject to the rules of C++. Inline member functions are useful for short operations involving class data members.

Inline Member Function वह member function होता है जो किसी class का हिस्सा होता है और inline होता है। Class definition के अंदर define किया गया member function C++ के नियमों के अनुसार implicitly inline होता है। Inline Member Functions का उपयोग class के data members पर छोटे operations perform करने के लिए किया जा सकता है।

4. Defining Inline Member Functions Inside a Class

When a member function is defined inside the class, its complete function body is written within the class definition. Such a function is implicitly inline.

जब किसी member function को class के अंदर define किया जाता है, तो उसका पूरा function body class definition के अंदर लिखा जाता है। ऐसा function implicitly inline होता है।

Example: Inline Member Function Defined Inside a Class

#include <iostream>
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
void setData(int l, int b)
{
length = l;
breadth = b;
}
int area()
{
return length * breadth;
}
};
int main()
{
Rectangle r1;
r1.setData(10, 5);
cout << "Area of Rectangle: " << r1.area();
return 0;
}

Output:

Area of Rectangle: 50

Explanation:

  • Rectangle is a class.

  • length and breadth are private data members.

  • setData() and area() are member functions.

  • Both member functions are defined inside the class and are implicitly inline.

  • The object r1 is used to call these functions.

व्याख्या:

  • Rectangle एक class है।

  • length और breadth private data members हैं।

  • setData() और area() member functions हैं।

  • दोनों member functions class के अंदर define किए गए हैं, इसलिए ये implicitly inline हैं।

  • इन functions को call करने के लिए r1 object का उपयोग किया गया है।

5. Defining Inline Member Functions Outside a Class

A member function can also be defined outside the class using the scope resolution operator (::). If we want to explicitly declare such a function as inline, we use the inline keyword in its definition.

Member function को class के बाहर Scope Resolution Operator (::) की सहायता से भी define किया जा सकता है। यदि ऐसे function को explicitly inline declare करना हो, तो उसकी definition में inline keyword का उपयोग किया जाता है।

Syntax

class ClassName
{
public:
return_type functionName(parameters);
};
inline return_type ClassName::functionName(parameters)
{
// Function Body
}

Example: Inline Member Function Defined Outside a Class

#include <iostream>
using namespace std;
class Rectangle
{
private:
int length;
int breadth;
public:
void setData(int l, int b);
int area();
};
inline void Rectangle::setData(int l, int b)
{
length = l;
breadth = b;
}
inline int Rectangle::area()
{
return length * breadth;
}
int main()
{
Rectangle r1;
r1.setData(8, 4);
cout << "Area of Rectangle: " << r1.area();
return 0;
}

Output:

Area of Rectangle: 32

Explanation:

  • The Rectangle class declares two member functions: setData() and area().

  • Their definitions are written outside the class.

  • The scope resolution operator (::) specifies that these functions belong to the Rectangle class.

  • The inline keyword is used explicitly in their definitions.

व्याख्या:

  • Rectangle class में setData() और area() नाम के दो member functions declare किए गए हैं।

  • इनकी definitions class के बाहर लिखी गई हैं।

  • Scope Resolution Operator (::) बताता है कि ये functions Rectangle class के members हैं।

  • इनकी definitions में explicitly inline keyword का उपयोग किया गया है।

Note: For a function declared in a header file and defined outside the class, the inline definition should generally be visible in every translation unit that uses it. This is commonly achieved by placing the definition in the header file.

ध्यान दें: यदि किसी function को header file में declare करके class के बाहर define किया जाता है, तो उसकी inline definition सामान्यतः प्रत्येक उस translation unit में उपलब्ध होनी चाहिए जो उसका उपयोग करती है। इसके लिए definition को header file में रखा जाता है।

6. Difference Between Inline Functions and Inline Member Functions

An inline function can be a standalone function, whereas an inline member function belongs to a class.

Inline Function एक standalone function हो सकता है, जबकि Inline Member Function किसी class का member होता है।

BasisInline FunctionInline Member Function
MeaningA standalone function declared inlineA class member function that is inline
DeclarationDeclared outside a classDeclared inside a class or as a class member
AccessCannot directly access private class members without appropriate accessCan access the class's private and protected members
DefinitionCan be defined outside a classCan be defined inside or outside a class
Exampleinline int square(int n)int area() defined inside a class

Important Note About Inline Functions

The inline keyword does not guarantee that the compiler will replace every function call with the function body. Modern C++ compilers make their own optimization decisions. Also, a function defined inside a class is implicitly inline even if the inline keyword is not written explicitly. 

inline keyword इस बात की guarantee नहीं देता कि compiler प्रत्येक function call को function body से replace करेगा। Modern C++ compilers अपने optimization decisions स्वयं लेते हैं। इसके अलावा, class के अंदर define किया गया function inline keyword explicitly लिखे बिना भी implicitly inline होता है। 

Inline functions are useful for organizing small functions and may reduce function call overhead when the compiler performs inline substitution. Inline member functions are class member functions that follow the same inline rules. In C++, member functions defined inside a class are implicitly inline, while functions defined outside the class can be explicitly declared inline using the inline keyword.

Inline Functions छोटे functions को व्यवस्थित करने के लिए उपयोगी होते हैं और compiler द्वारा inline substitution किए जाने पर function call overhead कम कर सकते हैं। Inline Member Functions वे class member functions होते हैं जो inline के समान नियमों का पालन करते हैं। C++ में class के अंदर define किए गए member functions implicitly inline होते हैं, जबकि class के बाहर define किए गए functions को inline keyword की सहायता से explicitly inline declare किया जा सकता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)