Online Tutorials & Training Materials | STechies.com
Register Login
Advertisement

Friend Function and Class in C++

|| 0

Friend Function and Class in C++
Stechies

Friend Class in C++

If a class is declared as the friend of another class, then the class can access the private and protected data members of the other class. Such a class is called a friend class. To access the protected and private member, the objects of a class has to be passed in the member functions of the friend class. However, this friendship is not mutual. If class Student is a friend of class Teacher, then Teacher does not become a friend of the Student class.

Friend Class Programming Example in C++

#include <iostream>
using namespace std;

class myclass1
{
    friend class myclass2; //myclass2 is a friend of myclass1
    int x;
    public:
        myclass1(int a){
            x = a;
        }
};
class myclass2
{
    public:
        void showData(myclass1 obj)
        {
            cout<<"Value for x is : "<<obj.x;
        }
};
int main(){
    myclass1 obj1(10);
    myclass2 obj2;
    obj2.showData(obj1);
    return 0;
}

OUTPUT:

Value for x is: 10

What is Friend Function in C++

A friend function is a function which has the ability to access the private and protected members of a class but is defined outside the scope of the class. The function prototypes are provided inside the class scope but they are not considered member functions of the class. This function can be a global function or the method of another class.

A friend can be function, member function, function template, class & class template.

We have to use a keyword friend through this keyword ‘friend’ compiler come to that this function is a friend of defined class inside the class.

Characteristics of Friend Function

The characteristics of a friend function are:

  • It can access the private and protected members of a class.
  • It is defined outside the scope of the class.
  • It can be a global function or the method of a different class.
  • It has to be declared along with the friend keyword inside the class.
  • The keyword is not used during the function definition.
  • Objects of the class cannot call a friend function as it is defined outside the scope of the class.
  • Any access specifier can be used with a friend function, and it does not affect the function.
  • Class objects are passed on as arguments with the friend function.   

Why we use friend function in C++?

  • When we need to access private data of a class, for example, if a class for say class D is a friend of class F  then we can call private and protected data of class D into class F.

Note: But class D cannot access private and protected data of class F

  • Friend function can also be used to compare multiple objects of different classes
  • Friend functions are useful if we want to use all the object an array at the same time
  • In order to print an object of any custom class, we are required to define operator<< on it and for that operator<< requires access to the private and protected members of the custom class to print it out. Therefore we need to have a friend class for our custom class.

How to declare friend function in the class

class <class_name>
{........ arguments
	friend return type <function name> 
					(contents)
	........
}

Friend Function in C++ Program Example

This program calculates the amount of tax on salary using Friend function in C++

#include <iostream>
#include <conio.h>

using namespace std;
class Caltx {
      string name;
   	int salary;
public:
   void Saly() {
      cout << "nEnter the Name whose salary calculation to be done:";
      cin >> name;
      cout << "nEnter Salary of the same Person:";
      cin >> salary;
   }
   void printDetails() {
      cout << "nName : " << name << ", Salary : " << salary << endl;
   }
   friend int calculateTax(Caltx Caltx);
};
int calculateTax(Caltx Caltx) {
   int tax = (Caltx.salary / 100) * 5;
   return tax;
}
int main() {
   Caltx mobj;
   int tax = 0;
   cout << "Friend Function Example Program In C++";

   mobj.Saly();
   mobj.printDetails();
   tax = calculateTax(mobj);
   cout << "Calculated Tax 5% : " << tax;
   getch();
   return 0;
}

Output

Friend Function Example Program In C++
Enter the Name whose salary calculation to be done: Sourav

Enter Salary of the same Person:1000
nName: Sourav, Salary : 1000
Calculated Tax 5% : 50

...Program finished with exit code 0
Press Enter to exit console.

 


Advertisement
Advertisement
Advertisement