Submitted by:
Submitted to: Natali Puri
Maninder Ma’am Course: BCA Hons.
Reg. no.:71910141
Friend Function in C++
Friend function in Cpp is a function that is preceded
by the keyword ‘Friend’. When the function is
declared as a friend, then it can access the private &
protected Data members of the class.
Access privileges in C++
You have access privileges in C++ such as public,
protected and private that helps in encapsulation
of data at various levels.
Private
If data are declared as private in a class then it is accessible by the
member functions of the class where they are declared. The private
member functions can be accessed only by the members of the
class. By default, any member of the class is considered as private
by the C++ complier, if no specifier is declared for the member.
Public
The member functions with public access specifier
can be accessed outside of the class. This kind of
members is accessed by creating instance of the
class.
Protected
Protected members are accessible by the class itself and it’s sub-
classes. The members with protected specifier act exactly like private
as long as they are referenced within the class or from the instance of
the class. This specifier specially used when you need to use
inheritance facility of C++. The protected members become private of
a child class in case of private inheritance, public in case of public
inheritance, and stay protected in case of protected inheritance.
Access Specifier Visible to own class Visible to objects of
members same/other class
Public Yes Yes
Private Yes No
Protected Yes No
Friend Functions
A friend function of a class is defined outside that class’s
scope, yet has the right to access the non-public (and
public) members of the class.
Let’s say you want a function to operate on objects of two
different classes and access their private data, you would
need to use a friend function.
Syntax of friend function in C++
class class_name
{
… .. …
friend return_type function_name(argument/s);
… .. …
}
Example of Friend Function
#include <iostream>
using namespace std;
class Box {
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
// Member function definition
void Box::setWidth( double wid ) {
width = wid;
}
// Note: printWidth() is not a member function of any class.
void printWidth( Box box ) {
/* Because printWidth() is a friend of Box, it can
directly access any member of this class */
cout << "Width of box : " << box.width <<endl;
}
// Main function for the program
int main( ) {
Box box;
// set box width with member function
box.setWidth(10.0);
// Use friend function to print the wdith.
printWidth( box );
return 0; Output
} Width of the box : 10
Friend Class in Cpp
A class can also be declared to be the friend of some other
class. When we create a friend class then all the member
functions of the friend class also become the friend of the other
class. This requires the condition that the friend becoming class
must be first declared or defined (forward declaration).
Example for Friend Class
#include <iostream>
using namespace std;
class MyClass
{
// Declare a friend class
friend class SecondClass;
public:
MyClass() : Secret(0){}
void printMember()
{
Cout << Secret << endl;
}
private:
int Secret;
};
class SecondClass
{
public:
void change( MyClass& yourclass, int x )
{
yourclass.Secret = x;
}
};
int main()
{
MyClass my_class;
SecondClass sec_class;
my_class.printMember();
sec_class.change( my_class, 5 );
my_class.printMember();
return 0;
}
Output
0
5
In this program we are using the SecondClass object to access
the MyClass object’s datamember and change its value. This is
possible because SecondClass is a friend of MyClass
Thanks!