Skip to content

Latest commit

 

History

History

Friendship

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Friendship

When we define classes, they can only access their own private members, and do not have access to private members of other classes. However, we may want to allow a class to access private members of another class, we can use the friend keyword to allow this.

class Room {
  private:
    int Length;
    int Width;
    
    // the class House now has access to private members of Room
    friend class House;
    
}

Example: friend class

We demonstrate the use of a friend class in the example below.

#include <iostream>
using namespace std;

class Room {
  private:
    int Length;
    int Width;
    
    // the class House now has access to private members of Room
    friend class House;

  public:
    Room(int len, int wid){
      Length = len;
      Width  = wid;
    }
    
};

class House {
  public: 
    int Area;

    House(Room room){

      // accessing private values
      Area = room.Length * room.Width;
    }


};

int main() {

  Room room(10,7);
  House home(room);
  
  cout << home.Area << "\n";

  return 0;
}
// 70

From the above example, the class House is able to access private data variables of Length and Width for the class Room. This is because House was declared as a friend of Room (friend class House;).

Example: friend function

If we only wanted to give a particular function in one class access to private members of another class, then we can define that specific class function as a friend. The example below illustrates this.

#include <iostream>
using namespace std;

// forward declaration
class Room; 

class House {
  public: 
    int Area;

    void calculateArea(Room);

};

class Room {
  private:
    int Length;
    int Width;

    // the class House now has access to private members of Room
    // friend class House;
    friend void House::calculateArea(Room room);

  public:
    Room(int len, int wid){
      Length = len;
      Width  = wid;
    }

    
};

void House::calculateArea(Room room){
  // accessing private values
  Area = room.Length * room.Width;
}

int main() {

  Room room(10,7);
  
  House home;
  home.calculateArea(room);

  cout << home.Area << "\n";

  return 0;
}
// 70

In the above example, we specify the class function friend void House::calculateArea(Room room) to have access to private members of the class Room.

  • Note that we perform a forward declaration with class Room;, otherwise the compiler will not know what Room is yet when processing the class House which references Room.
  • We also define the function House::calculateArea(Room room) after the class House for the same reason; the compiler will get confused as to what the function is referencing since it hasn't finished compiling the class Room yet.

Example: global friend function

We can also declare a friend function that is globally accessible. In the earlier example House::calculateArea(Room room) is a function that belongs to the class House because we declared that membership with :: notation. If we didn't use that notation, then the calculateArea(Room room) function would be a global function. The example below demonstrates the use of a global friend function.

#include <iostream>
using namespace std;

// forward declaration
class Room; 

class House {
  public: 
    int Area;

    void calculateArea(Room);

};

class Room {
  private:
    int Length;
    int Width;

    // the class House now has access to private members of Room
    // friend class House;
    friend void House::calculateArea(Room room);
    friend int calculateArea(Room room);

  public:
    Room(int len, int wid){
      Length = len;
      Width  = wid;
    }

    
};

void House::calculateArea(Room room){
  // accessing private values
  Area = room.Length * room.Width;
}

// global friend function
int calculateArea(Room room){
  // accessing private values
  int Area = room.Length * room.Width;
  return Area;
}

int main() {

  Room room(10,7);
  House home;
  
  // friend class function
  home.calculateArea(room);
  cout << home.Area << "\n";

  // global friend function
  int Area = calculateArea(room);
  cout << Area << "\n";

  return 0;
}
// 70
// 70

In the above example, we've defined a global function int calculateArea(Room room) {}, which can access private members of class Room through the following declaration: friend int calculateArea(Room room);

References: