Friend class and Function in C++ with Example

Friend Class and Friend Function in C++

In C++, a friend function is a function that is granted access to the private and protected members of a class. It can be declared inside the class, but it is not a member of that class. A friend function can be useful when you need to allow a function external to the class to access its private or protected members, while still maintaining encapsulation.

To declare a friend function, you need to add a function prototype inside the class declaration, preceded by the friend keyword.

Syntax:

class MyClass {

private:

    int privateData;

public:

    MyClass(int data) : privateData(data) {}

    friend void friendFunction(MyClass& obj);

};

void friendFunction(MyClass& obj) {

    // Accessing private member of MyClass

    obj.privateData = 42;

}

In the example above, friendFunction is declared as a friend function inside the MyClass declaration. It can access the private member privateData of any MyClass object.

To declare a friend class, you need to add a friend keyword followed by the class name inside the class declaration.

Syntax of Friend Function:

class MyClass {

private:

    int privateData;

public:

    MyClass(int data) : privateData(data) {}

    friend class FriendClass;

};

class FriendClass {

public:

    void modifyData(MyClass& obj, int newData) {

        obj.privateData = newData;

    }

};

In the above example, FriendClass is declared as a friend class of MyClass. It can access the private member privateData of any MyClass object through its modifyData member function.

Both friend functions and friend classes can be helpful in specific situations where you need to allow external functions or classes to access the private or protected members of a class while maintaining encapsulation and data hiding. However, it is generally recommended to minimize the use of friend functions and classes, as they can potentially break encapsulation if used excessively.

In C++ class has a data member and a member function. Data members declared private cannot be accessed outside the class, which means non-member functions of the class. But sometimes the situation arises, and private data members need to be accessed outside the class. That is done by the friend function. A friend function is called a non-member function. When we create a friend function, we can access private members of the class.

Example of a friend function in C++

#include<iostream.h>

#include<conio.h>

class xyz

{

private:

    int x;

  friend void disp();

};

void disp()

{

xyz obj;

obj.x=5;

cout<<“the value of x=”<<obj.x;

}

void main()

{

disp();

getch();

}

Output……

Friend class and Function in C++
Friend  Function in C++

Explanation….

In this example, we create a disp(), which is private. But the function friend means we can access outside the class. The friend function accesses the class variable x by using the dot operator. By using friend functions, the member function of one class can be accessed by another class.

Friend Class

In C++, a friend class(friend keyword) is a class that is allowed to access the private and protected members of another class.

 

Normally, private and protected members are not accessible outside the class, but by declaring a class as a friend, we give it special access.

Syntax:

class AA {

private:

    int secret;

 

public:

    AA() : secret(42) {}

 

 

    friend class BB;

};

 

class BB {

public:

    void showSecret(AA obj) {

        // Can access private member of AA

        cout << “Secret value: ” << obj.secret << endl;

    }

};

Example of a friend class in C++

#include <iostream.h>
#include <conio.h>
#include <string.h> // for strcpy

class Student {
private:
char name[30];
int marks;

public:
Student(char n[], int m) {
strcpy(name, n); // copy string into char array
marks = m;
}

// Declare Teacher as friend class
friend class Teacher;
};

class Teacher {
public:
void display(Student s) {
cout << “Student Name: ” << s.name << endl;
cout << “Student Marks: ” << s.marks << endl;
}
};

int main() {
clrscr();

Student stu(“XCnotes.com”, 15);
Teacher t;

t.display(stu);

getch(); // wait for key press
return 0;
}

friend class in C++
friend class in C++

Key Points of friend class in C++

  1. Friendship is not mutual.
  • If class AA is a friend of BB, it doesn’t mean BB is automatically a friend of AA.
  1. Friendship is not inherited.
  • If class BB is a friend of AA, then the derived classes of BB do not automatically become friends.
  1. Friend class can access private and protected members.

C++ Introduction

C++ Classes and Objects