Ticker

6/recent/ticker-posts

PROGRAM FOR ILLUSTRATING THE USE OF FRIEND FUNCTION:

#include< iostream.h> 

class account1; 

class account2

{

        private:

            int balance; 

        public:

            account2( ) { balance=567; } 

            void showacc2( )

            {

                cout<<”balanceinaccount2 is:”<<balance<<endl; 

                friend int transfer (account2 &acc2, account1 &acc1,int amount);

            }

};

class acount1

        private:

            int balance; 

        public:

            account1 ( ) { balance=345; }

        void showacc1 ( )

        

            cout<<”balance in account1 :”<<balance<<endl; 

        

        friend int transfer (account2 &acc2, account1 &acc1 ,int amount);

};

int transfer ( account2 &acc2, account1 & acc1, int amount) 

{

        if(amount <=accl . bvalance)

        {

                acc2. balance + = amount; 

                acc1 .balance - = amount;

        }

        else

            return(0);

}

int main()

{

        account1 aa;

        account2 bb;

        cout << “balance in the accounts before transfer:” ;

        aa . showacc1( );

        bb . showacc2( );

        cout << “amt transferred from account1 to account2 is:”;

        cout<<transfer ( bb,aa,100)<<endl;

        cout<< “ balance in the accounts after the transfer:”;

        aa . showacc 1 ( );

        bb. showacc 2( );

         return(0);

}

output:

balance in the accounts before transfer

balance in account 1 is 345

balance in account2 is 567 

and transferred from account! to account2 is 100

balance in account 1 is 245

balance in account2 is 667

ALSO SEARCH:

"friend function program in C++"

"example of friend function"

"use of friend function in C++"

"friend function in C++"

"what is the syntax of friend function"

"characteristics of friend function in C++"