0% found this document useful (0 votes)
2 views

Friend function and its characteristics

A friend function in C++ allows a non-member function to access private data of a class. It is declared within the class using the 'friend' keyword and can be associated with multiple classes. Friend functions are not members of the class and are called like normal functions, requiring an object to access class members.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Friend function and its characteristics

A friend function in C++ allows a non-member function to access private data of a class. It is declared within the class using the 'friend' keyword and can be associated with multiple classes. Friend functions are not members of the class and are called like normal functions, requiring an object to access class members.

Uploaded by

saadkhan73212
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Friend Function :

Non - member function cannot have access to the class class-name


private data of a class. {
private:
However, there could be a situation where user
variable declaration;
would like share private data outside the class public:
with normal (independent) function or two classes function declaration;
to share particular function.
At this situation friend function is used. friend return-type function name(arguments);
//declaration
C++ allows the common function to be made };
friendly with more than one classes, thereby
allowing the function to have access to the return-type function name(arguments)
{
private data of classes. function body; // Its Normal function.
Such a function need not be a member of any
classes }

To make an outside function " friendly " to a class ,


simply declare the function as the friend of the channel link : https://www.youtube.com/c/ComputerScienceAcademy7
video link : https://youtu.be/zZPoAuULJRY
class as shown below
The keyword " friend ' declares the function to be class class-name
friendly with that class . {
This function is defined as a normal C ++ function. private:
The function definition does not use class - name , variable declaration;
public:
keyword friend or scope resolution operator.
function declaration;
Eg.
Class number friend return-type function name(arguments);
//declaration
{ };
private: int a;
public: void display(); return-type function name(arguments)
friend void show(); {
}; function body; // Its Normal function.

void number::display() void show() }


{ { number N1;
a = 5; N1.a = 14;
cout<<“Number = ”<<a; cout<<“Number = ”<<N1.a;
} }
Characteristics of friend Function :
It is not in the scope of the class to which it has Eg.
been declared as friend. Class number
(i.e it is not a member of that class) {
private: int a;
Since it is not in scope of the class, it cannot be public: void display();
called by using object of that class.
friend void show();
It is called like a normal C++ function.
};
It can be declared either in public or the private void number::display()
part of a class without affecting its meaning. {
It cannot access the member a = 5;
function directly and has to use an cout<<“Number = ”<<a;
object name and dot operator with }
each member name. void main() void show()
{ {
Usually, it has the objects as show(); number N1;
arguments. number N; N1.a = 14;
N.display(); cout<<“Number = ”<<N1.a;
} }

You might also like