Friend Class in C++ - Definition, Syntax and Examples

Introduction to Friend Class in C++

In C++, a friend class is a class that is allowed to access the private and protected members of another class. Normally, private and protected members cannot be accessed directly from outside the class, but friendship provides a controlled exception.

C++ में Friend Class ऐसी class होती है जिसे किसी दूसरी class के private और protected members को access करने की अनुमति दी जाती है। सामान्यतः private और protected members को class के बाहर से सीधे access नहीं किया जा सकता, लेकिन friendship इस नियम में विशेष अनुमति प्रदान करती है।

Syntax of Friend Class

A friend class is declared inside another class using the friend keyword.

class ClassA {
friend class ClassB;
private:
int number;
};

Here, ClassB is declared as a friend of ClassA. Therefore, member functions of ClassB can access the private and protected members of ClassA through an object of ClassA.

यहाँ ClassB, ClassA की friend class है। इसलिए ClassB के member functions, ClassA के object के माध्यम से उसके private और protected members को access कर सकते हैं।

Example 1: Simple Friend Class in C++

The following program demonstrates how a friend class accesses the private data member of another class.

#include <iostream>
using namespace std;
class A {
private:
int number;
public:
A() {
number = 100;
}
friend class B;
};
class B {
public:
void display(A obj) {
cout << "Number = " << obj.number;
}
};
int main() {
A objA;
B objB;
objB.display(objA);
return 0;
}

Output:

Number = 100

Explanation:

  • Class A contains a private data member number.

  • Class B is declared as a friend of class A.

  • The display() member function of class B accesses obj.number, even though number is private.

  • The program successfully displays the value.

व्याख्या:

  • Class A में number नाम का private data member है।

  • Class B को A की friend class घोषित किया गया है।

  • Class B का display() function, number private होने के बावजूद obj.number को access करता है।

  • इसलिए program सफलतापूर्वक value display करता है।

Example 2: Friend Class Accessing Private and Protected Members

A friend class can access both private and protected members of another class.

Friend class किसी दूसरी class के private और protected दोनों प्रकार के members को access कर सकती है।

#include <iostream>
using namespace std;
class Student {
private:
int rollNo;
protected:
float marks;
public:
Student() {
rollNo = 101;
marks = 85.5;
}
friend class Result;
};
class Result {
public:
void display(Student s) {
cout << "Roll Number: " << s.rollNo << endl;
cout << "Marks: " << s.marks << endl;
}
};
int main() {
Student s;
Result r;
r.display(s);
return 0;
}

Output:

Roll Number: 101
Marks: 85.5

Explanation:

In this example, class Result is declared as a friend of class Student. Therefore, its member function can access both the private member rollNo and the protected member marks.

इस उदाहरण में Result class को Student class की friend घोषित किया गया है। इसलिए इसका member function private member rollNo और protected member marks दोनों को access कर सकता है।

Characteristics of Friend Class in C++

  1. A friend class can access the private and protected members of the class that declares it as a friend.
    Friend class उस class के private और protected members को access कर सकती है जिसने उसे friend घोषित किया है।

  2. Friendship is granted using the friend keyword.
    Friendship देने के लिए friend keyword का उपयोग किया जाता है।

  3. Friendship is not mutual. If class B is a friend of class A, class A does not automatically become a friend of class B.
    Friendship एकतरफा होती है। यदि class B, class A की friend है, तो class A अपने आप class B की friend नहीं बन जाती।

  4. Friendship is not inherited. A derived class does not automatically become a friend.
    Friendship inherit नहीं होती। Derived class अपने आप friend नहीं बनती।

  5. Friendship is not transitive. If B is a friend of A and C is a friend of B, C does not automatically become a friend of A.
    Friendship transitive नहीं होती। यदि B, A की friend है और C, B की friend है, तो C अपने आप A की friend नहीं बनती।

  6. A class can be a friend of more than one class.
    एक class को एक से अधिक classes का friend बनाया जा सकता है।

Difference Between Friend Function and Friend Class

Friend FunctionFriend Class
A friend function is a non-member function that is allowed to access private and protected members.A friend class allows all its member functions to access private and protected members of the class declaring it as a friend.
It is declared using the friend keyword followed by a function declaration.It is declared using the friend keyword followed by a class declaration.
Friendship is granted to a particular function.Friendship is granted to an entire class.
It is useful when only one function needs special access.It is useful when multiple member functions of another class need special access.

Advantages of Friend Class

  • Controlled Access: It provides selected classes access to private and protected members.
    नियंत्रित Access: यह चुनी हुई classes को private और protected members access करने की अनुमति देती है।

  • Better Collaboration: It helps two closely related classes work together.
    बेहतर सहयोग: यह आपस में संबंधित classes को एक साथ काम करने में सहायता करती है।

  • Useful in Complex Programs: It can simplify the implementation of classes that need to share internal data.
    जटिल Programs में उपयोगी: यह उन classes के implementation को सरल बना सकती है जिन्हें internal data साझा करने की आवश्यकता होती है।

A friend class in C++ is a mechanism that allows one class to access the private and protected members of another class. It is useful when two classes need close interaction, but it should be used carefully because excessive friendship can reduce data encapsulation.

C++ में Friend Class एक ऐसी सुविधा है जो एक class को दूसरी class के private और protected members access करने की अनुमति देती है। यह संबंधित classes के बीच interaction को आसान बनाती है, लेकिन इसका उपयोग सावधानी से करना चाहिए क्योंकि अत्यधिक friendship data encapsulation को कमजोर कर सकती है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

C++ Programming Syllabus Complete - From Basics to Advanced C++, including OOP and Data Structures and Algorithms (DSA)

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)