Friend Functions and Friend Classes in C++ with Examples

0

Friend Functions in C++:-

It is a type of a function which has the permission to access the private and protected members of a class in which this function is declared by a Friend keyword. Normally private and protected members of a class could not be accessed from outside the class but in some cases the program may have to access the private or protected variables to perform the functionality. In that case we declare a function in a class with a friend keyword whose private members are required to access by the program. Therefore, this function will be able to access these members and after performing the functionality the desired results could be produced.

The below example will completely illustrate the use of friend functions in C++ programming.

For Example:-


#include<iostream.h>
#include<conio.h>

class B;
class A
{
private:
int a;
public:
A()
{
a=25;
}
friend void show(A,B);
};
class B
{
private:
int b;
public:
B()
{
b=35;
}
friend void show(A,B);
};

void show(A x, B y)
{
int r;
r= x.a + y.b;
cout<<”The value of class A object =”<<x.a<<endl;
cout<<”The value of class B object =”<<y.b<<endl;
cout<<”The sum of both values =”<<r<<endl;
}
main()
{
A obj1;
B obj2;
show(obj1, obj2);
getch();
}

Explanation:-

In the above program, first of all we will declare a class B, we are just declaring it, so when we write a signatures of a friend function, we will use both classes A and B. In a class we will declare one integer type private data member and initialize it to a value 25 in a constructor of A. Then we will define a function with the friend keyword which contains parameters as both classes. Then we will write the definition of class B. This class will also declares a one private data member and initialize it to a value 35 in a constructor of class B. Then it will declare a function with a friend keyword whose signatures are both classes. Then we will write the definition of a friend function show, in which we will show the value of the private data members of both classes. Then in main function we will simple create the objects of these classes and call the friend function Show to display their respective fields.

Friend classes in C++:-

These are the special type of classes whose all of the member functions are allowed to access the private and protected data members of a particular class is known as friend class. Normally private and protected members could not be accessed from outside the class but in some situations the program has to access them in order to produce the desired results. In such situations the friend classes are used, the use of friend classes allows a class to access these members of another class. The syntax of declaring a friend class is not very tough, simply if a class is declared in another class with a friend keyword it will become the friend of this class.

The below example will completely illustrate the use of a friend classes.

For Example:-


#include<iostream.h>
#include<conio.h>

class A
{
private:
int a,b;
public:
A()
{
a=10;
b=20;
}
friend class B;
};
class B
{
public:
B()
void showA(A obj)
{
cout<<”The value of a: ”<<obj.a<<endl;
}
void showB(A obj)
{
cout<<”The value of b: ”<<obj.b<<endl;
}
};

main()
{
A x;
B y;
y.showA(x);
y.showB(x);
getch();
}

Explanation:-

[yop_poll id=”17″] In the above example we have declared to classes A and B. In A we simply declared two integer type private data members and initialized them in a constructor. In this class at the end we have declared a class B with a friend keyword which makes B the friend of class A. Therefore all the member functions of B will be able to access the private data members of A. Then in class B we simply created a function showA which have a permission to access the private members of class A.Then we will display the values of the attributes of the object of class A.

If you like my work and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

5/5 - (1 vote)

Author

  • royal52

    I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

2 Comments
  1. Thanks a LOT!!! (royal25) this program was the most helpful among the other websites i checked. The program above was very easy and simple to understand. Thanks Again!!!

Leave a Reply