Function Overloading in C++
15 questions · 12 min · Medium
Test your knowledge of friend functions in C++, including friend declarations, private and protected member access, syntax, classes, and common use cases.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
A. A function that can access private and protected members of a class
A friend function is not a member of the class, but it is granted access to the class's private and protected members.
A. friend
The friend keyword is used inside a class declaration to grant a function or another class access to private and protected members.
A. Inside the class declaration
A friend function is declared inside the class using the friend keyword, although its definition is normally outside the class.
A. No
A friend function is a non-member function that has been granted special access to the class.
A. Private and protected
Friend functions can directly access private and protected members of the class that grants friendship.
A. friend void display();
The friend keyword followed by the function declaration correctly declares a friend function inside a class.
A. Like a regular non-member function
Because a friend function is not a member function, it is normally called like an ordinary function rather than through an object using the dot operator.
A. Yes, the same function can be declared as a friend of multiple classes
A non-member function can be declared as a friend by multiple classes, allowing it to access the private and protected members of each class.
A. Friendship is not automatically inherited
Friendship is granted explicitly and is not automatically inherited by derived classes.
A. No
If class A declares class B as a friend, that does not automatically make A a friend of B.
A. Yes, a member function of another class can be declared as a friend
C++ allows a specific member function of another class to be declared as a friend.
A. Allowing closely related external functions to access private data
Friend functions are useful when an external function needs controlled access to a class's private or protected data.
A. operator<<
The stream insertion operator is commonly implemented as a friend function so it can access private data while supporting expressions such as std::cout << object.
A. It does not have a this pointer
A friend function is not a member function, so it does not have the implicit this pointer.
A. It grants specific non-member functions or classes access to private and protected members
The friend keyword provides selective access to private and protected members without making the function or class a member of the granting class.