• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Functions » What is Friend Functions in C++
Next →
← Prev

What is Friend Functions in C++

By Dinesh Thakur

In C++, a class encapsulates data and functions that operate on that data. Data members are usually made private so that they cannot be accessed from the class’s non-member functions. However, in certain situations, there is a need to access the private data members of a class by a function that is not the class member. It can be achieved by making the non-member function as a: friend function.

A friend function is a non-member function that has been granted access to all the private members of a class. To make a function a friend of a Class to access private members of the class, we declare the class’s function by prefixing its declaration with the keyword friend. Just like we allow our friends to share our private information, a function declared as a friend of a class will be able to access the private data of that class. The declaration of friend function can be placed anywhere in the class specification irrespective of access specifiers. The friend function definition is specified outside the class body and is not treated as part of the class.

The significant difference between a member function of a class and a friend function is that the member function is accessed through the object while a friend function requires objects to be passed by value or reference.

Since the friend function can access the private members of a class, it violates encapsulation rules. Therefore it should be used less frequently. One should have a strong justification for using the friend function in a program.

Note: There is no need to specify the keyword friend in the friend function definition.
#include<iostream.h>
#include<conio.h>
class demo {
  int x;
  public:
    demo(int xx)
    { x=xx; }
    friend void display(demo}; //Friend function declaration
};
void display(demo dd1)
  { cout<<dd1.x;}
int main() {
  clrscr();
  demo d1(5);
  display(d1);//friend function call
  return 0;
}

Explanation: In the above program, we have defined a class demo having a private data member x and a public constructor member function used to initialize data member x of object d1. Also, within the class demo, we declare the function display() as a class demo friend whose definition define outside the class. Since friend function display() is the class demo’s non-member function, it defines outside the class body without a scope resolution operator.
It can access the private member of the class demo. The statement,

 display(d1);

calls a friend function just like a normal function which then displays the value of data member x of the passed object d1 of a class demo. As display() is a non-member function of the class demo, it cannot invoke through an object d1 of the class demo. Hence, we need to pass the object d1 as an argument to access its private data member within the friend function definition.

The main objectives of using a friend function in a program are as follow.

• It acts as a bridge between two classes. To understand this, consider a situation where we want a function to access the private data members of two or more different classes. To solve this problem, we can define this function as a friend of two or more different classes, and hence in this sense, a friend function acts as a bridge between two classes.

• A friend function can be used to overload operators.

#include<iostream.h>
class yyy; //forward declaration of class
class xxx {
  private:
   int x;
  public:
   xxx(int xx)
   {x=xx;}
   friend void f1 (xxx,yyy); //Friend function declaration
};
class yyy {
  private:
    int y;
  public:
    yyy(int yy)
     { y=yy; }
     friend void f1(xxx,yyy);//Friend Function declaration
};
void f1(xxx objx,yyy objy) {
  cout<<"Difference = "<<objx.x-objy.y;
}
int main() {
  xxx ob1(10);
  yyy ob2(5);
  f1(ob1,ob2);//Friend Function call
  return 0;
}

Explanation: This program aims to calculate the difference of data members of the objects of two different classes. For this, we have defined two classes, xxx and yyy, having a data member x and y respectively, which initialized through their respective single parameter constructors. To compute the difference of the private data members of objects ob1 and ob2 of two classes xxx and yyy, respectively, we make a function f1(), a friend to both the classes. This function declared as a friend in both the classes as

friend void f1 (xxx,yyy);

This friend declaration has two arguments of class types xxx and yyy. The first declaration of the friend function in class xxx expects that the class yyy is already defined, but it is not. To refer to the name of the class yyy respectively as an argument without actually defining it, C++ provides a way known as forwarding Declaration. The statement class yyy; At the beginning of the program is the forward declaration of class yyy, which allows us to refer to class yyy without defining it.

You’ll also like:

  1. What is Functions? Explain Features of Functions,Types of Functions and Calling a Function
  2. Write A C++ Program Using A Friend to Overload ++ or – –
  3. Write a C++ program to Make op-, ++, = a friend; use reference.
  4. Write a C++ program for friend operator.
  5. Write a C++ program to Operator Overloading Using a Friend Function.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW