N
TruthVerse News

What is the use of friend keyword in C++?

Author

David Richardson

Updated on March 15, 2026

What is the use of friend keyword in C++?

friend is a keyword in C++ that is used to share the information of a class that was previously hidden. For example, the private members of a class are hidden from every other class and cannot be modified except through getters or setters.

In this regard, why do we use friend keyword?

A friend class can have access to the data members and functions of another class in which it is declared as a friend. They are used in situations where we want a certain class to have access to another class's private and protected members.

Beside above, should I use friend in C++?

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

Then, what does friend class mean in C++?

A friend class is a class that can access the private and protected members of a class in which it is declared as friend. This is needed when we want to allow a particular class to access the private and protected members of a class.

What is the use of friend keyword in C ++ discuss its advantages and disadvantages?

In C++, friend means to give permission to a class or function. The non-member function has to grant an access to update or access the class. The advantage of encapsulation and data hiding is that a non-member function of the class cannot access a member data of that class.

What is the advantage of friend function in C++?

Benefits of friend function

A friend function is used to access the non-public members of a class. It allows to generate more efficient code. It provides additional functionality which is not normally used by the class. It allows to share private class information by a non member function.

How do you declare a friend function?

Declaration of a friend function in C++

class class_name { friend data_type function_name(arguments/s); //syntax of friend function. }; In the above declaration, the keyword friend precedes the function. We can define the friend function anywhere in the program like a normal C++ function.

What is visibility mode?

Visibility mode is used in the inheritance of C++ to show or relate how base classes are viewed with respect to derived class. When one class gets inherited from another, visibility mode is used to inherit all the public and protected members of the base class.

Where does keyword friend should be placed?

Explanation: The keyword friend is placed only in the function declaration of the friend function and not in the function definition because it is used toaccess the member of a class.

Is a friend function a member function?

friend functions

A friend function is a function that is not a member of a class but has access to the class's private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges.

What is advantage of inheritance in C++?

The main advantages of inheritance are code reusability and readability. When child class inherits the properties and functionality of parent class, we need not to write the same code again in child class. This makes it easier to reuse the code, makes us write the less code and the code becomes much more readable.

What can a friend class do?

A friend class in C++ can access the private and protected members of the class in which it is declared as a friend. A significant use of a friend class is for a part of a data structure, represented by a class, to provide access to the main class representing that data structure.

Is a has a relationship C++?

C++ Aggregation (HAS-A Relationship)

In C++, aggregation is a process in which one class defines another class as any entity reference. It is another way to reuse the class. It is a form of association that represents HAS-A relationship.

What is the use of declaring a class as a friend of another?

Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example, a LinkedList class may be allowed to access private members of Node.

Are Friend classes bad?

At least friendship restricts the reduced encapsulation to the explicitly specified classes that need the extra access. The fact that the two classes are now tightly coupled need not be a bad thing, they can be considered a single unit of the overall design. No, not at all.

Is Friend inherited in C++?

In C++, friendship is not inherited. If a base class has a friend function, then the function doesn't become a friend of the derived class(es).

What is overriding in C++?

Function overriding in C++ is a feature that allows us to use a function in the child class that is already present in its parent class. Function overriding means creating a newer version of the parent class function in the child class.

What is protected in C++?

The protected keyword specifies access to class members in the member-list up to the next access specifier ( public or private ) or the end of the class definition. Class members declared as protected can be used only by the following: Direct privately derived classes that also have private access to protected members.

What does virtual do in C++?

Virtual Function in C++ A virtual function is a member function which is declared within a base class and is re-defined(Overriden) by a derived class. Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for function call.

What is the biggest demerit of friendship?

The biggest disadvantage of friendship is that not all friends are good people and if you are not able to segregate between good and bad friends then you can be in trouble as one bad friend is enough to spoil you as well as your future and thus making life difficult not only for you but also for your near and dear ones

Are C++ friends bad?

The most of OOP experts dislike friend function simply because they want the OOP part of C++ to behave like Smalltalk. But C++ is not Smalltalk, and they cannot even understand that friend don't break encapsulation, for the very simple reason that a function cannot be friend of your class without your class wants it.

Is friendship transitive C++?

The privileges of friendship aren't transitive. A friend of a friend isn't necessarily a friend. If class Fred declares class Wilma as a friend, and class Wilma declares class Betty as a friend, class Betty doesn't necessarily have any special access rights to Fred objects.

What is the purpose of destructor?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.

What are the characteristics of friend function in C++?

Characteristics of a Friend function:
  • The function is not in the scope of the class to which it has been declared as a friend.
  • It cannot be called using the object as it is not in the scope of that class.
  • It can be invoked like a normal function without using the object.

What are types of constructors?

Constructor Types
  • Default Constructor.
  • Parameterized Constructor.
  • Copy Constructor.
  • Static Constructor.
  • Private Constructor.

How do you create a friend function in C++?

A friend function in C++ is a function that is preceded by the keyword “friend”. When the function is declared as a friend, then it can access the private and protected data members of the class. A friend function is declared inside the class with a friend keyword preceding as shown below. class className{ ……

What is object initialization in C++?

Initialization provides an object's initial value. The object's type, scope, storage duration, and context determine whether and how it is initialized.

What are the advantages and disadvantages of friction?

Advantages of friction:
  • Friction enables us to walk freely.
  • It helps to support ladder against wall.
  • It becomes possible to transfer one form of energy to another.
  • Objects can be piled up without slipping.
  • Breaks of vehicles work due to friction.
  • It always resists the motion, so extra energy is required to overcome it.

What is the use of namespace?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

How do you access objects in the classroom?

Follow the class name with the member-access operator ( . ) and then the member name. You should always access a Shared member of the object directly through the class name. If you have already created an object from the class, you can alternatively access a Shared member through the object's variable.

What is scope operator in C++?

Scope resolution operator in C++

The :: (scope resolution) operator is used to get hidden names due to variable scopes so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by a particular declaration of an equivalent name during a block or class.