C++ friend Function and friend Classes
In this tutorial, we will learn to make friend functions and friend classes in C++ with the help of examples.
Data hiding away is a central idea of object-oriented programming. It confines the entrance of private individuals from outside of the class.
Correspondingly, ensured individuals must be gotten to by derived classes and are difficult to reach from outside. For instance,
class MyClass {
private:
int member1;
}
int main() {
MyClass obj;
// Error! Cannot access private members from here.
obj.member1 = 5;
}
In any case, there is an element in C++ called friend functions that disrupt this guideline and allow us to get to part works from outside the class.
Also, there is a friend class too, which we will learn later in this tutorial.
In this article, you will learn-
friend Function in C++
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.
class className {
... .. ...
friend returnType functionName(arguments);
... .. ...
}
Example 1: Working of friend Function
// C++ program to demonstrate the working of friend function
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
// friend function
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
// friend function definition
int addFive(Distance d) {
//accessing private members from the friend function
d.meter += 5;
return d.meter;
}
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Output
Distance: 5
Here, addFive() is a friend work that can get to both private and public data members.
Though this example gives us an idea about the concept of a friend function, it doesn’t show any meaningful use.
A more important use would be working on objects of two unique classes. That is the point at which the friend function can be useful.
Example 2: Add Members of Two Different Classes
// 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;
}
Output
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;
friend Class in C++
We can also use a friend Class in C++ using the friend keyword. For example,
class ClassB;
class ClassA {
// ClassB is a friend class of ClassA
friend class ClassB;
... .. ...
}
class ClassB {
... .. ...
}
At the point when a class is declared a friend class, all the part elements of the companion class become companion capacities.
Since classB is a friend class, we can get to all individuals from classA from inside classB.
However, we can’t get to individuals from ClassB from inside classA. It is on the grounds that friend relation in C++ is just granted , not taken.
Example 3: C++ friend Class
// C++ program to demonstrate the working of friend class
#include <iostream>
using namespace std;
// forward declaration
class ClassB;
class ClassA {
private:
int numA;
// friend class declaration
friend class ClassB;
public:
// constructor to initialize numA to 12
ClassA() : numA(12) {}
};
class ClassB {
private:
int numB;
public:
// constructor to initialize numB to 1
ClassB() : numB(1) {}
// member function to add numA
// from ClassA and numB from ClassB
int add() {
ClassA objectA;
return objectA.numA + numB;
}
};
int main() {
ClassB objectB;
cout << "Sum: " << objectB.add();
return 0;
}
Output
Sum: 13
Here, ClassB is a friend class of ClassA. So, ClassB has access to the members of classA.
In ClassB, we have created a function add() that returns the sum of numA and numB.
Since ClassB is a friend class, we can create objects of ClassA inside of ClassB.
Thanks for watching! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.