0% found this document useful (0 votes)
10 views

Lec -9 Functions in a Class 2 (1)

The document discusses inline functions in C++, explaining how they reduce function call overhead by expanding the function code at the call site. It also covers the concept of passing class objects as function parameters and returning them, highlighting the use of friend functions for accessing private members across classes. Additionally, it mentions the use of constant arguments to prevent modification of function parameters.

Uploaded by

Aksjsy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lec -9 Functions in a Class 2 (1)

The document discusses inline functions in C++, explaining how they reduce function call overhead by expanding the function code at the call site. It also covers the concept of passing class objects as function parameters and returning them, highlighting the use of friend functions for accessing private members across classes. Additionally, it mentions the use of constant arguments to prevent modification of function parameters.

Uploaded by

Aksjsy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Object Oriented

Programming
Mohona Ghosh
Inline Functions

When the program executes the function call instruction the CPU stores the memory address of the
instruction following the function call, copies the arguments of the function on the stack and finally transfers
control to the specified function. The CPU then executes the function code, stores the function return value
in a predefined memory location/register and returns control to the calling function.

• https://www.youtube.com/watch?v=ygK0YON10sQ
Inline Functions
• This can become overhead if the execution time of function is less than the switching
time from the caller function to called function (callee).
• For functions that are large and/or perform complex tasks, the overhead of the function
call is usually insignificant compared to the amount of time the function takes to run.
However, for small, commonly-used functions, the time needed to make the function call
is often a lot more than the time needed to actually execute the function’s code. This
overhead occurs for small functions because execution time of small function is less than
the switching time.
• C++ provides an inline functions to reduce the function call overhead.
• Inline function is a function that is expanded in line when it is called. When the inline
function is called whole code of the inline function gets inserted or substituted at the
point of inline function call. This substitution is performed by the C++ compiler at
compile time.
• So the overhead of function calling is reduced
• Inline function may increase efficiency if it is small.
Inline Functions
Inline Functions
Some Important points about Inline Functions
• All the functions defined inside class definition are by default
inline
• We must keep inline functions small, small inline functions have better
efficiency.
• Inline functions do increase efficiency, but we should not make all the
functions inline. Because if we make large functions inline, it may lead to
code bloat, i.e., makes the program to take up more memory because the
statements that define the inline function are reproduced at each point
where the function is called and this might affect the speed too.
• Hence, it is advised to define large functions outside the class definition
using scope resolution :: operator, because if we define such functions
inside class definition, then they become inline automatically.
Inline Functions
Friend Function
Friend Function

Let’s have a look at “passing objects as arguments to functions” first…


Passing Class object as Function parameter
• In C++ we can pass class’s objects as arguments and also return them
from a function the same way we pass and return other variables.
• No special keyword or header file is required to do so.
Passing Class object as
Function parameter

• In this Example there is a class which


has an integer variable ‘a’ and a
function ‘add’ which takes an object
as argument.
• The function is called by one object
and takes another as an argument.
• Inside the function, the integer value
of the argument object is added to
that on which the ‘add’ function is
called.
• In this method, we can pass objects
as an argument and alter them.
Returning Class object in Function
• In the previous example we can see that the add function does not
return any value since its return-type is void.
• In the next program, the add function returns an object of type
‘Example'(i.e., class name) whose value is stored in E3.
Passing Class object as
Function parameter
• In this example, we can see both the things that
are how we can pass the objects as well as return
them.
• When the object E3 calls the add function it
passes the other two objects namely E1 & E2 as
arguments.
• Inside the function, another object is declared
which calculates the sum of all the three variables
and returns it to E3.
• This code and the previous code is almost the
same, the only difference is that this time the add
function returns an object whose value is stored
in another object of the same class ‘Example’ E3.
• Here the value of E1 is displayed by object1, the
value of E2 by object2 and value of E3 by object3.
Returning Back to Friend Function
Friend Function Example
Friend Function Example
Friend Function between multiple classes
#include<iostream> int main()
using namespace std; {
A object1; // Object of class A
class B; B object2; // Object of class B
class A
{
cout << add(object1, object2);
return 0; Example
int a1 = 9; }
public:
friend int add(A, B);
};

class B
{
int b1 = 10;
public:
friend int add(A, B);
};
int add(A a, B b)
{
return a.a1 + b.b1;
}
Default Arguments
Constant Arguments
• In C++, an argument to a function can be declared as const.
• The argument with constant value should be initialized during the
function declaration.
• Syntax
type function_name(const data_type variable_name=value);
• For example
int max(const int a=3, int b); //function prototype
• The qualifier const tells the compiler that the value of that argument
cannot be modified, and any such attempt will generate a compile
time error.
OUTPUT:
10
0

You might also like