Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Inline function ,Friend function ,Friend class

Rating
-
Sold
-
Pages
6
Uploaded on
06-03-2023
Written in
2022/2023

How inline function and friend function work? Examples of inline function, friend function ,and friend class. Programs to show how inline and friend function work.

Institution
Course

Content preview

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:

Written for

Course

Document information

Uploaded on
March 6, 2023
Number of pages
6
Written in
2022/2023
Type
SUMMARY

Subjects

$8.89
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
ananyachakraborty

Also available in package deal

Get to know the seller

Seller avatar
ananyachakraborty Institute of Engineering and Management
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions