Friend Function in C++
What is Friend Function in C++:
Whenever data variables are declared in private category of a class these variables are restricted from accessing non member functions.
Because the private data values can be neither read nor written by non member functions.
If user is trying to access these data members then compiler will display the error message “ inaccessible data type”.
To access these data members by non member functions is to change a private data to public group.
When private or protected data member is changed to public category then it it violates the concepts of data hiding and data encapsulation.
To overcome this problem a friend function concept of object oriented programming is used.
Friend function is a special mechanism of OOP for letting non member functions access private data.
A friend function is declared or defined within scope of class.
The keyword friend is used to declared friend function. Friend keyword tells compiler that it is not a member function of class.
Syntax to Declare friend Function:
class class_name
{
private:
//data members
public:
//member functions
firend return_type function_name (parameter); //firend functions
};
Example:
class ABC
{
private:
int a,b;
public:
void getdata();
friend void display(ABC a); //friend functions
};
void display(ABC a)
{
//methods
}
In the above program segment display() function is a non member function because it is declared with friend keyword. In the parenthesis of friend function class object is used as an argument. So the private data of class is accessed by this non member through the object of class.
Following program demonstrate friend function in C ++
#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a,b;
public:
void display()
{
a=100;
b=200;
}
friend float mean(sample abc); //friend function
}; //end of class definition
float mean(sample abc) //friend function defined
{
return float(abc.a+abc.b)/2.0;
}
int main()
{
sample obj; //class object created
obj.display();
cout<<”The mean value is”<<endl;
cout<<mean(obj)<<endl;
return 0;
}
Characteristics of friend Function:
It is not in the scope of the class to which it has been declared as a friend
It cannot be called by the object of that class
It can be invoked without the help of any object
It cannot access member names directly so to use object name and dot operator with each member name.( e.g. abc.a)
It has the objects as an argument
It can be declared either private or public without affecting its meaning.
A program to access the private data of a class by non member function through friend where the friend function is declared in the location of public category.
#include<iostream.h>
class sample
{
private:
int a;
public:
void getdata();
void friend display(class sample ); //friend function
};
void sample::getdata()
{
cout<<”Enter value of a”<<endl;
cin>>a;
}
void display(sample s)
{
cout<<”Entered value is”<<endl;
cout<<s.a;
}
void main()
{
sample obj;
obj.getdata();
cout<<”Accessing private data member by non member”<<endl;
cout<<”function”<<endl;
display(obj);
}
A program to access the private data of a class by non member function through friend where the friend function is declared in the location of private category.
#include<iostream.h>
class sample
{
private:
int a;
void friend display(class sample ); //friend function
public:
void getdata();
};
void sample::getdata()
{
cout<<”Enter value of a”<<endl;
cin>>a;
}
void display(sample s)
{
cout<<”Entered value is”<<endl;
cout<<s.a;
}
void main()
{
sample obj;
obj.getdata();
cout<<”Accessing private data member by non member”<<endl;
cout<<”function”<<endl;
display(obj);
}
0 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