Inline function, friend function, default argument
Inline function:
To save the execution time that gets lost in the overheads of calling a function, the compiler can be instructed to insert the code wherever function is called. An inline function is a function whose code is expanded in line at the point at which it is invoked, rather than being called. There are two ways to create an inline function. The first is to use the inline qualifier.
inline int f()
{
..........
}
There is another way to create an inline function. This is accomplished by defining the code of a member function inside a class declaration ( .h file).
Friend function:
If a member of a particular class is private or protected, functions outside the class cannot access the non-public members of the class. This is the primary objective of encapsulation. However, at times, it becomes a problem for the programmer. In order to
access the nonpublic members of a class, C++ provides the friend keyword. Any non-member function may be declared a friend by a class, in which case the function may directly access the private member attributes and methods of the class objects.
Default arguments:
In C++, you can give a parameter a default value that is automatically used when no argument corresponding to that parameter is specified in a call to a function.
For example, the constructor for the class fraction can be declared with default arguments
fraction (int num=0, int denom=1);
and the definition will be
fraction( int num, int denom)
{
numerator=num/gcd(num, denom);
denominator = denom/gcd(num, denom);
}
A function can be called with zero, one or two arguments. Missing arguments must be the trailing arguments and get the default values. Note that if one argument is missing when the function is called it is assumed to be the last argument.
Thanks
Mukesh Rajput
Post A Comment:
0 comments: