Sitemap

Friend Function

5 min readMay 30, 2024

--

In object-oriented programming, private members of a class are inaccessible from outside the class, meaning non-member functions cannot directly access the class’s private data. However, there are scenarios where one class may need to access another class’s private data, or an external function may require access to a class’s private members. This can be achieved using friend functions.

A friend function is a non-member function that has full access rights to the private members of a class. To designate an external function as a friend to a class, it must be declared as such using the friend keyword before its declaration.

Here’s the basic syntax for declaring a friend function within a class:

class ABC {
// Class members…
public:
// Public members…
friend void xyz(); // Declaration of a friend function
};

void xyz(void) //function definition
{
//function body
}
It should be noted that a function definition does not use friend keyword or scope resolution operator (: :). A function can be declared as friend in any number of classes.
Characteristics of a Friend Function:
• It is not in the scope of the class to which it has been declared as friend. That is why, it cannot be called using object of that class.
• It can be invoked like a normal function without the help of any object.
• It cannot access member names (member data) directly and has to use an object name and dot membership operator with each member name(eg.A.x)
• It can be declared in the public or the private part of a class without affecting its meaning.
• Usually, it has the objects as arguments.

1.Program to illustrate the use of friend function
#include <iostream>
using namespace std;
class sample
{
private:
int a;
int b;
public:
void setvalue( )
{
a=25 ;
b=40 ;
}
friend float mean (sample s) ;
};
float mean (sample s)
{
return float(s.a+s.b)/2 ;
}
int main( )
{
sample x ;
x.setvalue( ) ;
cout<<”Mean value=”<<mean(x)<<endl;
return 0 ;

}

2. Create classes called class1 and class2 with each of having one private member .Add member function to set a value(say setvalue) one each class. Add one more function max() that is friendly to both classes. max() function should compare two private member of two classes
and show maximum among them. Create one-one object of each class and then set a value on them. Display the maximum number among them.
#include <iostream>
using namespace std;
class class2;
class class1
{
private:
int x;
public:
void setvalue (int num)
{
x=num ;
}
friend void max (class1, class2);
};
class class2
{
private:
int y ;
public:
void setvalue(int num)
{
y=num ;
}
friend void max(class1, class2) ;
};
void max(class1 m, class2 n)
{
if (m.x>=n.y)
cout<<”Maximum value=”<<m.x ;
else
cout<<”Maximum value=”<<n.y ;
}
int main( )
{
class1 p;
class2 q;
p.setvalue(10) ;
q.setvalue(20) ;
max(p, q);
return 0;
}

WAP using friend function to Add Data Objects of two Different Classes
OR
Addition of two private data of different classes using friend fuction.

#include <iostream>
using namespace std;
class ABC;
// Forward declaration
class XYZ
{
private:
int x;
public:
void setdata (int num)
{
x=num ;
}
friend void add (XYZ,ABC);
};
class ABC
{
private:
int y ;
public:
void setdata (int num)
{
y=num ;
}
friend void add(XYZ,ABC) ;
};

void add (XYZ m, ABC n)// Definition of friend
{
cout<<”Sum =”<<(m.x+n.y);
}
int main( )
{
XYZ p;
ABC q;
p.setdata(15) ;
q.setdata(20) ;
add(p,q);
return 0;
}
Output:
Sum=35
Alternative solution
#include <iostream>
using namespace std;
class ABC; // Forward declaration
class XYZ
{
public:
int x;
void getdata ()
{
cout<<”Enter Value of class XYZ”<<endl;
cin>>x;
}
friend void add (XYZ,ABC);
};
class ABC
{
int y ;
public:
void getdata ()
{
cout<<”Enter the value of class ABC”<<endl;
cin>>y;
}
friend void add(XYZ,ABC) ;
};
void add (XYZ m, ABC n)
{
cout<<”Sum =”<<(m.x+n.y);
}
int main( )
{
XYZ p;
ABC q;
p.getdata() ;
q.getdata() ;
add(p,q);
return 0;
}
Swapping Private data of classes
#include <iostream>
using namespace std;
class ABC; // Forward declaration
class XYZ
{
private:
int x;
public:
void getdata ()
{
cout<<”Enter Value of class XYZ”<<endl;
cin>>x;
}
void display()
{
cout<<”value1=”<<x<<endl;
}
friend void swap (XYZ &,ABC &);
};
class ABC
{
private:
int y ;
public:
void getdata ()
{
cout<<”Enter the value of class ABC”<<endl;
cin>>y;
}
void display()
{
cout<<”value2=”<<y<<endl;
}
friend void swap(XYZ &,ABC &) ;
};
void swap (XYZ &m, ABC &n) // Definition of friend
{
int temp;
temp=m.x;
m.x=n.y;
n.y=temp;
}
int main( )
{
XYZ p;
ABC q;
p.getdata() ;
q.getdata() ;
cout<<”Value before swapping”<<endl;
p.display();
q.display();
swap(p,q);
cout<<”Value after swapping”<<endl;
p.display();
q.display();
return 0;
}
Friend class
We can declare all the member functions of one class as the friend functions of another class. In such cases, the class is called friend class. A friend class can use all the data member of a class for which it is friend. For example
class B;
class A
{
…….
friend class B;
};
Here, class B can use all the data member of class A.
Example:
#include<iostream>
using namespace std;
class B; //forward declaration
class A
{
int x,y;
public:
void getdata()
{
cout<<”Enter the value of x and y:”<<endl;
cin>>x>>y;
}
friend class B;
};
class B
{
int z;
public:
void getdata()
{
cout<<”Enter the value of z:”;
cin>>z;
}
void sum(A t)
{
cout<<”Sum of x,y and z =”<<(t.x+t.y+z)<<endl;
}
};
int main()
{
A p;
B q;
p.getdata();
q.getdata();
q.sum(p);
return 0;
}

