Explain inline function & Function overloading with example.
Question
Explain inline function & Function overloading with example.
Solution
Sure, let's break it down:
- Inline Function: An inline function is a function in C++ 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. Inline function may increase efficiency if it is small.
Example:
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
In this example, Max is an inline function, it will substitute the code at the point of function call.
- Function Overloading: Function overloading is a feature in C++ where two or more functions can have the same name but different parameters. Function overloading can be considered as an example of polymorphism feature in C++. When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call.
Example:
#include <iostream>
using namespace std;
void print(int i) {
cout << " Here is int " << i << endl;
}
void print(double f) {
cout << " Here is float " << f << endl;
}
void print(char* c) {
cout << " Here is char* " << c << endl;
}
int main() {
print(10);
print(10.10);
print("ten");
return 0;
}
In this example, the function print is overloaded with different parameter types. The correct function to be called is determined by the arguments used when the function is called.
Similar Questions
Explain inline function & Function overloading with example.
difference between inline and blocked line in html
What does the following code snippet do?
Which of the following is an inline element?1 point<form><div><p><span>
In C++, which of the following function causes the full definition of the function to be inserted in each place when it gets called?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.