What is Inline function?
inline function is powerful concept in C++. If a function is inline, the compiler places a copy of the code
of that function at each point where the function is called at compile time. place the keyword inline before
the function name and define the function before any calls are made to the function.
In the normal case if we call any function, f1( ) from the main() function, the control is transferred to the
definition of the f1( ) function. The addresses from where the function is called and the definition of the
function are different. This control transfer takes a lot of time and increases the overhead. But there is no
need to control transfer which saves a lot of time. It is useful when a function is frequently used in a
program.
We cannot provide the inlining to the functions in the following circumstances:
⮚ If a function is recursive.
⮚ If a function contains a loop like for, while, do-while loop.
⮚ If a function contains a switch or go to statement
An inline function can be used in the following scenarios:
⮚ An inline function can be used when the performance is required.
⮚ We can use the inline function outside the class.
Example-1
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
int main()
{
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
, Example-2
#include<iostream>
using namespace std;
inline int area(int length, int breadth)
{
return length * breadth ;
}
inline int perimeter(int length, int breadth)
{
return 2*(length+breadth) ;
}
int main()
{
int length, breadth, result1,result2;
cout << "Enter the length of the rectangle: "<<endl;
cin >> length;
cout << "Enter the breadth of the rectangle: "<<endl;
cin >> breadth;
result1 = area(length , breadth);
cout << "The area of the rectangle is: " << result1<<endl;
result2 = perimeter(length , breadth);
cout << "The perimeter of the rectangle is: " << result2;
return 0 ;
}
Friend function in C++:
In C++ if a function is declared as a friend function, then the private and protected data member can be
access using the function. For accessing the data, the declaration of a friend function should be done
inside the body of a class starting with the keyword friend. It cannot access the member names directly
and has to use an object name.
Example of friend function:
#include <iostream>
using namespace std;
class B; // forward declaration.
class A
{
int x;
public:
inline function is powerful concept in C++. If a function is inline, the compiler places a copy of the code
of that function at each point where the function is called at compile time. place the keyword inline before
the function name and define the function before any calls are made to the function.
In the normal case if we call any function, f1( ) from the main() function, the control is transferred to the
definition of the f1( ) function. The addresses from where the function is called and the definition of the
function are different. This control transfer takes a lot of time and increases the overhead. But there is no
need to control transfer which saves a lot of time. It is useful when a function is frequently used in a
program.
We cannot provide the inlining to the functions in the following circumstances:
⮚ If a function is recursive.
⮚ If a function contains a loop like for, while, do-while loop.
⮚ If a function contains a switch or go to statement
An inline function can be used in the following scenarios:
⮚ An inline function can be used when the performance is required.
⮚ We can use the inline function outside the class.
Example-1
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
int main()
{
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
, Example-2
#include<iostream>
using namespace std;
inline int area(int length, int breadth)
{
return length * breadth ;
}
inline int perimeter(int length, int breadth)
{
return 2*(length+breadth) ;
}
int main()
{
int length, breadth, result1,result2;
cout << "Enter the length of the rectangle: "<<endl;
cin >> length;
cout << "Enter the breadth of the rectangle: "<<endl;
cin >> breadth;
result1 = area(length , breadth);
cout << "The area of the rectangle is: " << result1<<endl;
result2 = perimeter(length , breadth);
cout << "The perimeter of the rectangle is: " << result2;
return 0 ;
}
Friend function in C++:
In C++ if a function is declared as a friend function, then the private and protected data member can be
access using the function. For accessing the data, the declaration of a friend function should be done
inside the body of a class starting with the keyword friend. It cannot access the member names directly
and has to use an object name.
Example of friend function:
#include <iostream>
using namespace std;
class B; // forward declaration.
class A
{
int x;
public: