Home C++ Tutorial C++ program to demonstrate friend function and friend class

C++ program to demonstrate friend function and friend class

by Anup Maurya
28 minutes read

In this article, you’ll learn about how to make a C++ program to demonstrate friend function and friend class with explanation.

What is friend function?

A friend function can access the private and protected data of a class. We declare a friend function using the friend keyword inside the body of the class.

What is friend class?

Friend Class is a class that can access both private and protected variables of the class in which it is declared as a friend, just like a friend function.

C++ program to demonstrate friend function and friend class

// Add members of two different classes using friend functions

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {
    
    public:
        // constructor to initialize numA to 12
        ClassA() : numA(12) {}
        
    private:
        int numA;
        
         // friend function declaration
         friend int add(ClassA, ClassB);
};

class ClassB {

    public:
        // constructor to initialize numB to 1
        ClassB() : numB(1) {}
    
    private:
        int numB;
 
        // friend function declaration
        friend int add(ClassA, ClassB);
};

// access members of both classes
int add(ClassA objectA, ClassB objectB) {
    return (objectA.numA + objectB.numB);
}

int main() {
    ClassA objectA;
    ClassB objectB;
    cout << "Sum: " << add(objectA, objectB);
    return 0;
}

The output of this program is

Sum: 13

In this program, ClassA and ClassB have declared add() as a friend function. Thus, this function can access private data of both classes.

One thing to notice here is the friend function inside ClassA is using the ClassB. However, we haven’t defined ClassB at this point.

// inside classA 
friend int add(ClassA, ClassB);

For this to work, we need a forward declaration of ClassB in our program.

// forward declaration
class ClassB;

related posts

Leave a Comment

Enable Notifications OK No thanks