Pages

Sunday, 9 September 2012

Learn C++

Module 8 :Friend Functions

Introduction

Up to now you have learnt functions, classes, member functions. In this module you will learn about friend functions and its special features in C++.

Learning Objective


By the end of this module, you will able to:
  • Explain and use friend functions in C++ programs
  • Implement software objects as friends to one another

Friend

The concepts of encapsulation and data hiding dictate that nonmember functions should not be able to access an object’s private or protected data. The policy is, if you’re not a member, you can’t get in. However, there are situations where such rigid discrimination leads to considerable inconvenience. In order to access the non-public members of a class, C++ provides the friend facility. The accessibility of class members in various forms is shown in the below figure

See: https://docs.google.com/open?id=0B_cy2j6cIeqTSXotVGtwN19Jd3c

Friend Functions

Imagine that you want a function to operate on objects of two different classes. Perhaps the function will take objects of the two classes as arguments, and operate on their private data. In this situation there’s nothing like a friend function. Here’s a simple example, FRIEND that shows how friend functions can act as a bridge between two classes: <source lang = cpp> / friend.cpp // friend functions include <iostream> using namespace std; class beta; //needed for frifunc declaration //////////////////////////////////////////////////////////////// class alpha { private: int data; public: alpha() : data(3) { }
   //no-arg constructor 
friend int frifunc(alpha, beta); //friend function }; //////////////////////////////////////////////////////////////// class beta { private: int data; public: beta() : data(7) { } //no-arg constructor friend int frifunc(alpha, beta); //friend function }; //////////////////////////////////////////////////////////////// int frifunc(alpha a, beta b) //function definition { return( a.data + b.data ); }; //-------------------------------------------------------------- int main() { alpha aa; beta bb; cout << frifunc(aa, bb) << endl; //call the function return 0; } </source> In this program, the two classes are alpha and beta. The constructors in these classes initialize their single data items to fixed values (3 in alpha and 7 in beta). We want the function frifunc() to have access to both of these private data members, so we make it a friend function. It’s declared with the friend keyword in both classes: friend int frifunc(alpha, beta); This declaration can be placed anywhere in the class; it doesn’t matter whether it goes in the public or the private section. An object of each class is passed as an argument to the function frifunc(), and it accesses the private data member of both classes through these arguments. The function doesn’t do much: It adds the data items and returns the sum. The main() program calls this function and prints the result. A minor point: Remember that a class can’t be referred to until it has been declared. Class beta is referred to in the declaration of the function frifunc() in class alpha, so beta must be declared before alpha. Hence the declaration class beta; at the beginning of the program.

Friend Classes

The member functions of a class can all be made friends at the same time when you make the entire class a friend. The program FRICLASS shows how this looks.
// friclass.cpp 
include <iostream> 
using namespace std; //////////////////////////////////////////////////////////////// 
class alpha { 
 private: int data1; 
 public: alpha() : data1(99) { } //constructor 
 friend class beta; //beta is a friend class 
}; //////////////////////////////////////////////////////////////// 
class beta { //all member functions can access public, private data
 public: 
 private alpha data 
 void func1(alpha a) { cout << “\ndata1=” << a.data1; } 
 void func2(alpha a) { cout << “\ndata1=” << a.data1; } 
}; //////////////////////////////////////////////////////////////// 
int main() { 
 alpha a;
 beta b;
 b.func1(a);
 b.func2(a);
 cout << endl;
 return 0;
} 
In class alpha the entire class beta is proclaimed a friend. Now all the member functions of beta can access the private data of alpha (in this program, the single data item data1). Note that in the friend declaration we specify that beta is a class using the class keyword:
friend class beta; 
We could have also declared beta to be a class before the alpha class specifier, as in previous examples class beta; and then, within alpha, referred to beta without the class keyword:
friend beta; 

No comments:

Post a Comment