Get Rudra Nepal’s stories in your inbox

Join Medium for free to get updates from this writer.

Wap to add two time using Friend Function

#include <iostream>
using namespace std;
class time {
private:
int hr, min;
public:
void set_data( );
void display();
friend void add_time(time t1, time t2);
};
void time:: set_data ( ) {
cout << “Enter hour:”;
cin>>hr;
cout << “Enter minutes:”;
cin>>min;
}
void time::display( ) {
cout<< “hr:”<<hr;
cout<< “ min:”<<min<<endl;
}

void add_time(time t1, time t2) {
int min, hr;
min = t1.min + t2.min;
hr = min/60;
min = min%60;
hr = hr + t1.hr + t2.hr;
cout<< “hr:”<<hr;
cout<< “ min:”<<min<<endl;
}
int main( ) {
time t1, t2, t3;
t1.set_data ();
t2.set_data ();
t1.display ();
t2.display ();
add_time (t1, t2);

return 0;
}

Create a class Mountain with data members name, height, location, a constructor that initializes the members to the values passed it to its parameters, a function called CmpHeight() to compare two objects and DispInf() to display the information of mountain. In main create two objects of the class mountain and print the information of the mountain which is greatest height.

#include<iostream>
#include<string.h>
using namespace std;
class Mountain
{
private:
char name[20],location[20];
float height;
public:
Mountain(char n[],float h,char l[])
{
strcpy(name,n);
height=h;
strcpy(location,l);
}
void DispInf()
{
cout<<”Name:”<<name<<endl;
cout<<”Height:”<<height<<endl;
cout<<”Location:”<<location<<endl;
}
friend void CmpHeight(Mountain,Mountain);
};
void CmpHeight(Mountain m1,Mountain m2)
{
if(m1.height>m2.height)
{
m1.DispInf();
}
else
{
m2.DispInf();
}
}

int main()
{
char name1[20],name2[20];
float height1,height2;
char location1[20],location2[20];
cout<<”Enter Name,Height and location of first mountain”<<endl;
cin>>name1>>height1>>location1;
cout<<”Enter Name,Height and location of second mountain”<<endl;
cin>>name2>>height2>>location2;
Mountain m1(name1,height1,location1);
Mountain m2(name2,height2,location2);
cout<<”Information of mountain with greatest height”<<endl;
CmpHeight(m1,m2);
return 0;
}

WAP to find sum of two weight.

#include <iostream>
using namespace std;
class weight {
private:
int kg, gm;
public:
void set_data( );
void display( );
friend weight sum_weight(weight w1, weight w2);
};
void weight:: set_data ( ) {
cout << “Enter kg:”;
cin>>kg;
cout << “Enter gram:”;
cin>>gm;
}
void weight::display() {
cout<< “KG:”<<kg;
cout<< “ Gram:”<<gm<<endl;
}

weight sum_weight(weight w1, weight w2) {
weight w;
w.gm = w1.gm + w2.gm;
w.kg = w.gm/1000;
w.gm = w.gm%1000;
w.kg = w.kg + w1.kg + w2.kg;
return w;
}
int main( ) {
weight w1, w2, w3;
w1.set_data ();
w2.set_data ();
w1.display ();
w2.display ();
w3 = sum_weight (w1, w2);
w3.display ();

return 0;
}

--

--