Blog Archive

Computer Instructor C++ Friend Function & Friend class

 

 C++  Class:14

 Friend Function

     Friend Class

 

Friend Functions and Friend Class

Friend Function:

A friend function can access the private and protected data of a class. We declare a friend function using the friend keyword inside the body of the class.

class className {

    ... .. ...

    friend returnType functionName(arguments);

    ... .. ...

}


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 as shown in the example below:

 

Friend Function Example

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

Output:

100
Z

1. The Friend Functions are used to access the member variables of the class indirectly.

2. It is a not a member function of a class. It is     a friend function of the class.

3. We define the friend function outside the class.

4. it can be declared either in public or private.

5. It has object as an argument.

6. It can access the member variables with the use of the objects of that class.

 

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.

 

we can also use a friend Class in C++ using the friend keyword. For example,

class ClassB;

 

class ClassA {

   // ClassB is a friend class of ClassA

   friend class ClassB;

   ... .. ...

}

 

class ClassB {

   ... .. ...

}

When a class is declared a friend class, all the member functions of the friend class become friend functions.

Since ClassB is a friend class, we can access all members of ClassA from inside ClassB.

However, we cannot access members of ClassB from inside ClassA. It is because friend relation in C++ is only granted, not taken.

 

Friend Class Example

 

#include <iostream>

using namespace std;

class classA {

private:

   char ch='A';

   int num = 11;

public:

   /* This statement would make class classB

    * a friend class of classA, this means that

    * classB can access the private and protected

    * members of classA class.

    */

   friend class classB;

};

class classB {

public:

   void disp(classA objA){

      cout<<objA.ch<<endl;

      cout<<objA.num<<endl;

   }

};

int main() {

   classA objA;

   classB objB;

   objB.disp(objA);

   return 0;

}

 

Output:

A
11



 

 

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

PG -TRB COMPUTER INSTRUCTOR C plus plus Class : 4 switch do while

  C++ switch The C++ switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement in C++. sw...