You are on page 1of 9

C++ Class Members and friends

Pi19404
November 22, 2013

Contents

Contents
C++ Class Members and friends
0.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 0.2 Friend Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3
3 3

2|9

C++ Class Members and friends

C++ Class Members and friends


0.1 Introduction
This article tells us how to grant access to its non public members by the use of friend mechanism.

0.2 Friend Mechanism


 A friend of a class X is a function or class that is not a member of X, but is granted the same access to X as the members of X  Functions declared with the friend specifier in a class member list are called friend functions of that class  Classes declared with the friend specifier in the member list of another class are called friend classes of that class.  A class Y must be defined before any member of Y can be declared a friend of another class.  We can define a function as a friend or an entire class as friend. The function can be a member of a class or declared in the global namespace. The function needs to be declared before it can be used as friend.  If a friend function is a member of another class,a scope resolution operator needs to be used ,for example Y::print(X& x);  however a function can be defined provided function is unqualified and has namespace scope (function is not a member of function of another class or declared in another workspace) and class is non local.

#include <iostream> #include <typeinfo> #include <stdlib.h> using namespace std;

3|9

C++ Class Members and friends

class F; class X; void print2(); void print1(X x1); class Y { public: void print(X& x); }; class X { private: int a; protected: int c; friend void friend void friend void { cout << }

Y::print(X& x); print1(X x); print2() "defining a friend function in the class " <<endl;

public: X() : a(1), c(2) { } }; void print1(X x1) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x1.a << endl; cout << "c is " << x1.c << endl; } void Y::print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl;

int main() {

4|9

C++ Class Members and friends

X xobj; Y yobj; yobj.print(xobj); print1(xobj); print2();

 If a class F is a friend of class A,then every member function and static data member definition of class F has access to class A.  For using the class as a friend,in the declaration must be elaborated class name , for example friend class F  A friend class can be defined after it is declared as friend in other class  A friend cannot be declared with a storage class specifier.  A class cannot be defined within a friend declaration,

#include <iostream> #include <typeinfo> #include <stdlib.h> using namespace std; class F; class X; class X { private: int a; protected: int c; friend class F; public: X() : a(1), c(2) { } }; class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl;

5|9

C++ Class Members and friends

} };

cout << "c is " << x.c << endl;

int main() { X xobj; F fobj; fobj.print(xobj); }


 The scope of a friend class name is the first nonclass enclosing scope In the example the scope of the friend class name is class Z1 and not class

class Z; void print3(Z &z); class Z { public:

class Z1 { private : int z1; public: Z1() : z1(4){} friend void print3(Z &z) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; //cout << "a is " << z.z2 << endl; //including this line will give comp cout << "b is " << z.z11.z1 << endl; } }; Z1 z11; private : int z2; public: Z() : z2(3){} };
 Friends of a base class are inherited by any classes derived from that base class The functionality that the friend of the base

6|9

C++ Class Members and friends class are inherited by the derived by the base class may be compiler dependent.The g++ compiler on Linux supports this feature,while many online references suggest this inheritance does not hold.

class X { private: int a; protected: int c; friend class F; public: X() : a(1), c(2) { } }; class X1 : private X { }; class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl; } void print(X1 & x1) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x1.a << endl; cout << "c is " << x1.c << endl; } }; int main() { X xobj; fobj.print(xobj); fobj.print(x1obj); }
 If a class is derived from a friend class ,then it is also a friend of

7|9

C++ Class Members and friends the original base class.This feature may also be compiler dependent.The g++ compiler on Linux supports this feature,while many online references suggest this inheritance does not hold.

class X { private: int a; protected: int c; friend class F; public: X() : a(1), c(2) { } }; class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl; } } class G : public F { } void main() { X xobj; G gobj; gobj.print(X); }
 Friendships are not transitive if A is a friend of B,and C is a friend of A,it does not imply that C is a friend of B unless explicitly specified.

class X { private: int a; protected: int c; friend class F;

8|9

C++ Class Members and friends

public: X() : a(1), c(2) { } }; class F { public: void print(X& x) { cout << "In member cout << "a is " << cout << "c is " << } } class H { public: void print(X& x) { cout << "In member cout << "a is " << cout << "c is " << } };

function " << __PRETTY_FUNCTION__ <<endl; x.a << endl; x.c << endl;

function " << __PRETTY_FUNCTION__ <<endl; x.a << endl; x.c << endl;

class F { public: void print(X& x) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x.a << endl; cout << "c is " << x.c << endl; } void print(X1 & x1) { cout << "In member function " << __PRETTY_FUNCTION__ <<endl; cout << "a is " << x1.a << endl; cout << "c is " << x1.c << endl; } friend class H; }; int main() { X xobj; H hobj; // hobj.print(xobj); // this line will give compilation error }

9|9

You might also like