Sunteți pe pagina 1din 11

Topic: Friend Functions

Course Name:
Object Oriented
Programming with
C/C++
Course Code:
19008600
Friend Function
 Access Privileges in C++
◦ You have access privileges in C++ such as
public, private, protected that helps in
encapsulation of data at various level.
 Private:
If data are declared as private in a class
then its accessible by the member
functions of the class where they are
declared.
The private member functions can be
accessed only by the member of the class.
By default, any member of the class is
considered as private by the C++ compiler,
if no specifier is declared for the member.
Friend Function(Cont…)
 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 class.

 Protected:
 Protected members are accessible by the class itself
and its subclasses.
 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, stay protected in case of protected
inheritance.
Friend Function(Cont…)
 When we want our private data to be shared by non
member function.
Then:
• Basically , we declare something as a friend, you give it
access to your private data members.
• Single functions or entire classes may be declared as
friends of a class.
Friend Function(Cont…)
 A friend function is a non-member function of the class
that has been granted access to all private members of
the class.
 We simply declare the function within the class by
prefixing its declaration with keyword friend.
 Function definition must not use keyword friend.
 Definition of friend function is specified outside the
class body and is not treated as a part of the class.
 The MAJOR difference b/w member function and friend
function is that the member function is accessed
through object to be passed as parameter.
Syntax
 class<class_name>
{
……
public:
friend return_type function_name(class_name obj)

};
return_type function_name(class_name obj)
{

}
Note:
 Friend Function must present inside
the function. However, function must
be defined outside the class without
using “Friend” keyword.
 Since it is not a member of a class, a
friend function is directly called
without using object.
 A function can be friend to multiple
classes.
 A class can have multiple Friend
Function.
Ques Write a program in C++ to find out useful life of a product using class &
object. Use friend function to calculate the lifetime of product.
#include<iostream.h>
#include<conio.h>
Class PRO
{
int mfg, exp;
public:
void input ( );
friend void lifetime(PRO);
};
void PRO : : input( )
{
cout<< “Enter the year of manufacture & expiry: “;
cin>> mfg>> exp;
}
void lifetime(PRO p1)
{
int LOP=0;
LOP=p1.exp – p1.mfg;
cout<< “ Useful life of a product : “ << LOP <<‘’yrs”;
}
void main( )
{
PRO p2;
p2.input;
lifetime(p2); //Friend Function is called
getch();
}
Ques Write a program in C++ to illustrate the concept that a function can friend of
multiple classes.

#include<iostream.h>
#include<conio.h>
class TWO; //Forward declaration of class
class ONE
{
int n1;
public:
void input ( );
friend void add(ONE,TWO); //Friend Function of class one
};
void ONE: : input( )
{
cout<< “Enter the value of n1: “;
cin>> n1;
}
class TWO
{
int n2;
public:
void input ( );
friend void add(ONE,TWO); //Friend Function of class two
};
void TWO: : input( )
{
cout<< “Enter the value of n1: “;
cin>> n2;
}
Void add(ONE o1,TWO t1)
{
int sum=0;
sum=o1.n1 + t1.n2;
cout<< “ Sum is “ <<sum;
}
Void main( )
{
ONE o2;
o2.input();
TWO t2;
t2.input();
add(02,t2);
}

Ques (Do it own your own)


Write C++ program to illustrate the concept that a class can have
multiple friend function.
Ques (Do it own your own)
Write C++ program to illustrate the concept that a class can have
multiple friend function.

Ques (Do it own your own)


Write a program to find Maximum out of Two Numbers Using Friend
Function.

Ques (Do it own your own)


Write a program to swap private data members of classes named as
class_1, class_2 using friend function.