

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference Between Friend Function and Friend Class
In this post, we will understand the difference between Friend function and Friend class.
Friend Function
It is usually used with operator overloading operation.
It is used with the ‘friend’ keyword.
It helps give a non-member function the access to the private members of the class.
It has to be declared before it is used.
It is used to access private and protected members of the class.
It can be a global function or a function in another class.
Example
class Node { private: int val; Node* next; // Other members of Node Class // friend int LinkedList::search(); // Only search method of linkedList // can be used to access the internal members };
Friend Class
It is a class that is used with ‘friend’ keyword.
It is not necessary to declare it before using it.
A friend class is used when a class is created as an inherited class from another base class.
It is used to access private and protected members of the class.
Example
class Node { private: int val; Node* next; /* Other members of Class */ // The class can access private members of //Node friend class LinkedList; };
- Related Questions & Answers
- Friend class and function in C++
- Friend Pairing Problem
- PHP equivalent of friend or internal
- How can I make the balance between my best friend and lover?
- What are the qualities of a best friend?
- When should you use 'friend' in C++?
- What is the C# equivalent of C++ friend keyword?
- Google Talk - An Old Friend Now Rested In Peace
- Difference Between Structure and Class
- Difference between class alv and function alv in SAP ABAP?
- What is the difference between class and typeof function in R?
- Difference between abstract class and interface
- Difference between Function and Procedure
- Difference between String class and StringBuffer class in Java
- What qualities you will search in a person while making him a friend?
Advertisements