Friend Function
Friend Function
[ES203]
Dr. Subhash Chand Gupta
1
Amity School of Engineering & Technology
2
Amity School of Engineering & Technology
Friend functions
• A friend function is a function that can access the private
members of a class as though it were a member of that
class.
4
Amity School of Engineering & Technology
6
Amity School of Engineering & Technology
7
Amity School of Engineering & Technology
class sample{
int length, width;
public:
sample(int len, int bre){
length=len, width =bre; }
friend void calcArea(sample s); //friend function declaration
};
//friend function definition
void calcArea(sample s){
cout<<"Area = "<<s.length * s. width;
}
int main()
{ sample s1(10,15);
calcArea(s1);
return 0;
8
}
Amity School of Engineering & Technology
class Box {
double width;
public:
void setWidth( double wid );
friend void printWidth( Box box ); };
9
Amity School of Engineering & Technology
int main( )
{
Box box;
// set box width without member function
box.setWidth(10.0);
// Use friend function to print the wdith.
printWidth( box );
return 0; }
10
Other example
Amity School of Engineering & Technology
#include <iostream>
using namespace std;
class XYZ
{
private: int num=100; char ch='Z';
public: friend void disp(XYZ obj);
};
Void disp(XYZ obj)
{
cout<<obj.num<<endl; cout<<obj.ch<<endl;
}
int main()
{
XYZ obj; disp(obj); return 0;
} 11
Amity School of Engineering & Technology
Friend Class
• Friend class can access private and
protected members of the class to which it
is a friend.
12
Amity School of Engineering & Technology
class A{
……
friend class B;
};
class B
{ ……..
};
B is a friend of class A. So class B can access the
private and protected members of class A.
But this does not mean that class A can access private
and protected members of the class B. Note that the
friendship is not mutual unless we make it so.
13
Amity School of Engineering & Technology
15
Amity School of Engineering & Technology
16
Amity School of Engineering & Technology
int main()
{
A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}
17
Properties of Friend Function
Amity School of Engineering & Technology
18
Array of Objects
Amity School of Engineering & Technology
Array of Objects
#include void main()
<iostream> {
class MyClass MyClass obs[4];
{ int i;
int x; for(i=0; i < 4; i++)
public: {obs[i].setX(i); } Output
void setX(int i) { x for(i=0; i < 4; i++) obs[0].getX(): 0
= i; } { obs[1].getX(): 1
int getX() { return cout << "obs[" << i << "].getX(): " << obs[2].getX(): 2
x; } obs[i].getX() << "\n"; obs[3].getX(): 3
}; }
getch(); }
Amity School of Engineering & Technology
#include<iostream.h>
#include<conio.h>
class Employee
{ int Id; char Name[25];
int Age; long Salary;
public: void GetData() //Statement 1 : Defining GetData()
{
cout<<"\n\tEnter Employee Id : ";
cin>>Id; cout<<"\n\tEnter Employee Name : ";
cin>>Name; cout<<"\n\tEnter Employee Age : ";
cin>>Age;
cout<<"\n\tEnter Employee Salary : ";
cin>>Salary;
}
void PutData() //Statement 2 : Defining PutData()
{
cout<<"\n"<<Id<<"\t"<<Name<<"\t"<<Age<<"\t"<<Salary;
} }; 21
Amity School of Engineering & Technology
22
Amity School of Engineering & Technology
Output
The value using object d : 28
The value using object d1 : 8
Amity School of Engineering & Technology
Thank You
26