Friend Class in C++:-
● A friend class can access private and protected members of other
classes in which it is declared as a friend.
● It is sometimes useful to allow a particular class to access private and
protected members of other classes.
● We can declare a friend class in C++ by using the friend keyword.
●
Syntax:
friend class class_name; // declared in the base class
Declaration of friend class:
class class_name
{
friend class friend_class;// declaring friend class
};
class friend_class
{
};
PROGRAM EXAMPLE :
//friend class
#include<iostream>
using namespace std;
class one
{
private:
int a;
protected:
int b;
public:
void setv()
{
a=20;
b=60;
}
friend class two;
};
class two
{
public:
void display(one x)
{
cout<<"Value of Private variable:-"<<x.a<<endl;
cout<<"Value of Protected variable:-"<<x.b;
}
};
int main()
{
one o;
two t;
o.setv();
t.display(o);
}
OUTPUT:-
Value of Private variable:-20
Value of Protected variable:-60
Friend Function in C++:-
Like a friend class, a friend function can be granted special access to private
and protected members of a class in C++. They are the non-member
functions that can access and manipulate the private and protected members
of the class for they are declared as friends.
A friend function can be:
1. A global function
2. A member function of another class
1. Global Function as Friend Function :-
We can declare any global function as a friend function.
Syntax:
friend return_type function_name (arguments); // for a global
function
PROGRAM EXAMPLE :
//friend-function class
#include<iostream>
using namespace std;
class one
{
private:
int a;
protected:
int b;
public:
void setv()
{
a=20;
b=60;
}
friend void fun(one &x);
};
void fun(one &x)
{
cout<<"Value of Private variable:-"<<x.a<<endl;
cout<<"Value of Protected variable:-"<<x.b;
};
int main()
{
one o;
o.setv();
fun(o);
}
OUTPUT:-
Value of Private variable:-20
Value of Protected variable:-60
2. Member Function of Another Class as Friend Function :-
We can also declare a member function of another class as a friend function
in C++.
Syntax:
friend class_name::function_name (arguments); // for a
memreturn_type ber function of another class
PROGRAM EXAMPLE :
//function of another class
#include<iostream>
using namespace std;
class one;
class two
{
public:
void display(one &x);
};
class one
{
private:
int a;
protected:
int b;
public:
void setv()
{
a=20;
b=60;
}
friend void two::display(one &);
};
void two::display(one &x)
{
cout<<"Value of Private variable:-"<<x.a<<endl;
cout<<"Value of Protected variable:-"<<x.b;
}
int main()
{
one o;
two t;
o.setv();
t.display(o);
}
OUTPUT:-
Value of Private variable:-20
Value of Protected variable:-60
Characteristics of Friend Function in C++
● The function is not in the ‘scope’ of the class to which it has been
declared a friend.
● Friend functionality is not restricted to only one class
● Friend functions can be a member of a class or a function that is
declared outside the scope of class.
● It cannot be invoked using the object as it is not in the scope of
that class.
● We can invoke it like any normal function of the class.
● Friend functions have objects as arguments.
● It cannot access the member names directly and has to use dot
membership operator and use an object name with the member
name.
● We can declare it either in the ‘public’ or the ‘private’ part.
● These are some of the friend functions in C++ characteristics