C++ friend class

Friend Class in C++
friend class is a class that can access the private and protected(also public) members of a class in which it is declared as a friend. We need to declare the class as a friend of another class to allow or request another class to access the private and protected members of that class.

Friend Class in CPP

Example Of Function Class

In this example, we have two classes T4 and Tutorials. The T4 class has two private data members ch and num, this class declares Tutorials as a friend class. This means that Tutorials can access the private members of T4, the same has been demonstrated in the example where the function disp() of Tutorials class accesses the private members’ num and ch. In this example, we are passing an object as an argument to the function.

How to access the C++ friend class?

//C++ Friend class by T4Tutorials
#include <iostream>
using namespace std;
class T4 
{
private:
   char ch='G';
   int num = 9;
   
public:
   friend class Tutorials;
};
class Tutorials {
public:
   void disp(T4 obj2){
      cout<<obj2.ch<<endl;
      cout<<obj2.num<<endl;
   }
};
int main() {
   Tutorials obj1;
   T4 obj2;
   obj1.disp(obj2);
   return 0;
}
Output

G

9

Friend Function in C++
If we declare a function as a friend in a class, then this function can access the private and protected members of that class. You must know that a global function can also be declared as a  friend function of the class.

Syntax of friend function in C++

class class_name_T4Tutorials

{

friend return_type function_name(arguments);

}

Now, you can define the friend function as a normal function to access the data of the class. No friend keyword is used in the definition.

class class_name

{

friend return_type function_Name(argument/s);

}
return_type function_Name(arguments)
{

// Private and protected data of class can be accessed from this function
// because it is a friend function of class.

}

How to access the C++ friend function?

#include <iostream>
using namespace std;
class T4Tutorials {
private:
   char ch='G';
    int num=9;
public:
   friend void disp(T4Tutorials obj);
};
void disp(T4Tutorials obj){
   cout<<obj.ch<<endl;
   cout<<obj.num<<endl; 
}
int main() {
   T4Tutorials obj;
   disp(obj);
   return 0;
}
Output

G

9

Factorial using friend function in C++

How to find factorial using friend function in C++?

#include<iostream>
using namespace std;

class T4Tutorials {
private:
    int n;
public:
    void input() {
        cout << "Please Enter a Number :";
        cin >> n;
    }

    friend void factorial(T4Tutorials t);
};

void factorial(T4Tutorials t) {

    int f = 1, i;
    for (i = 1; i <= t.n; i++) {
        f = f*i;
    }
    cout << "\nThe factorial is :" << f;
}

int main() {

    T4Tutorials t;
    t.input();
    factorial(t);

    return 0;
}

Output

Please Enter a Number : 4

The factorial is : 24

Video Lecture

Examples of friend function in c++  | friend function in c++ pdf  | advantages of friend function in c++ | disadvantages of friend function in c++  | characteristics of friend function in c++  | friend function in c++ ppt  | why we use friend function in c++.

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.