C++ Friend Function - Quiz Explanation

The answers you selected are indicated below, along with text that explains the correct answers.
 
1. A friend function declaration:
  A. gives nonmember functions access to hidden class members
  B. is another term for an overloaded function within a class
  C. is any function created for you by the compiler (such as a default constructor)
  D. is any function outside the class with the same name as a function in the class
  The correct answer is A.
A friend function is not a member of the class, but has access as if it were.

2. A friend function declaration appears:
  A. in the header of the friend function outside the class
  B. inside the class declaration to which it is a friend
  C. as a separate declaration inside main()
  D. anywhere prior to the function invocation
  The correct answer is B.
The friend function definition appears elsewhere, but the declaration using the keyword friend must appear in the class. In this course, we place it in the public access section, but it can go anywhere.

3. How is a static member function similar to a friend function?
  A. A static member function is like a friend function that doesn't need to be public:.
  B. A friend function is similar to binary operator overloading.
  C. A friend function has an implicit this parameter .
  D. A friend function does not have direct access to the class’s 1) private: and 2) protected: parts.
  Answer A is correct.
B, C and D are false. A static member function is like a friend function with a funny name that doesn't need to be public:.
Static member functions and top-level friend functions are similar in that neither has an implicit this parameter, and both have direct access to the class's 1) private: and 2) protected: parts. Except for overloaded operators, most friend functions end up actually being static member functions, because static member functions have a scoped name (they do not pollute the global namespace) and they do not have to be public:
They can also be
  1. private: or
  2. protected:.

4. Which of the following is true about friend functions in C++?**
Please select the best answer.
  A. A friend function is inherited by derived classes.
  B. A friend function is a member of the class that declares it as a friend.
  C. A friend function can be declared in multiple classes.
  D. A friend function can access private and protected members of the class that declares it as a friend.
  Answer D is correct.
A, B and C are false.
Explanation of Question 4:
  • Option A: "A friend function is inherited by derived classes."(False)

    Friend functions are not members of a class, so they are not inherited. They exist outside the class and are granted special access to the private and protected members of the class that declares them as friends. Since they are not part of the class hierarchy, they do not participate in inheritance.

  • Option B: "A friend function is a member of the class that declares it as a friend."(False)

    A friend function is not a member of the class—it is an external function that is granted access to private and protected members of the class. Unlike member functions, friend functions do not require an instance of the class to be called and do not have an implicit this pointer.

  • Option C: "A friend function can be declared in multiple classes."(False)

    A friend function is declared inside a single class to grant it access to that specific class’s private and protected members. However, the same function can be declared as a friend in multiple classes, but each declaration must be explicitly made in each class separately. This is not an automatic property, making the statement misleading.

  • Option D: "A friend function can access private and protected members of the class that declares it as a friend."(True)

    This is the correct answer. The primary purpose of friend functions is to allow non-member functions (or other classes) to access the private and protected members of a class. This is useful for operator overloading, non-member utility functions, or when multiple classes need to interact closely.

Final Answer: ✅ **Option D is correct.**