In this article friend function in OOP we give the information about with a friend function, we can access a private member of a class while it is not a member of the class.
Friend Function in OOP:
As we know that the member declared in private of a class can be accessed only by the public members of that class i.e. private cannot be accessed from outside the class.
With a friend function, we can access a private member of a class while it is not a member of the class.
However, it is a function that tells the compiler that it is a friend of a class and can access private members of that class. Because it is not a member of any class, so we can declare it anywhere (private or public) in the program. A function is declared as friend function by using friend keyword-
Syntax:-
friend return-type function-name(class-name);
In the above syntax, an object of class -name is also declared in the function parameter.
C++ friend function declaration:-
Note, the class of which it is to be made friend is declared inside that class (anywhere in private or public) i.e. the declaration of friend function declaration is inside class while the definition is outside class, like-
class student
{
………
public:
……….
friend void show_record(student);
};
Defining friend function:-
When we define a friend function. So we also declare an object of that class in the parameter of friend function. With this object, friend can access both private and public members of function class-
How to call friend function in C++?
To call a friend -function, we do not use dot operator (. ) but class -object is there as parameter in it.
The syntax for calling a friend function is given below-
friend function_name(class-object);
Characteristics of Friend Function:-
- It is not in the scope of the class to which it has been declared as friend.
- Science it is not in the scope of the class, it cannot be called using the object of that class.
- It can be invoked like a normal function with the help of any object.
- Unlike member functions, it cannot access the member names directly and has to use an object name and dot membership operator with each member name.
- It can be declared either in the public or the private part of a class without affecting its meaning.
- Usually, it has the objects as arguments.
Example:
void main()
{
class-name class-obj1;
friend-function-name(class-obj1);
}
Remember, the object defined as parameter, class -obj1 is the main object of the class.
// Friend function Program in C++
#include<iostream.h>
#include<conio.h>
class stat
{
private:
int a; // data members
int b;
friend float mean(stat q); // friend function
public:
void getdata() // member function
{
cout<<“\n\n\n Enter the 2 numbers: “;
cin>>a>>b;
}
};
float mean(stat q) // friend function definition
{
return (float)(q.a+q.b)/2;
}
void main()
{
stat p; // object created
clrscr();
p.getdata(); // function call
cout<<“\n\n Mean Value: “<<mean(p);
getch();
}
OUTPUT:
Enter the 2 numbers: 10
5
Mean Value: 7.5
Friend Classes:
A friend classes are a classes that can access private and protected members of the class in which it is declared. This is useful when we want to access private and protected members of another class through a particular class.
The friend keyword is used to make a class a friend class.
Syntax:
class A
{
friend class B;
};
class B
{
};
When a class is declared as a friend class, then all the member functions of the friend class also become friend functions.
It is similar to a friend function, here the class takes the place of the friend function. In this, a class is a friend of another class or several classes.
Where many classes can access private members of each other class.
Here is an example of two classes “first” and “second”, where the second class accesses the private members of the first class-
In this, the first class is a friend of the second class, but the second class is not a friend of the first class, that is, only the first class can access the private member of the second class.
Here we declare two classes “first” and “second”, where we are accessing the member (int a) of the first class from the second class (obj1 of the first class).
Friend Function in OOP:
Friend class example in c++:-
#include<iostream.h>
#include<conio.h>
class first
{
int a;
friend class second; //second is declaration in private mode of first class
public:
void get_num(int x)
{
a = x;
}
}; // first class terminate
class second
{
int b;
public:
void get_num(int y)
{
b = y;
}
void get_sum( first obj1) // second class member defination
{
int sum;
sum = obj1.a + b; // accessing first class member using first class object f1
cout<<“\n Total: “<<sum;
}
}; // second class terminate
void main() // main function start here
{
int a,b;
clrscr();
first obj2; // first class object declare
second s; // second class object declare
cout<<“\n\n\n Enter two number: “;
cin>>a>>b;
obj2.get_num(a); // first class function call
s.get_num(b); // second class function call
s.get_sum(obj2); // friend function call
getch();
}
OUTPUT:
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining