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

Friend Class Final Notes

PUTTASWAMY B S Assistant Professor, Dept. of CS&BS, PES College of Engineering,Mandya - 571401

Uploaded by

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

Friend Class Final Notes

PUTTASWAMY B S Assistant Professor, Dept. of CS&BS, PES College of Engineering,Mandya - 571401

Uploaded by

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

Friend Function and Class in C++

Introduction:
In C++, a class is a user-defined type or data structure that
includes data and functions as members and whose access is controlled by
the three access specifiers private, protected, and public. Generally, the
private data members of a class can accessible within that class only and
protected members can only be accessed by derived classes.
According to the data hiding concept, Access to members of a C++ class is
usually restricted. (we cannot access the (restricts the access of) private and
protected members from outside the class (global functions cannot access
the private members of a class)). That means a class cannot access the
private members of another class. Similarly, a class that doesn’t inherit
another class cannot access its protected members. Here, we are not talking
about public members because they are accessible outside the class upon an
object, but still, if we are trying to access it will prompt an error.
For example:
class MyClass
{
private:
int member1;
};
int main()
{
MyClass obj;
obj.member1 = 5; // Error! Cannot access private members from
here.
}
In some situation, we need to access the private or protected members
from outside of the class (In C++, global functions cannot access the private
members of a class. However, sometimes we need a global function to
access private members to perform certain operations, without defining it
inside the class).
This is where friend functions come into play.
Friend function:
A friend function is a non-member function that have a
privilege (permissions) to access all private and protected members
(variables and functions) to perform operations, even though it is not a
member function of that class. A Friend Function can be either a member
function of another class or a global function (defined outside the scope of
the class).

Friend class:
Friend classes are those classes that have permission to
access private and protected members of the class in which they are
declared. The main thing to note here is that if the class is made friends of
another class, then it can access all the private members of that class.

Use of Friend Class:


The Friend class is mostly used when operator
overloading is involved in classes. To access private member data, operator
overloading takes input and output, and the friend class helps in the easy
access of private members.
General Syntax:
The syntax for declaring a friend class inside a class is
class class_name
{
friend class friend_class_name;
};
Example:
#include <iostream>
using namespace std;
class A
{
int x =5;
friend class B; // friend class.
};
class B
{
public:
void display(A &a)
{
cout<<"value of x is : "<<a.x;
}
};
int main()
{
A a;
B b;
b.display(a);
return 0;
}
Class Declaration - temp1:
class temp1
{
int x = 5;
friend class temp2; // `temp2` declared as a friend class.
};
 temp1 has a private member variable x, initialized to 5.
 temp2 is declared as a friend class, which means temp2 can access the
private members of temp1.
Class Declaration - temp2:
class temp2
{
public:
void display(temp1 &a)
{
cout << "Value of x is : " << a.x;
}
};
 temp2 has a public member function display that takes a reference to a
temp1 object.
 Inside display, it accesses the private member x of the temp1 instance
and prints its value.
The function declaration void display(temp1 &a) indicates that the
function display takes a single argument, which is a reference to an object of
type temp1. Here's a breakdown of its components:
Components Breakdown
1. Return Type:
 void: This indicates that the display function does not return any value.
2. Function Name:
 display: This is the name of the function, which suggests that its
purpose is to display some information, in this case, from an instance
of temp1.
3. Parameter List:
 temp1 &a: This signifies that the function accepts one parameter:
 temp1: The type of the parameter, which is a class named temp1.
 &: The ampersand operator indicates that the parameter is passed
by reference, not by value. This means that the function will operate
on the original object passed to it, and any changes made
to a within the function will reflect on the original object.
 a: This is the name of the parameter; it will be used within the
function to refer to the passed object.
Main Function:
int main()
{
temp1 a;
temp2 b;
b.display(a); // Calls the display function of temp2 with a temp1 object.
return 0;
}
 In the main function, an instance of temp1 (a) and an instance of temp2
(b) are created.
 The display method of temp2 is called, passing a, which allows temp2 to
access and print the private member x of temp1.

You might also like