C++ Tutorial - friend

CoderSource.net
C++ Tutorial - friend
Rating:

Date: 5/1/2004 12:00:00 AM

   Any data which is declared private inside a class is not accessible from outside the class. A function which is not a member or an external class can never access such private data. But there may be some cases, where a programmer will need need access to the private data from non-member functions and external classes. C++ offers some exceptions in such cases.

   A class can allow non-member functions and other classes to access its own private data, by making them as friends. This part of C++ tutorial essentially gives two important points.

  • Once a non-member function is declared as a friend, it can access the private data of the class
  • similarly when a class is declared as a friend, the friend class can have access to the private data of the class which made this a friend
   Let's see a sample in this C++ tutorial for each of the above cases.

C++ Tutorial - Friend function sample:

    #include <iostream.h>
    //Declaration of the function to be made as friend for the C++ Tutorial sample
    int AddToFriend(int x);
    class CPP_Tutorial
    {
       int private_data;
       friend int AddToFriend(int x);
    public:
       CPP_Tutorial()
      {
          private_data = 5;
       }
    };
    int AddToFriend(int x)
    {
       CPP_Tutorial var1;
       return var1.private_data + x;
    }
    int main()
    {
       cout << "Added Result for this C++ tutorial: "<< AddToFriend(4)<<endl;
    } 

    The output of the above C++ Tutorial sample will be
       Added Result for this C++ tutorial: 9

C++ tutorial - friend class:

    Declaration of a friend class is also similar. Only thing is a class definition is slightly different.

C++ Tutorial - Friend function:

    #include < iostream.h >
    class CPP_Tutorial
    {
       int private_data;
       friend class friendclass;
    public:
       CPP_Tutorial()
       {
          private_data = 5;
      }
    };
    class friendclass
    {
    public:
       int subtractfrom(int x)
       {
          CPP_Tutorial var2;
          return var2.private_data - x;
       }
    };
    int main()
    {
       friendclass var3;
       cout << "Added Result for this C++ tutorial: "<< var3.subtractfrom(2)<     }

   The output of the above C++ Tutorial sample will be
       Subtracted Result for this C++ tutorial: 3

   This is a good way out given by C++ to avoid restrictions on private variables. But this should be used with caution though. If all the functions and classes are declared as friends, then the concept of encapsulation and data security will go for a toss.

   That is why the concept of friend functions and classes should be used with proper judgment.

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us