C++ Friend Class & Function

Friend class is special feature of c++. they are applicable to access private and protected function of another class.

/*
  Example to friend class in c++.
*/
#include<iostream>
using namespace std;

class keyboard{
  private:
    //data member
    int keys;
  public :
    //constructor of class keyboard
    keyboard(){
      //set default value
      keys=104;
    }
  //Declare mouse  as friend of class keyboard  
  friend class mouse ;
};

class mouse  
{
  public:
    void display(keyboard obj){
      //access private data member of keyboard
      cout<<"Keyboard keys : "<<obj.keys;
    }
};
int main(){
  /*------
    Create object of keyboard class
    Call constructor and initialize value.
  */
  keyboard key_obj; 

  /*
    Create object of mouse class.
  */
  mouse touchpad;
  //call data member and pass the object of keyboard class
  touchpad.display(key_obj);

}
Output
Keyboard keys : 104

In this example observe that class mouse is a friend of Keyword class that means mouse class are capable to utilize the private and protected data member in keyword class. Passing the object of keyword class to mouse class member function. and help of this object we can access those object specific private and protected data member value.

If we are remove friend class mouse inside a keyword class then compiler produce following error. see example.

/*
  Example to friend class in c++.
*/
#include<iostream>
using namespace std;

class keyboard{
  private:
    //data member
    int keys;
  public :
    //constructor of class keyboard
    keyboard(){
      //set default value
      keys=104;
    }
  //Declare mouse  as friend of class keyboard  
  //friend class mouse ; //remove this line
};

class mouse  
{
  public:
    void display(keyboard obj){
      //access private data member of keyboard
      cout<<"Keyboard keys : "<<obj.keys;
    }
};
int main(){
  /*------
    Create object of keyboard class
    Call constructor and initialize value.
  */
  keyboard key_obj; 

  /*
    Create object of mouse class.
  */
  mouse touchpad;
  //call data member and pass the object of keyboard class
  touchpad.display(key_obj);

}
Error
error: 'int keyboard::keys' is private within this context
    cout<<"keyboard keys : "<<obj.keys;

Because cannot access private and protected data member and function using of object. clear that friend class is provided this special feature to access private and protected data using of object.

Important point about friend class

1) We can declare friend class to within of any access specifiers. and it will produce same result. see the example.

#include <iostream>
using namespace std;
class Programming{
  private:
    int language; 
    //defined friend class to private section
  friend class coder; 
  public:
    Programming(){
      language=1000;
    }
};
class coder{
public:
  void make_code(Programming p1){
       cout<<"language :"<<p1.language;
  }
};
int main(){
  Programming p1;
  coder c1;
  c1.make_code(p1);
}
Output
language :1000

2) Friendship are not mutual. for above example class 'coder' is friend of class 'programming' but programming class are not automatically friend of class 'coder'.

3) friend class are break the restriction and access control. so they are only used to special cases.

Friend Function

Help of friend function we can access the private and protected member of a class. Compiler are understand this function when declared function with started on friend keyword.

class class_name{
  //access specifiers
  friend return_type  function_name(parameter);
};
/*
  friend : keyword 
  return_type:  return type
  parameter: same as arguments
*/

We can declare friend function in any access specifiers. and definition are no need to use friend keyword.

class class_name{
  //access specifiers
  friend return_type  function_name(parameters);
};
return_type function_name(parameters){
  //code
}
View Example
#include <iostream>
using namespace std;
class Mobile{
  
    private:
    //private data member
    int message;
    //declared friend function
  friend void print(Mobile);
    public:
      //constructor
      Mobile(int value){
        message=value;
      }

};
//defined friend function
void print(Mobile obj){
  cout<<"Total message :"<<obj.message;
}
int main(){
  Mobile info(90);
  print(info); //call function
}
Output
Total message :90

In this example using of friend function utilized private data of Mobile class.

© 2019, regularcodes.com, All rights reserved