Jump to content

Friend class

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nalawadec (talk | contribs) at 04:55, 16 October 2011. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

A friend class in C++, can access the "private" and "protected" members of the class in which it is declared as a friend.On declaration of friend class all member function of the friend class become friend of the class friend of the class in which the friend class was declared.

Declaration

Classes are declared as friends within the definition of the class to whom access is to be given; this prevents a class from giving itself access to another's protected members, which enforces encapsulation. The friend class has the same level of access irrespective of whether the friend declaration appears in either the public, protected or private sections of the class definition. Friend status is granted by using the class keyword:

friend class ClassName;

Example

#include <iostream>

class B
{
    // B declares A as a friend...
    friend class A;

private:
    void privatePrint()
    {
        std::cout << "hello, world" << std::endl;
    }
};

class A
{
public:
    A()
    {
        B b;
        // ... and A now has access to B's private members
        b.privatePrint();
    }
};

int main()
{
    A a;
    return 0;
}

Advantages Of Using Friend Class

  • Sometimes private datamembers are needed to be accessed and used by two different classes simultaneously. At that time,at that time we can use friend function which can access to private data members of both the classes.For that purpose,it should be declared as 'friend' in both the classes.They should not be member of any one of the class.[1]
  • It provides additional functionality which is kept outside the class.
  • It provides functions that need data which is not normally used by the class.
  • It allows sharing private class information by a non member function.[2]

Scope

The class name which is introduced in the friend class declaration its scope is not in class granting friendship and also is not the member of the class granting access.If the name of the friend class is declared before the friend class declaration then the compiler searches for the class with the same name as the friend class at the scope of friend declaration.If the name of the friend class is same as that of the enclosing class which is nested then the nested class is a friend of enclosing class.

Example

class X{
     class Y{
          friend class Z;
     }
}

is equivalent to

class Z
class X{
     class Y{
          friend class Z;
     };
};

Features

  • Friendships are not corresponded – If class A is a friend of class B, class B is not automatically a friend of class A.
  • Friendships are not transitive – If class A is a friend of class B, and class B is a friend of class C, class A is not automatically a friend of class C.
  • Friendships are not inherited – A friend of class Base is not automatically a friend of class Derived and vica verca; equally if Base is a friend of another class, Derived is not automatically a friend and vica verca.
  • Access due to friendship is inherited – A friend of Base can access the restricted members of Derived that were inherited from Base. Note though that a friend of Derived only has access to members inherited from Base to which Derived has access itself, e.g. if Derived inherits publicly from Base, Derived only has access to the protected (and public) members inherited from Base, not the private members, so neither does a friend.

References

  • An Introduction to Object-Oriented Programming in C++: with Applications in Computer Graphics, by Graham M. Seed

See also