Friend Functions
Submitted by Muzammil Muneer on Friday, October 24, 2014 - 09:35.
Friend functions
In this part you will learn: 1. What are friend functions 2. Uses of friend function 3. Syntax of friend function 4. Program 5. Output Friend Function A function declared as friend function isn’t really a part of member functions of a class but it can access the public as well as the private members of the class which considers the function as its friend. There are three most common uses of friend function. • To access member variables of two different classes to perform operations on it. • Making one class friend of the other to allow access to class members (private and protected). • To perform function on class without specifying the caller function. Right now we will consider the first and the second, the last one will be discussed in detail in the coming topic of operator overloading. Syntax Following is the syntax of making a function friend of a class. class Class_Name { friend return_type Function_name(parameters without variable names); } return_type Function_name(parameters) { //function definition } As we can see in the syntax that the function which is going to become a friend of class is declared/defined outside the class, the only part present in the class is its prototype following label “friend”. Note: while declaring a function friend in class, we don’t write the names of variables being passed in the function, i.e. we just write the data_type of parameters being passed in the function. By the example given below the concept will become clear. Program with Friend function In this example I have declared a Friend function “gcd” which calculates and returns the greatest common divisor of the fraction passed in it. Basic Step:- #include<iostream>
- #include<conio.h>
- using namespace std;
- class fraction
- {
- private: //access specifier
- int num;
- int denom;
- public:
- void getfraction(){
- cout << "Fraction is: " << num << "/" << denom << endl;
- }
- void setfraction(){
- cout << "Enter numerator: ";
- cin >> num;
- cout << "Enter denominator: ";
- cin >> denom;
- }
- void setnum(int n){
- num = n;
- }
- void setdenom(int d){
- denom = d;
- }
- int getnum(){
- return num;
- }
- int getdenom(){
- return denom;
- }
- fraction addfraction(fraction f){
- fraction sum;
- sum.num = (this->num*f.denom) + (this->denom*f.num);
- sum.denom = this->denom*f.denom;
- return sum;
- }
- friend int gcd(fraction);
- };
- int gcd(fraction f)
- {
- int temp; //variable declaration
- if (f.num != 0) //condition when numerator is not equal to zero
- {
- while (f.num != 0)
- {
- temp = f.denom%f.num; //calculations
- f.denom = f.num; //swaping
- f.num = temp; //swaping
- }
- }
- return f.denom;
- }
- int main()
- {
- fraction f1; //initializing a variable f1 of type fraction
- fraction f2; //initializing a variable f2 of type fraction
- f1.setfraction();
- f2.setfraction();
- fraction f3;
- f3 = f1.addfraction(f2);
- f3.setnum(f3.getnum() / gcd(f3));
- f3.setdenom(f3.getdenom() / gcd(f3));
- f3.getfraction();
- }
Add new comment
- 63 views