Friend function in C++ | C++ Friend class | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Thursday, July 30, 2020

Friend function in C++ | C++ Friend class | My CS Tutorial

Friend function in C++ | Friend class in c++ | C++ Friend function and friend class | Example of friend function | Example of friend class | My CS Tutorial

Friend function and friend class in C++


Friend Function


Similar to friend class, this function can access the private and protected members of another class. A global function can also be declared as friend.

If a function is defined as a friend function then, the private and protected data of a class can be accessed using the function.

The complier knows a given function is a friend function by the use of the keyword friend.
A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

For accessing the data, the declaration of a friend function should be made inside the body of the class (can be anywhere inside class either in private or public section) starting with keyword friend.


Declaration of friend function in C++


class class_name
{
    ... .. ...
    friend return_type function_name(argument/s);
    ... .. ...
}

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 className
{
    ... .. ...
    friend return_type functionName(argument/s);
    ... .. ...
}

return_type functionName(argument/s)
{
    ... .. ...
    // Private and protected data of className can be accessed from
    // this function because it is a friend function of className.
    ... .. ...
}

Friend Function Example


#include <iostream>
using namespace std;
class XYZ {
private:
   int num=100;
   char ch='Z';
public:
   friend void disp(XYZ obj);
};
//Global Function
void disp(XYZ obj){
   cout<<obj.num<<endl;
   cout<<obj.ch<<endl;
}
int main() {
   XYZ obj;
   disp(obj);
   return 0;
}


A class cannot access the private members of other class. Similarly a class that doesn’t inherit another class cannot access its protected members.

Friend Class


A friend class is a class that can access the private and protected members of a class in which it is declared as friend. This is needed when we want to allow a particular class to access the private and protected members of a class.


friend Class in C++ Programming


Similarly, like a friend function, a class can also be made a friend of another class using keyword friend. For example:

... .. ...
class B;
class A
{
   // class B is a friend class of class A
   friend class B;
   ... .. ...
}

class B
{
   ... .. ...
}
When a class is made a friend class, all the member functions of that class becomes friend functions.

In this program, all member functions of class B will be friend functions of class A. Thus, any member function of class B can access the private and protected data of class A. But, member functions of class A cannot access the data of class B.

Friend function in C++ | Friend class in c++ | C++ Friend function and friend class | Example of friend function | Example of friend class | My CS Tutorial
_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment