Demonstrate Friend Function

Beginner

Introduction

In this lab, we will learn how to demonstrate the concept of Friend Function in the C++ programming language. We will create a Volume class with private member variables and use a Friend Function to access these private variables outside of the class.

Create the Volume class

First, create a new C++ file in the ~/project directory with the name main.cpp, and add the following code to define the Volume class:

#include <iostream>

using namespace std;

//Class Volume to demonstrate the concept of Friend Function in CPP
class Volume {
    //Member variables are declared as private and hence cannot be simply accessed from outside the class
    private:
        int liter;

    //Initializing the value of variable liter to 2 using the default constructor
    public:
        Volume(): liter(2) {}
};

We define a Volume class with a private member variable liter which cannot be directly accessed from outside the class.

Declare a Friend Function for the Volume class

Now, declare a Friend Function for the Volume class which will allow us to access the private variables of this class outside of the class.

//Declaring the Friend Function for the class Volume
friend int mulFive(Volume);

We declare a Friend Function with name mulFive() which takes a Volume object as input and returns an integer value.

Define the Friend Function

Now, define the mulFive() function which will use the Friend Function feature to access the private variable of the Volume class. Add the following code to define the mulFive() function:

// Defining the Friend Function to be used by the Volume Class
int mulFive(Volume v) {
    //Friend function enables accessing private data from non-member function
    v.liter *= 5;
    return v.liter;
}

We define a mulFive() function which has access to Volume's private variable liter using the Friend Function feature. This function multiplies the liter variable by 5 and returns it.

Call the Friend Function in the main() function

In the main() function, create an object of the Volume class and call the mulFive() function to access and manipulate the private data of the Volume class. This is the final and most important step.

//Defining the main method to access the members of the class
int main() {

    cout << "\n\nWelcome to LabEx :-)\n\n\n";
    cout << " =====  Program to demonstrate the working of a Friend Function in CPP  ===== \n\n";

    //Declaring the Class objects to access the class members
    Volume vol;

    cout << "Volume after calling the Friend Function = " << mulFive(vol);

    cout << "\n\n\n";

    return 0;
}

We define a main() function which creates an object of the Volume class and calls the mulFive() function to access the private data of the Volume class.

Summary

In this lab, we learned how to demonstrate the concept of Friend Function in the C++ programming language by creating a Volume class and using a Friend Function to access the private variables of this class outside of the class. A Friend Function is a non-member function that has access to the private members of a class, and it can be useful in some cases for manipulating or accessing private data of a class.

Other Tutorials you may like