Friend Function in CPP
Generally, in the program of CPP and java, we cannot access the private and protected members from outside the class. That private and protected member should be accessible within that class only but still, if we are trying to access it will prompt an error so to access the private and protected members of one class from outside the class using the friend function and friend class. We don’t have the concept of the friend function in java but we have it in CPP.
Now, the question is why do we have a friend function in java? Because using the friend function
Anyone can access the private data of the class and here, it will break the security of the code.
By using the keyword friend compiler knows the given function is a friend function.
To access the data, the declaration of a friend function should be done inside the body of a class starting with the keyword friend and we can define that function at anywhere in the class like a normal function. C++ Training in Pune with SevenMentor will make your career a new height
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
Declaration syntax of friend function in C++
class class_name
{
friend data_type/return type function_name(argument/s); // syntax of friend function.
};
E.g
Class Login
{
friend void getCredentials(Login);
}
In the above declaration, the friend function is preceded by the keyword friend.
The function definition does not use either the friend keyword and scope resolution operator together because in this friend function is just a friend of the specified class, it is not a member of the class that’s why there is no need to define that friend function using scope resolution operator…
Characteristics of a Friend function:
1. The function is not a part of the class or we can say it is not in the scope of the class to which it has been declared as a friend.
2. It cannot be called using the object as it is not part of the class. Its just a friend of the class.
3. It can be call or invoked like a normal function without using the object.
4. It cannot access the member names directly and has to use an object name and dot operator with the member name. which we want to access.
5. Friend function can be declared either in the private or the public part of the class
//Program for friend function in C++
#include<iostream>
using namespace std;
class Login
{
private:
string uname;
string pass;
public:
Login( string uname,string pass)
{
this->uname = uname;
this->pass =pass;
}
friend void disp(Login lobj);
}; //Login class ends here
void disp(Login lobj) //add(int a,int b)
{
cout<<lobj.uname<<endl;
cout<<lobj.pass;
}
main()
{
Login l1(“panel123@sm.com”,”12345**”);
disp(l1);
}
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
In the above code, disp() is a friend function of class Login so here disp() can access the private members uname and pass to outside that login class. And to access that private members in disp() function we are creating one reference variable obj in the parameter list of disp() function
//Program for friend class(to add two numbers)
#include<iostream>
using namespace std;
class B;
class A
{
private:
int a;
public:
A()
{
a=7;
}
friend class B;
};
class B
{
private:
int b;
public:
B()
{
b=10;
}
public:
void add(A a1)
{
int c=b+a1.a;
cout<<“\nadd is = “<<c;
}
};
main()
{
A aobj;
B bobj;
bobj.add(aobj);
}
In the above code, we are performing the addition of 2 numbers in class B. The first number ‘a’ is private from class A that we are accessing in class B. As we are accessing a private member of class A in class B so we have to make class B a friend class of class A by using the friend keyword.
For Free, Demo classes Call: 8237077325
Registration Link: Click Here!
//Program that finds out the largest between 2 numbers using friend function and friend class
#include<iostream>
using namespace std;
class B ; //forward declaration
class A
{
private:
int n1;
public:
A( int n1)
{
this->n1=n1;
}
friend void large(A a,B b);
};
class B
{
private:
int n2;
public:
B(int n2)
{
this->n2=n2;
}
friend void large(A a,B b);
};
void large(A a,B b)
{
if(a.n1 > b.n2)
{
cout<<“n1 is large”;
}
else
{
cout<<“n2 is large..”;
}
}
main()
{
A a(434);
B b(6700);
large(a,b);
}
Author:-
Pooja Nandode-Bhavsar
Call the Trainer and Book your free demo class for now!!!
© Copyright 2020 | Sevenmentor Pvt Ltd.