Friend Functions

Friend functions are the functions which can access the private members of the class. We have to write the prototype of friend function inside the class of which it is the friend.

Properties of Friend Functions

  • Friend functions are declared inside the class.
  • They can access the private and protected data members of class of which they are friends.
  • They can be declared inside the public or private part of the class.
  • They are not the members of the class and we do not need object to call them.
  • Unlike methods/member functions which can access data members directly, friend functions cannot access data members directly. There is a need of dot operator, like obj.dataMemberName

Friend Classes

Friend class is also the same concept. Friend class is a class which can access the private member of some other class. But this relationship is not reversible. For example, if class1 is the friend of class2, then class1 can access private and protected members of class2 but class2 can not access private or protected members of class1.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

What is the return type of a function that doesn't return any value?

10 XP~2 min
Exercise 2 of 2Medium

A function that calls itself is called a ___ function.

15 XP~3 min