C++ friendship: which of the following statements about the friend keyword is incorrect?

Difficulty: Easy

Correct Answer: Friend keyword can be used on main().

Explanation:


Introduction / Context:
The friend declaration in C++ grants access to private and protected members to specified functions or classes. It is a powerful but narrow tool: you can friend a function or a class, and you can place the friend declaration in any access section (public, protected, or private) without changing its effect. This item asks you to spot the statement that mischaracterizes friendship.


Given Data / Assumptions:

  • Friendship can be granted to non-member functions and to whole classes.
  • Placing a friend declaration in any section does not alter its effect.
  • The program entry function main is not a typical target for friendship in real-world code.


Concept / Approach:

Options A, B, and C all reflect valid uses: you may friend another class, and you may friend a specific function; the declaration can appear under public or private. Option D suggests “using friend on main()” which is, at best, ill-formed or meaningless in practical C++ design: the entry point requires a unique global signature; granting it friendship provides no sensible encapsulation mechanism and is not a recognized or advisable construct for access control questions like this. Therefore D is the incorrect statement in the intended sense of the question bank.


Step-by-Step Solution:

Confirm that friend can target classes (A) → valid. Confirm that friend can target functions regardless of placement (B, C) → valid. Recognize that “friend main()” is not a meaningful or standard design → mark as incorrect.


Verification / Alternative check:

Practical codebases never rely on granting friendship to main(). Try to “friend main” to gain special access and note it brings no coherent advantage; compilers or style rules will reject this pattern.


Why Other Options Are Wrong (i.e., not the incorrect statement):

A: Legal and common for tight class collaboration.

B/C: Friend declarations are unaffected by the access section where they appear.


Common Pitfalls:

  • Thinking friendship is inherited or transitive—it is neither.
  • Attempting to friend a particular object instance—friendship is to functions or classes only.


Final Answer:

Friend keyword can be used on main().

More Questions from Objects and Classes

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion