What is the difference between friend function and inline function? Explain with examples?
Answers
Answer:
A friend function is used to access non public members of the class. A friend function cannot be called by class object.
Friend keyword is used to define the friend function.
And the Inline functions are functions where the call is made to inline functions.
Answer: The main difference between a friend function and an inline function is that a friend function is a non-member function that can access the private and protected members of a class, while an inline function is a member function that is expanded in line when it is called to improve performance.
Explanation: A friend function is a non-member function that has been granted access to the private and protected members of a class. It is declared with the keyword "friend" in the class, but is defined outside of the class. It is not considered a member of the class, so it cannot be called using an object of the class, but it can access the class's private and protected members. Here's an example of how to declare and use a friend function:
class MyClass {
private:
int value;
public:
friend int getValue(MyClass&);
};
int getValue(MyClass &obj)
{
return obj.value;
}
int main()
{
MyClass obj;
int val = getValue(obj);
return 0;
}
An inline function is a function that is expanded in line when it is called, instead of generating a function call. The compiler replaces the function call with the actual code of the function. This can improve performance by reducing function call overhead, but it also increases the size of the binary file. To make a function inline, you can use the keyword "inline" before the function name, like this:
inline int getValue(MyClass &obj)
{
return obj.value;
}
To learn more about inline function from the link below
brainly.in/question/8702988
To learn more about friend function from the link below
brainly.in/question/1674585
#SPJ3