Home / Object Oriented Programming / Discuss inline function, default arguments and friend function in C++

Discuss inline function, default arguments and friend function in C++

Inline function: Inline function is an important feature that is commonly used with classes. 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. Inline function decreases execution time of a program and makes program efficient.

Default arguments: In C++, we can provide default values for function parameters. A default argument is a value provided in function declaration that is automatically assigned by the compiler if caller function does not provide a value for the arguments.

Example:

int sum (int x, int y, int w = 0, int z = 0)

{

return (x + y + w + z);

}

int main()

{

cout << sum (10, 15) << endl << sum(10, 15, 20) << endl <<sum(10, 15, 20, 25) << endl;

}

Define friend function:

Private data members cannot be accessed from outside the class. However situations arise where two classes need to share a particular function for such situation C++ introduces friend function. These are functions that can be made friendly with both classes.