Friend Function and Friend Class in C++
Data hiding is a fundamental concept in OOPs that restricts outsiders' access to private data and preserves private data and information. This tutorial will explain the friend function and friend class concepts in C++ using examples and a description.
What is a Friend Function in C++?
In C++, a friend function is a technique that gives users exclusive access to a class's private and protected members. They are commonly understood outside of the class. The word "friend" is used before a function is declared a friend function.
You should be aware that friend functions are not class member functions. It is a specific kind of keyword specified externally to a class but has access to its protected and private elements.
There might be situations when programmers want two classes to share members. These members may be data members, class functions, or function templates. To access the private and protected data of the class members in these circumstances, we add the necessary function of a friend to both classes.
Once a function has been marked as a friend function, it generally cannot access a class's private members, even the private and protected ones.
Here is a simple syntax for the friend function in C++:
class MyClass {
// Class members and functions
// Declaration of friend function
friend return_type friendFunction(parameters);
};
return_type friendFunction(parameters) {
// Function definition
}
- The class to which access will be given to the friend method is MyClass.
- The return type for the friend function is return_type.
- The name friendFunction knows the friend function.
- The friend function accepts parameters as input.
- The friend function is declared within the class using the friend keyword.
Example of Friend Function
Code:
#include <iostream>
class MyClass {
private:
int privateMember;
public:
MyClass(int value) : privateMember(value) {}
// Declaration of friend function
friend void friendFunction(MyClass &obj);
};
void friendFunction(MyClass &obj) {
// The friend function can access private members of MyClass
int value = obj.privateMember;
std::cout << "Value from friend function: " << value << std::endl;
}
int main() {
MyClass obj(42);
// Call the friend function
friendFunction(obj);
return 0;
}
In this example:
- We define a class MyClass with a private member privateMember.
- We declare a friend function friendFunction inside the class, allowing it to access private members of MyClass.
- The friendFunction is defined outside the class and takes a reference to a MyClass object as its parameter.
- Inside the friendFunction, we can access and print the private member of the MyClass object.
Output:
![Friend Function and Friend Class in C++](https://static.tutorialandexample.com/cpp/friend-function-and-friend-class-in-cpp1.png)
The friend function can access the private member of the MyClass object.
What is a Friend Class in C++?
“A friend class in C++ could access the protected and private members of the class when it is defined. A friend class is typically applied to provide access to the primary class that represents a part of a data structure represented by a class.”
The friend class approach extends storage and access to the components while maintaining appropriate encapsulation, as the data structure's users observe.
Much like a friend function, a friend class can access the protected and private members of the class in which it is declared.
Syntax:
class FriendClass {
// FriendClass members and functions
};
class MyClass {
// MyClass members and functions
friend class FriendClass; // Declaration of friend class
};
- MyClass is the class that declares FriendClass as a friend class.
- MyClass's protected and private members will be accessible to FriendClass.
- Using the friend keyword, you declare the friend class in MyClass.
Example of Friend Class:
Code:
#include <iostream>
class MyClass; // Forward declaration
class FriendClass {
public:
void accessMyClassPrivate(MyClass &obj);
};
class MyClass {
private:
int privateMember;
public:
MyClass(int value) : privateMember(value) {}
// Declaration of friend class
friend class FriendClass;
};
void FriendClass::accessMyClassPrivate(MyClass &obj) {
int value = obj.privateMember;
std::cout << "Value from FriendClass: " << value << std::endl;
}
int main() {
MyClass obj(73);
FriendClass friendObj;
friendObj.accessMyClassPrivate(obj);
return 0;
}
In this example,
- To define FriendClass, we first declared MyClass in a forward declaration. Because of this, FriendClass may refer MyClass before it has been fully defined.
- FriendClass is declared a friend class by MyClass, which also has a private member named privateMember.
- FriendClass has an externally specified member method called accessMyClassPrivate. To access the private member, it requires a reference to a MyClass object.
- Objects for MyClass and FriendClass are created in the main method. The MyClass object is subsequently passed as a parameter when we use the accessMyClassPrivate function of the FriendClass object.
- The definition of the friend function follows the class declaration.
Output:
![Friend Function and Friend Class in C++](https://static.tutorialandexample.com/cpp/friend-function-and-friend-class-in-cpp2.png)
The friend class can access the private member of the MyClass object.