C++(friend function)
C++(friend function)
#include <iostream>
var = -var;
return var;
if (var < 0)
var = -var;
return var;
int main() {
cout << "Absolute value of 5.5 = " << absolute(5.5f) << endl;
return 0;
Output
Absolute value of -5 = 5
Working of overloading
for the absolute() function
In this program, we overload the absolute() function. Based on the type of parameter passed during
the function call, the corresponding function is called.
int main() {
int a = 5;
double b = 5.5;
return 0;
}
Output
Integer number: 5
Float number: 5.5
Integer number: 5 and double number: 5.5
Here, the display() function is called three times with different arguments. Depending on the
number and type of arguments passed, the corresponding display() function is called.
Note: In C++, many standard library functions are overloaded. For example,
the sqrt() function can take double, float, int, etc. as parameters. This is possible because
the sqrt() function is overloaded in C++.
C++ friend Function and friend Classes
Data hiding is a fundamental concept of object-oriented programming. It restricts the access
of private members from outside of the class.
Similarly, protected members can only be accessed by derived classes and are inaccessible
from outside. For example,
class MyClass {
private:
int member1;
}
int main() {
MyClass obj;
#include <iostream>
using namespace std;
class Distance {
private:
int meter;
// friend function
friend int addFive(Distance);
public:
Distance() : meter(0) {}
};
int main() {
Distance D;
cout << "Distance: " << addFive(D);
return 0;
}
Output
Distance: 5
Here, addFive() is a friend function that can access 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 meaningful use would be operating on objects of two different classes. That's when
the friend function can be very helpful.
#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;
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;
class ClassA {
// ClassB is a friend class of ClassA
friend class ClassB;
... .. ...
}
class ClassB {
... .. ...
}
When a class is declared a friend class, all the member functions of the friend class become
friend functions.
Since ClassB is a friend class, we can access all members of ClassA from inside ClassB.
However, we cannot access members of ClassB from inside ClassA. It is because friend
relation in C++ is only 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;
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.