C++ 29: Friend Functions
Definition: When we want to use private and protected members of a class outside the class in that case we use friend functions.
Characteristics of a Friend Function:
1)The Friend Function is not a ember function of the class.
2)The friend function can be declared as friend of class by defining
the prototype of the function inside the class preceded with the keyword
friend.
3) The friend function can use private protected members of one or more
classes outside the class.
4) The prototype of functions can be write in a private or protected or public
section of class.
5) The friend function is mainly used if we want to perform specific operation
by using private or protected members of two or more different classes.
6) Usually the objects have arguments.
Syntax Prototype:
friend return_type funct_name (datatype_of_arg1,data_type_of_arg_2,....);
Syntax For calling:
1)fun_name();
2)variable = fun_name(val1,val2,...);
Example: Declaring Main function as friend function.
#include <iostream>using namespace std;class Set{private:int x = 2;protected:int y = 4;public:int z = 6;friend int main();};int main(){Set a;cout << "a.x = " << a.x << endl;cout << "a.y = " << a.y << endl;cout << "a.z = " << a.z << endl;return 0;}
Output:
a.x = 2
a.y = 4
a.z = 6Design a class Alpha with an int data member.Define a member
function showdata().The objects of the class must be created by
passing data.
Design a class Beta with a float data member function showdata().
The objects of the class must be created by passing data.
Define a friend function sum() which will adds the detail of the
Alpha and Beta class and return it.
#include <iostream>using namespace std;class Beta;class Alpha{private:int x;public:Alpha(int X){x = X;}void showData(){cout << "The vale of alpha x is :" << x << endl;}friend float sum(Alpha, Beta);};class Beta{private:float y;public:Beta(float Y){y = Y;}void showData(){cout << "The value of beta Y is : " << y << endl;}friend float sum(Alpha, Beta);};float sum(Alpha obj1, Beta obj2){return (obj1.x + obj2.y);}main(){Alpha obj1(10);Beta obj2(3.6);obj1.showData();obj2.showData();cout << "Sum is " << sum(obj1, obj2) << endl;}
Output:
The vale of alpha x is :10
The value of beta Y is : 3.6
Sum is 13.6Create a friend function to swap the two members object.
#include <iostream>using namespace std;class Sample{private:int x, y;public:Sample(int x, int y){x = x;y = y;}void showData(){cout << "The value of x: " << x << endl;cout << "The value of y: " << y << endl;}friend void swap(Sample &obj);};void swap(Sample &obj1) //friend function to swap members of function{int temp = obj1.x;obj1.x = obj1.y;obj1.y = temp;}main(){Sample obj1(20, 30);obj1.showData();swap(obj1); //function callcout << "Value after swaping: " << endl;obj1.showData();}
Output:
The value of x: 16
The value of y: 0
Value after swaping:
The value of x: 0
The value of y: 16Example 3:Create three classes Alpha, Beta and Gama and create a friend function max which will print which class object have greatest value.
#include <iostream>using namespace std;class Gamma;class Beta;class Alpha{private:int a;public:Alpha(int x){a = x;}void showdata(){cout << "The Value of Alpha A: " << a << endl;}friend void max(Alpha a, Beta b, Gamma c);};class Beta{private:int b;public:Beta(int y){b = y;}void showdata(){cout << "The Value of Beta B: " << b << endl;}friend void max(Alpha a, Beta b, Gamma c);};class Gamma{private:int c;public:Gamma(int z){c = z;}void showdata(){cout << "The Value of Gamma c: " << c << endl;}friend void max(Alpha a, Beta b, Gamma c);};void max(Alpha x, Beta y, Gamma z){if (x.a > y.b && x.a > z.c){cout << "The Greatest is Alpha x:" << x.a << endl;}else if (y.b > z.c){cout << "The Greatest is Beta y: " << y.b << endl;}else{cout << "The max is Gamma c:" << z.c << endl;}}main(){Alpha a(10);Beta b(20);Gamma c(15);max(a, b, c);return 0;}
Output:
The Greatest is Beta y: 20Create a class Set having a single data member x of int. Create a friend function in it
which will swap the values of two objects of same class.
#include <iostream>using namespace std;class Set{private:int x;public:Set(int X){x = X;}void showData(){cout << "The Value is " << x << endl;}friend void swap(Set &p, Set &q);};void swap(Set &p, Set &q){int temp = p.x;p.x = q.x;q.x = temp;}main(){Set a(20), b(8);a.showData();b.showData();swap(a, b);cout << "After Swaping:" << endl;a.showData();b.showData();}
Output:
The Value is 20
The Value is 8
After Swaping:
The Value is 8
The Value is 20
Comments
Post a Comment