C++ Friend Function And Friend Class Example

C++ Friend class and functionFollow the series “C++ By Examples” for all the C++ tutorials.

One basic fundamental of C++ is encapsulation. It means bundling data and interface. Major idea behind encapsulation is data hiding. Only providing interfaces to access data outside the class implementation. This is done through access specifiers public, protected and private. You must be knowing that private members of a class can’t be accessed outside that class. And the biggest evaders of private access are friend function and friend class. Yes the fundamental notion of data hiding is being broken wide open by friend specifier.

Friend function and class can access any private members of a class with whom the friendship is developed through the friend keyword. To declare any class or function friend of say class A, they need to be specified with friend keyword prefixed inside class A. One more thing, friend will have access to everything irrespective of in which section it’s specified – be it public, protected or private.

In our example, DemoFriend class is friend of DemoBase class. DemoBase has iValue_ private member. But in DemoFriend’s function setDBValue, we can access the iValue_ variable. And also if you just make setDBValue function friend of DemoBase class, it still can access iValue_. That’s all friends!

One funny quote about C++ friend keyword, I read somewhere,

“C++ : Where friends have access to your private members.” -Gavin Russell Baker

Header file 008_1_use_friend.h

/**
* File Name     : 008_1_use_friend.h
* Functionality : This is demo for friend class, friend function.
                  DemoFriend class is for friend class/function example.

* Copyright (C) 2012 Divyang Patel

* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.

* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>

class DemoBase;

class DemoFriend
{
        public:
                DemoFriend():ifValue_(0){}
                int getValue() throw();
                void setValue( int iVal ) throw();
                void setDBValue( DemoBase& dbobj, int iVal ) throw();

        private:
                int ifValue_;
};

class DemoBase
{
        public:
                DemoBase():iValue_(0){}
                virtual int getValue() throw();
                virtual void setValue( int iVal ) throw();

        private:
                int iValue_;
                friend class DemoFriend;

                // If you just make setDBValue function friend, then only that function
                // can access private members. Try it yourself. But first comment out
                // above code which makes DemoFriend friend of this class.

                //friend void DemoFriend::setDBValue( DemoBase& dbobj, int iVal );
};

Source file 008_1_use_friend.cpp

/**
* File Name     : 008_1_use_friend.cpp
* Functionality : See 008_1_use_friend.h for the functionality
                  description

* Copyright (C) 2012 Divyang Patel

* See <http://www.gnu.org/licenses/>.
*/

#include "008_1_use_friend.h"

using namespace std;

#ifndef DEBUG
#define DEBUG 0
#endif
#define PRINT_DEBUG(def_var) if(DEBUG) cout << def_var << endl;

int DemoBase::getValue() throw()
{
        return iValue_;
}

void DemoBase::setValue( int iVal ) throw()
{
        iValue_ = iVal;
}

int DemoFriend::getValue() throw()
{
        return ifValue_;
}

void DemoFriend::setValue( int iVal ) throw()
{
        ifValue_ = iVal;
}

// As DemoFriend is friend of DemoBase class, we can directly access
// DemoBase private members without get/set methods.

void DemoFriend::setDBValue( DemoBase& dbobj, int iVal ) throw()
{
        // Here iValue_ is private member of DemoBase class
        // and still we can access it inside DemoFriend class
        dbobj.iValue_ = iVal;
}

int main()
{
        DemoBase dbobj;
        DemoFriend dfobj;

        PRINT_DEBUG("***************************************************");
        PRINT_DEBUG("   Test case : Friend Class/Function           ");
        PRINT_DEBUG("***************************************************");

        dbobj.setValue(5);

        cout << "dbobj iValue_ = " << dbobj.getValue() << endl;

        // See the definition of setDBValue function. We are directly
        // accessing DemoBase private members as DemoFriend is friend of
        // DemoBase class.

        dfobj.setDBValue(dbobj, 10);

        cout << "dbobj iValue_ = " << dbobj.getValue() << endl;

        return 0;
}

You can use Makefile posted in this article.

Related Posts

Custom Exception Class in c++

Writing Custom Exception Class In C++

C++ Static Keyword Example

Static Method And Variables In C++

Array Of Objects Having Virtual Functions In C++

CPP-By-Examples-square

C++ By Examples

Leave a Reply









Ad

Like Us!