Friend Functions
A friend function is a special function that is not a member of a class but has access to private and
protected members of the class. A friend function of a class is defined outside that class’s scope.
If a friend function is to be made a friend of a class then its prototype has to be declared within the
body of the class preceded by the keyword friend.
Friend functions are neither public nor private, and it can be declared anywhere inside the class.
Whenever a friend function is defined neither, the name of the class nor the scope resolution
operator appears in its definition. Moreover, the keyword friend also does not appear.
Whenever a friend function is called, neither the name of the object nor the dot operator appears.
If a friend function wants to manipulate the values of the data members of an object it needs to
reference the object to be passed as a parameter.
A function or an entire class may be declared to be a “friend” of another class.
o Assume we have two classes, Class1 and Class2.
Saying “friend class Class1” within Class2; will make Class1 a friend of Class2.
Friends are not symmetric. That is, if Class1 is a friend of Class2, it does not imply that Class2 is a
friend of Class1.
Friends are also not transitive. That is if Class1 is a friend of Class2, and Class2 is a friend of Class3, it
does not imply that Class1 is a friend of Class3.
Using friend functions enhances performance.
Use friendship with care. Incorrect use of friends may corrupt the concept of information hiding and
encapsulation.
Example 1 :
class example
{
private:
int num;
public:
example();
friend int getnum(example obj); //prototype
};
example::example()
{
num=0;
}
int getnum(example obj)
{
return obj.num;
}
#include<iostream>
using namespace std;
#include "example.h"
void main()
{
example e;
cout<<getnum(e)<<endl;
system("pause");
}
Example2:
class cube{
private:
int num;
public:
cube()
{
cout<<"Enter an integer value:";
cin>>num;
}
friend void findcube(cube&);
};
void findcube(cube&obj)
{
obj.num=obj.num*obj.num*obj.num;
cout<<obj.num<<endl;
}
#include<iostream>
using namespace std;
#include"cube.h"
void main()
{
cube c;
findcube(c);
cout<<endl;
system("pause");
}
Example3:
class count{
private:
int x;
public:
count() {x=0;}
void print() {cout<<x<<endl;}
friend void setx(count&,int);
};
void setx(count &c, int val)
{
c.x=val;
}
#include <iostream>
using namespace std;
#include”count.h”
void main()
{
count counter;
cout<<"Value of counter.x :";
counter.print();
cout<<"counter.x after call to setx friend function:";
setx(counter,8);
counter.print();
system("pause");
}
Output:
Value of counter.x:0
counter.x after call to setx friend function:8
Press any key to continue . . .
Example4:
class student {
private:
int grade;
public:
friend void display(student & );
void assign_grade(int newgrade)
{
grade=newgrade;
}
};
void display(student &s)
{
cout<<s.grade;
}
# include <iostream>
using namespace std;
#include”student.h”
void main()
{
student john;
john.assign_grade(80);
display(john);
}
Note: In above example:
Friend void display(student &); function prototype
Student john
Display(john) function call statement
Void display (student & s) function heading
Set up a reference relation between object john and object s both belonging to the class student.
Example5: (Creating object using PASS-BY-VALUE method)
class student
{
int stdno;
char grade;
float cgpa;
public:
void get();
friend void show (student);
};
void student::set()
{
cout << "enter stdno, grade & cgpa:";
cin >> stdno>> grade>>cgpa;
}
void show (student p)
{
cout << p.stdno<< " " << p.grade<< " " <<p.cgpa;
}
#include <iostream>
using namespace std;
#include”student.h”
void main( )
{
student S;
S.set();
show(S);
system("pause");
}
Example 6: (Creating an object using PASS-BY-REFERENCE method)
class student
{
int stdno;
char grade;
float cgpa;
public:
void set();
friend void show (student&);
};
void student::set()
{
cout << "enter stdno, grade & cgpa:";
cin >> stdno>> grade>>cgpa;
}
void show (student &p)
{
cout << p.stdno<< " " << p.grade<< " " <<p.cgpa;
}
#include <iostream>
using namespace std;
#include”student.h”
void main( )
{
student S;
set(S);
S.show();
system("pause");
}
Example 7: (Creating an object using PASS-BY-ADDRESS method)
class student
{
int stdno;
char grade;
float cgpa;
public:
void set();
friend void show (student*);
};
void student::set()
{
cout << "enter stdno, grade & cgpa:";
cin >> stdno>> grade>>cgpa;
}
void show (student* p)
{
cout << p->stdno<< " " << p->grade<< " " <<p->cgpa;
}
#include <iostream>
using namespace std;
#include”student.h”
void main( )
{
student S;
set(&S);
S.show();
system("pause");
}
Example 8:
class silver{
int x,y;
public:
silver()
{
x=1; y=2;
}
void display()
{cout<<x<<" "<<y<<endl;
}
void reset()
{
x-=2;
y*=2;
}
friend void fun1(silver);
friend void fun2(silver&);
};
void fun1(silver s)
{
cout<<s.x<<" "<<s.y<<endl;
s.x+=4;
s.y+=2;
cout<<s.x<<" "<<s.y<<endl;
}
void fun2(silver &s)
{
cout<<s.x<<" "<<s.y<<endl;
s.x+=2;
s.y+=3;
}
#include <iostream>
using namespace std;
#include”silver.h”
void main()
{
silver s1;
fun1(s1);
s1.reset();
fun2(s1);
s1.display();
system("pause");
}
Example 9: A FUNCTION BEING FRIEND OF TWO CLASSES
class beta; //forward declaration
class alpha{
private:
int x;
public:
void getx()
{
cout<<"enter x:";
cin>>x;
}
friend void compare(alpha, beta);
};
class beta{
private:
int y;
public:
void gety()
{
cout<<"enter y:";
cin>>y;
}
friend void compare(alpha , beta);
};
void compare(alpha a, beta b)
{
if(a.x>b.y)
cout<<"Greater value:"<<a.x<<endl;
else if(b.y>a.x)
cout<<"Greater value:"<<b.y<<endl;
else
cout<<"equal"<<endl; }
#include<iostream>
using namespace std;
#include"ab.h"
void main()
{
alpha obj1;
beta obj2;
obj1.getx();
obj2.gety();
compare(obj1,obj2);
system("pause");
}
EXERCISES
Write down the output of the following programs
a)
#include <iostream.h>
class test {
int x; double y; //private data members
public:
test(int=5);
void set(int);
int get(int);
double sqr();
void f1(int&t,double&d1);
void show();
void friend f2(test&m);
};
test:: test (int k)
{ x=k ; y=2.0*k; }
void test :: set (int t)
{ x=t;t=t*t; y=t; }
int test :: get ( int k )
{
if (k==1) return x;
else return x*x; }
double test :: sqr ()
{ x=x*x ; y=y*y;
return x*y; }
void test :: f1 (int&t, double&d1)
{ t=x ; d1=y; }
void test::show()
{ cout<<"\nX = "<<x<<" Y= "<<y<<endl; }
void f2 (test&m)
{
m.x+=5 ; m.y*=2.0;
}
void main()
{
int a; double b;
test obj1, obj2(3);
obj1.show();
obj2.show();
obj1.set(2);
obj1.show();
cout<<"\n"<<obj2.sqr();
obj2.show();
obj1.f1(a,b);
cout<<"\n"<<a<<" "<<b;
obj2.f1(a,b);
cout<<"\n"<<a<<" "<<b;
f2(obj1);
f2(obj2);
obj1.show();
obj2.show();
}
b)
#include <iostream.h>
class test {
int a , b ;
public:
test ( int x = 2 , int y = 1 )
{a=x*3;b=y*2;}
void show()
{ cout<<a<<" "<<b<<endl; }
friend void f1 ( test & );
int f2 ( int c , int & d )
{a=c+1;b =d+2;c=4;d=3;
return ( a*c - b*d) ; }
};
void f1 ( test & s )
{ cout<<s.a<<" "<<s.b<<endl;
s.a=3 ; s.b = 5 ; }
void main()
{
test t1, t2(5) , t3(3,2) ;
t1.show();
t2.show();
t3.show();
int k = 2 , m = 5 ;
cout<<t2.f2(k,m)<<endl;
cout<<k<<" "<<m<<endl;
t2.show();
f1(t3);
t3.show();
}