Object Oriented Programming C++ Lecture No 9

Outline

  • Friends
  • The friend functions of a class
  • friend Classes

 

Friends

  • Normally, we declare class data as private.
  • The concepts of encapsulation and data hiding dictate that non-member functions should not be able to access an object’s private data.
  • The policy is, if you are not a member, you can’t get it.
  • However, sometimes we need to relax this rigid policy.
  • Sometimes, it is useful to treat certain, selected functions as “honorary members” of the class, and allow them to access private members of a class object, just as though they were members of the class.
  • Such functions are called friends of the class.
  • A friend can access any of the members of a class object, regardless of their access specification.
  • There are two situations that we need to consider:
  • An individual function can be specified as a friend of a class
  • Or a whole class can be specified as a friend of another class

–All the member functions of the friend class have the same access privileges as a normal member of the class.

 

The friend Functions of a Class

  • A function that is not a member of a class but nonetheless can access all its members is called a friend function of that class.
  • To make a function a friend of a class, we need to declare it as such within the class definition, using the keyword friend.
  • A friend function of a class can be a global function, or it can be a member of another class.
  • However, a function cannot be the friend of a class of which it is a member.
  • Access specifier do not apply to the friends of a class.

 

Example: friend Function

A friend function has the right to access all members (private or public) of the class.

 

 

 Box Example: friend Function

Declare the function boxSurface as a friend of the Box class by writing the function prototype using the keyword friend, within the class definition.

friend function is not a member of the class, so access specifiers do not apply to it.

boxSurface is a global function, and its definition follows that of main. We could put it in box.cpp if we wanted.

Placing it in the driver file helps to indicate that it is a global function.

A friend function is not a member of the class, therefore, the data members cannot be referenced by their names alone (this pointer).

 

 

The friend Functions of a Class

  • Within a friend function, data members need to be qualified by the object name, in exactly the same way as they might in an ordinary function accessing public members of a class.
  • A friend function is the same as an ordinary function, except that it can access all the members of a class without restriction.

 

Example: friend Function

  • We want friFunc() to have access to both classes’ private data members, so we make it a friend function.
  • It is declared with the friend keyword in both the classes.
  • A class can’t be referred to until it has been declared.
  • Class Alpha and Beta are referred to in the declaration of the function friFunc(), so we need to declare these two classes:

–class Beta; //declaration in alpha.h

–class Alpha; //declaration in beta.h

 

 

The Need for friend Functions

  • The need for friend functions in practice is limited
  • Useful in situations where a function needs access to the internals (especially the private members) of two different kinds of objects, where making the function a friend of both classes makes that possible.

 

friend Classes

  • We can also declare a whole class to be a friend of another class.
  • All the member functions of the friend class will have unrestricted access to the members of the class of which it has been declare a friend.
  • Example:

–We have defined a class, Carton, and we want to allow the member functions of the Carton class to have access to the members of the Box class.

–We need to include a statement within the Box class definition that declares Carton to be its friend.

  • Friendship is not a reciprocal (mutual) arrangement.

–Functions in the Carton class can now access all the members of the Box class.

–Functions in the Box class have no access to the private members of the Carton class.

  • Friendship amongst classes is not transitive either.

–Just because class A is a friend of class B, and class B is a friend of class C, it doesn’t follow that class A is a friend of class C.

 

Example: friend Class

In this example, A is not a friend of B. A can not access private members of B.

The Need for friend Classes

  • A typical use for a friend class is where the functioning of one class is highly intertwined with that of another class.

Leave a Reply

Your email address will not be published. Required fields are marked *

twelve − 7 =