C++ Programming

Friend Function And Friend Class In C++ With Examples

Friend Function:

Friend Function –The private and protected members of any class cannot be accessed from outside the class. Sometime, it may require to access private and protected members.

A Friend function is basically a non-member function of the class. A friend function is used to for accessing the private and other protected members of the class from outside of the class. A friend function can be used in basic and  in the most advanced programs.

A function is declared to be a friend of a class by using the keyword “friend” before the name of the function in function prototype. When a friend function is defined outside the class, no resolution operator (::) or name of class is mentioned along with the function header.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Write a program that explains the concept of friend function:

#include <iostream>

using namespace std;
class abc
{
    
    friend void xy(abc &, int, int);
    private:
    int a,b;
    public:
    void print(void)
    {
        cout<<"sum = "<<a+b<<endl;
        
    }
};

int main()
{
    
    abc obj;
    xy(obj, 3, 6);
    obj.print();
    
}

void xy(abc &c, int x, int y)
{
    
    c.a=x;
    c.b=y;
}

Friend Function

In this program, the function “xy” is a friend function. Even though the prototype of this function is given in the definition of class “abc” but it is not a member function of this class. Only the friendship is created with the class just to access its private and protected members.

The friend function is defined outside the class like an ordinary function without the scope resolution operator (::). The function “xy” is called in the main() function without the reference of class object but the address of class object “obj” is passed as argument to the function.

In definition of function “xy”, the private data members a & b of class “abc” are accessed (i.e. values are assigned to ‘a’ and ‘b’) with the reference of address of class object ‘c’.



Write a program by using classes A and B that initializes the data to private data member ‘a’ of class A and private data member ‘b’ of class B using constructors:

#include <iostream>

using namespace std;
class B;
class A
{
    
    
    private:
    int a;
    public:
    friend int sum(A, B);
    A(int x)
    {
        a=x;
    }
};
class B
{
    private:
    int b;
    public:
    friend int sum(A, B);
    B(int y)
    {
        b=y;
    }
    
};
int main()
{
    A aa(5);
    B bb(5);
    cout<<"sum = "<<sum(aa, bb);
    
}

int sum(A x, B y)
{
    
    return (x.a + y.b);
}

Friend Function

In this program, the function “sum” is declared as friend function in both classes (such as “friend int sum(A, B);”. This statement tells the compiler that the function is friend function of classes A & B.

The prototype of class B is also specified before the class A (such as “class B;”). It is because, class B is referenced in the prototype of friend function “sum” in class A (and also in class B). we know that is a class cannot be referenced before it has been declared. So the prototype of class B is necessary before using its name as a reference. Otherwise syntax error will be arise.

In the main() function, two object ‘aa’ and ‘bb’ are declared. The ‘aa’ and ‘bb’ are the objects of classes A and B respectively. These object are passed to the friend function “sum” to calculate the sum of private data members ‘a’ and ‘b’ of classes A and B respectively. The values are assigned to data members ‘a’ and ‘b’ by constructors of the classes.


Friend Class:

A class can also be declared as friend of another class. A type of class that allows for accessing the private and protected members of a particular class is called friend class. the keyword ‘friend’ is used before the class name to make it the friend of another class.

For example, to declare class b as friend of class a, the following statement is written in the definition of class a.

friend class B;


write a program that explains the concept of friend class:

#include <iostream>

using namespace std;
class A
{
    private:
        float m;
    protected:
        float n;
    public:
        void assign(float x, float y)
        {
            m=x;
            n=y;
            
        }    
        friend class B;
};

class B
{
    public:
    float sum(A obj)
    {
        return obj.m + obj.n;
        
    }
    
};
int main()
{
  A aa;
  B bb;
  float a,b;
  cout<<"enter first value= ";
  cin>>a;
  cout<<"enter second value= ";
  cin>>b;
  aa.assign(a,b);
  cout<<"sum of two values = "<<bb.sum(aa);
    
}

Friend Function

in this program, two classes A and B are declared. In the definition of class A class B is declared as friend class A. so the members of class B can access the private and protected members of class A.

the class A contains two data members (one private and second protected ) and one member function. The member function contains the statements to assign values to data members ‘m’ and ‘n’.

the class B contains one member function ‘sum’. It has one argument of object type of class A. the member function ‘sum’ accesses the data members ‘m’ and ‘n’ through object ‘obj’ of class A and computes the sum of values. The result is returned to the calling function.

In the main() function, two object ‘aa’ and ‘bb’ are declared of class A and class B respectively. The data values in data members ‘m’ and ‘n’ of class A are assigned through member function ‘assign()’ of class A. the member function of class B is called for execution by passing object ‘aa’. The object ‘aa’ is passed by values and assigns to the object ‘obj’ used as argument in members of class A are used for calculating the sum of values of data members of class A

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button