Write a Program to Implement Friend Functions and Friend Classes in C++
💡 Understanding Friend Functions and Friend Classes in C++
C++ is an object-oriented programming language that enforces the concept of data hiding, where class members are by default private and inaccessible from outside the class. However, there are situations where access to these private members is necessary for specific functions or classes. To allow this controlled access, C++ provides friend functions and friend classes.
In this blog post, we will:
- Understand what friend functions and friend classes are
- Learn how to implement them
- Explore a complete C++ program that demonstrates both
🔷 What is a Friend Function?
A friend function is a non-member function that is granted access to the private and protected members of a class using the `friend` keyword. It is declared inside the class but defined outside like a regular function.
🔷 What is a Friend Class?
A friend class is a class whose all member functions have access to the private and protected members of another class.
✅ C++ Program to Demonstrate Friend Function and Friend Class
#include <iostream>
using namespace std;
class Box; // Forward declaration
// Friend function declaration
void showVolume(Box);
// Class A is a friend class of Box
class A {
public:
void printDimensions(Box&);
};
class Box {
private:
int length, breadth, height;
public:
// Constructor
Box(int l, int b, int h) {
length = l;
breadth = b;
height = h;
}
// Declare friend function
friend void showVolume(Box);
// Declare friend class
friend class A;
};
// Definition of friend function
void showVolume(Box b) {
int volume = b.length * b.breadth * b.height;
cout << "Volume of the box is: " << volume << endl;
}
// Member function of friend class A
void A::printDimensions(Box& b) {
cout << "Length: " << b.length << ", Breadth: " << b.breadth << ", Height: " << b.height << endl;
}
// Main function
int main() {
Box myBox(5, 4, 3);
// Call friend function
showVolume(myBox);
// Use friend class
A objA;
objA.printDimensions(myBox);
return 0;
}
🔍 Output
Volume of the box is: 60
Length: 5, Breadth: 4, Height: 3
📝 Explanation
- The class `Box` has private members: `length`, `breadth`, and `height`.
- `showVolume()` is a friend function, so it can directly access private members of `Box` without using any getter methods.
- The class `A` is declared as a friend class of `Box`, which means all member functions of class `A` can access the private data of class `Box`.
- In `main()`, we demonstrate both usages clearly with output.
✅ Use Cases of Friend Functions and Classes
- Operator overloading, where non-member functions need access to private data.
- Allowing controlled access to class internals without exposing them through public methods.
- Tight coupling between specific classes that need to collaborate closely.
📌 Conclusion
Friend functions and friend classes are powerful features in C++ that help in accessing private data securely, especially in cases where encapsulation might create overhead. However, they should be used sparingly as they break encapsulation, one of the core principles of object-oriented programming.
With the help of the above example, you should now be able to declare and use friend functions and friend classes in your C++ programs efficiently.
Comments
Post a Comment