What is the main purpose of using friend functions in C++? 

  • To increase the execution speed of programs 
  • To enhance object-oriented principles 
  • To achieve data encapsulation and abstraction 
  • To provide access to class's private and protected members from outside the class
While C++ uses classes and objects to encapsulate data and behavior, there are cases where you might want an external function (not belonging to the class) to access the private or protected members of the class. This is where friend functions come into play. They provide an exception to the usual encapsulation rules.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *