- C++ Programming Basics
- C++ Tutorial
- C++ Environment Setup
- C++ Character Set
- C++ Keywords
- C++ Identifiers
- C++ Constants
- C++ Punctuators
- C++ Program Structure
- C++ Basic Syntax
- C++ Comments
- C++ Basic Programs
- C++ Input Output Operator
- C++ Input Output Stream
- C++ Type & Variable
- C++ Data Types
- C++ Data Type Modifiers
- C++ Variables
- C++ Variable Types
- C++ Variable Scope
- C++ Storage Classes
- C++ Formatting Output
- C++ Operators
- C++ Operators
- C++ Type Conversion
- C++ Numbers
- C++ Assignment Operator
- C++ Shorthand
- C++ Flow of Control
- C++ Statements
- C++ Flow Control
- C++ Decision Making
- C++ if if-else if-else-if switch
- C++ Loops
- C++ for while do-while Loop
- C++ break continue goto
- C++ Standard Library
- C++ Standard Library
- C++ Header Files
- C++ Character String
- C++ Mathematical Functions
- C++ Functions
- C++ Functions
- C++ Function Types
- C++ Function Prototype
- C++ Function Call
- C++ Function Return
- C++ Friend Function
- C++ Scope Rules
- C++ Arrays & Strings
- C++ Arrays
- C++ One Dimensional Arrays
- C++ Two Dimensional Arrays
- C++ Strings
- C++ Data Structure
- C++ Data Structure
- C++ Access Structure Member
- C++ Nested Data Structure
- C++ Structure Array
- C++ Pass Structure to Function
- C++ typedef
- C++ #define
- C++ Programming Pointers
- C++ Pointers
- C++ Memory Map
- C++ Free Store
- C++ Declare Initialize Pointers
- C++ Dynamic Memory Allocation
- C++ Pointers & Arrays
- C++ Pointers & Const
- C++ Pointers & Functions
- C++ Pointers & Structures
- C++ Objects as Function Arguments
- C++ Pointers & Objects
- C++ References
- C++ File Handling
- C++ File Handling
- C++ File Streams
- C++ Data Files
- C++ Opening & Closing Files
- C++ Steps to Process Files
- C++ Change Stream Behaviour
- C++ Sequential I/O Operations
- C++ Detecting EOF
- C++ File Pointers Random Access
- C++ Binary Files Operations
- C++ Error Handling
- C++ Object Oriented
- C++ Object Oriented
- C++ Function Overloading
- C++ Classes & Objects
- C++ Constructors & Destructors
- C++ Inheritance
- C++ Encapsulation
- C++ Polymorphism
- C++ Data Abstraction
- C++ Interfaces
- C++ Programming Advance
- C++ Linked Lists
- C++ Stacks
- C++ Queues
- C++ Date Time
- C++ Preprocessors
- C++ Exception Handling
- C++ Namespaces
- C++ Dynamic Memory
- C++ Multithreading
- C++ Templates
- C++ Signal Handling
- C++ Web Programming
- C++ Programming Examples
- C++ Programming Examples
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Friend Function
In this tutorial, you will learn all about friend function in C++ with its example program and step by step explanation of the code.
What is Friend Function ?
A friend function is a function of the class that has the same access as member functions of the same class. But friend function is not a member function. If a keyword friend is placed before any function declared inside the class, then that function can be called as friend function.
Where Friend Function is Declared ?
Friend function is declared inside the class where member function of the same class gets declared. But friend function is not a member function of the class to which it is a friend (or where it is declared).
Use friend keyword to declare a function as friend function. Or precede the function name with this keyword. For example,
friend void myFun();
Friend function is defined outside the class (where it is declared). To define a friend function, friend keyword is not required. This keyword is required only while declaring the function inside the class.
Why friend Keyword is used ?
It is used to tell the compiler, that it is not a member function of the class. Rather it is a friend function. Because if you remove the keyword friend, then this function gets processed by compiler as member function of the class.
How to call Friend Function ?
Friend function has no object to call it. Or friend function has no any caller object. Because it is not a member function of the class where it is declared. And to define it, there is no any membership lebel gets attach while defining the function. Therefore, to call this function from main() function, just call it as normal function call.
Why to use Friend Function
In a big project, there are a lot of programmers who work for the same project. Therefore, using friend function, one programmer can access to the private data members of the class defined by another programmer.
For example, let's suppose a programmer named James who want to access the private data member of the class that is defined by other programmer named Ricky. So, Ricky declares a friend function inside the class defined by him and tells the name of this function to his friend James who work for the same project. Now James can use this function to access private data member of the class defined by Ricky.
Step by Step Explanation of Friend Function
Now let's take an example that multiplies any two number entered by user. There is a use of friend function in this program. Let's have a look at the program, later I'll explain each and everything goes inside the program in step by step manner.
#include<iostream> using namespace std; class CodesCracker { private: int m, n, res; public: void getData() { cout<<"Enter Two Numbers: "; cin>>m>>n; } void showResult() { cout<<"\nResult = "<<res; } friend void mulFun(CodesCracker &c); }; int main() { CodesCracker c; c.getData(); mulFun(c); c.showResult(); cout<<endl; return 0; } void mulFun(CodesCracker &c) { c.res = (c.m) * (c.n); }
This program was build and run under Code::Blocks IDE. The snapshot given below shows the sample run of the above program:
Now supply two numbers say 5 and 6. Press ENTER
key to see the output as shown in the
following snapshot:
Here is step by step explanation of previous program:
- Program flow always starts from main() function
- Therefore, in main() function, there is a statement
CodesCracker c;
- It tells the compiler, to create an object named c of the class CodesCracker
- But before creating an object, compiler checks for the class CodesCracker. That is, whether it is defined or not.
- Because it is defined, then an object named c gets created
- Using this object c, a member function getData() gets called
- Because c is the object declared for the class CodesCracker, therefore function getData() from this class only gets called
- This function asks from user to enter any two number and stores it in m and n variables
- This variable is declared as private data member of the same class
- Now a friend function mulFun() gets called with c as its parameter
- Here c is the object of class CodesCracker
- So Program checked whether this function is declared or not, inside the class of that object
- Because it is declared inside the same class with friend keyword
- So this function is a friend to that class
- Friend to the same class means, it can access private data of that class
- Because, it is already declared in the same class of that object passed as its parameter
- So program goes to function definition part
- In function definition, the statement c.res = (c.m) * (c.n); defines that, the value of m and n variable of the class to which the object c is declared, gets multiplied and stored in a variable res of the same class
- Now program flow again goes back to main() function
- And the statement c.showResult(); gets executed, where the value of res variable gets printed on output screen. Thats it.
« Previous Tutorial Next Tutorial »
Like/Share Us on Facebook 😋