Ticker

6/recent/ticker-posts

FRIEND FUNCTIONS IN C++ PROGRAMMING

FRIEND FUNCTIONS

1) A non member function cannot have an access to the private data of a class. However there could be situation where we would like two classes to share a particular function. 

2) In such situation, C++ allows the common function to be made friendly with both the classes, thereby allowing the function to have access to the private data of these classes such a function need not be a member of any of these classes. 

3) The syntax for friend function is 

class ABC 
{
 ======= 
public: 
======= 
friend void function1(void); 

The function is declared with friend keyword. But while defining friend function. It does not use either keyword friend or :: operator.

A friend function, although not a member function, has full access right to the private member of the class. 

4) A friend, function has following characteristics. 

i) It is not in the scope of the class to which it has been declared as friend. 

ii) A friend function cannot be called using the object of that class. If can be invoked like a normal function without help of any object. 

iii) It cannot access the member variables directly & has to use an object name dot membership operator with member name. 

iv) It can be declared either in the public or the private part of a class without affecting its meaning. 

v) Usually, it has the object as arguments.

5) Ex- 

# include 

class sample 

        int a, b; 
    public: 
        void setvalue() 
        
                a = 25; b = 40; 
        
        friend float mean (sample s); 
}; 
float mean(sample s) 

        return float(s.a + s.b)/ 2.0; 

main ( ) 

        sample x ; 
        x.setvalue(); 
        cout << " Mean Value = "; 
        mean(x); 

output -

        Mean value = 32.5

6) Member function of one class can be friend functions of another class. In such cases they are defined using the scope resolution operator. 

EX - class x 
{ -------
int fun(); // member function of x 
}; 
class y 
{--------- 
---------- 
friend int x : : fun() 
}; --------- 

7) We can declare all the member functions of one class as the friend functions of another class by declaring that class as a friend class. 

Ex - class z 
        { -------- 
            -------- 
        friend class x ; 
        }

8) A friend function work as a bridge between the classes. Ex 

#include 
class ABC ; //forward declaration 
class xyz 

        int x; 
    public: 
        void setvalue(int i) { x = i; } 
        friend void max (xyz, ABC); 
}; 
class ABC 

        int a; 
    public a: 
        void setvalue(int i) { a =i;} 
        friend void max(xyz, ABC); 
}; 
void max(xyz m, ABC n) // definition of friend. 

        if(m. x > = n. a) 
                cout << m .x; 
        else 
                cout << n.a; 

main ( ) 

        ABC K; 
        K. setvalue (20);
        XYZ l; l 
        setvalue (40);
        max (l , k); 

output is - 40 

9) A friend function can be called by reference. In this local copies of the object are not made. Instead, a pointer to the address of the object is passed and the called function directly works on the actual object used in the call. 

Ex-following program shows how to use a common friend function to exchange private value of two classes. 

# include 
class c2; 
class c1 

        int value1; 
    public: 
        void indata (int a) {value1 = a;} 
        void display (void) {cout << value1;} 
        friend void exchange (c1 &, c2 &); 
};
class c2 

        int value2; 
    public: 
        void indata (int a) {value 2 = a;} 
        void display (void) {cout << value2;} 
        friend void exchange (C1 &, C2 &) 
}; 
void exchange (cl &x, c2, &y) 
{
            int temp = x.value1; 
            x.value 1= y.value2; 
            y.value 2 = temp 

main() 

            c1 co1; 
            c2 co2; 
            co1. indata(10); 
            co2. indata(20) 
            cout << "Values before exchange"; 
            co1. display() 
            co2.display(); 
            exchange(co1, co2); 
            cout << "values after exchange"; 
            co1.display();
            co2.display(); 

output of the program 
        values before exchange 
        10 
        20 
        values after exchange 
        20 
        10

ALSO SEARCH:

"friend function in C++"

"friend class in C++ example"

"use of friend function in C++""

"example of friend function"

"characteristics of friend function in C++"

"advantages of friend function in C++"

"friend function syntax"