Lost Password?

  #1 (permalink)  
Old 03-17-2008, 11:26 PM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default using friend functions

a friend function is used to allow access to its private data from a non-member function.this is achieved by declaring the non-member a friend.
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 t which it is a friend.
example ::

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

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.

Code:
    #include < iostream.h >
    class Tutorial
    {
       int private_data;
       friend class friendclass;
    public: 
       Tutorial()
       {
          private_data = 5; 
      }
    };
    class friendclass
    {
    public: 
       int subtractfrom(int x)
       { 
          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 will be overlooked.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
virtual functions Chinmoy C Tutorials 0 03-17-2008 11:03 PM
Console API Functions dargueta C and C++ 14 10-20-2007 03:38 PM
Virtual Functions ???? Patrick C and C++ 1 10-07-2007 09:54 AM
Flex, bison multifunction calculator annatsos C and C++ 1 01-04-2007 06:00 AM
Get and Set Functions Chan C# Programming 3 08-25-2006 11:44 AM


All times are GMT -5. The time now is 12:05 PM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 66%

Ads