All Exams Test series for 1 year @ ₹349 only
Question

Which one of the following is correct, when a class grants friend status to another class ?

The correct answer is
All member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship.

Understanding C++ Friend Class Declaration

In C++, the $friend$ keyword allows a class or function to access the private and protected members of another class. When one class (let's call it Class A) declares another class (Class B) as its $friend$, it grants specific access privileges.

Friendship Access Rules

The declaration of friendship is asymmetric. When Class A declares Class B as a friend:

  • Class B's member functions can access the private and protected members of Class A.
  • This access is granted to all member functions of Class B.
  • Class A's members do not automatically gain access to Class B's members.
  • Friendship is not transitive (if A is a friend of B, and B is a friend of C, A is not automatically a friend of C).
  • Friendship is not reciprocal; Class B must explicitly declare Class A as a friend if it needs similar access.

Analyzing the Options

  • Option 1: Incorrect. The member functions of the class granting friendship (Class A) cannot access the members of the friend class (Class B) just because of the friendship declaration.
  • Option 2: Correct. This accurately describes the relationship. The class *granted* friendship (Class B) gains access to the members of the class that *granted* the friendship (Class A).
  • Option 3: Incorrect. Friendship must be explicitly declared and is not automatically reciprocal.
  • Option 4: Incorrect. The concept of friend classes is a valid and important feature in C++.

Therefore, the correct statement is that all member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship.

Was this answer helpful?

Important Questions from OOPs concept

  1. Let A be the base class in C++ and B be the derived class from A with protected inheritance. Which of the following statement is false for class B?

  2. Which of the following statements are true regarding C++?

    (a) Overloading a function gives the capability to an existing operator to operate on other data types.

    (b) Inheritance in object-oriented programming provides support to reusability.

    (c) When object of a derived class is defined, first the constructor of derived class is executed then constructor of a base class is executed.

    (d) Overloading is a type of polymorphism.

    Choose the correct option from those given below:
  3. Match List I with List II

    List IList II
    (Programming Paradigm)(Characteristic)
    A.ImperativeI.Declarative, clausal representation, theorem proving
    B.Object‐orientedII.Side‐effect free, declarative, expression evaluation
    C.LogicIII. Imperative, abstract data type
    D.FunctionalIV.Command‐based, procedural

    Choose the correct answer from the options given below :

  4. A member function can always access the data in _______, (in C++).

  5. Which of the following is not correct for virtual function in C++?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App