C++ Friend Function In Hindi

📔 : C++ 🔗

जैसा कि आप पिछले topics में पढ़ चुके हैं कि किसी class के private members को केवल उस class के अंदर ही access कर सकते हैं है यानी private को class के बाहर या Inheritance में child / derived class से access नहीं किये जा सकते है।
For Example

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define a User class.
class User {
  // define private members.
  private : 
  string name;
  int age;
  
  public :
  void set_user(){
    cout << "Enter User Name : ";
    getline(cin, name);
    cout << "Enter User Age : ";
    cin >> age;
  }
  
};

int main() {
  User user;
  cout << user.name;
  
  return 0;
} 
Output
In function 'int main()':
23:16: error: 'std::string User::name' is private within this context
23 |   cout << user.name;
   |                ^~~~
8:10: note: declared private here
8 |   string name;
  |          ^~~~

लेकिन कभी हमें ऐसी जरूरत आ ही जाती है , कि private members को भी access करना पड़ता है , ऐसे में C++ में हमें friend function का concept provide किया है।


एक friend function से हम किसी class के private member को access कर सकते हैं लेकिन यह class का member नहीं होता है। यह एक function होता है जो compiler को बताता है कि वह उस class का friend है जो उस class के private member को access कर सकता है।

C++ Define Friend Function

किसी class के private members को friend function के through access करने के लिए उसका declaration class अंदर दिया जाता है जबकि उसकी definition class के बाहर। जिससे हम उसे directly call कर सकें। इसे define करने के लिए friend keyword का use किया जाता है। और current class का Name भी pass किया जाता है , ताकि compiler किस class का friend function है।

class Test{
.... other member declaration..

return-type friend function_name(Current-Class-Object);

};

C++ Friend Function Example

अब ऊपर दिए गए example को हम friend function के through private members को access करके देखेंगे।

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define a User class.
class User {
  // define private members.
  private : 
  string name;
  int age;
  
  public :
  void set_user(){
    cout << "Enter User Name : ";
    getline(cin, name);
    cout << "Enter User Age : ";
    cin >> age;
  }
  // declare friend function
  void friend show_user(User);
};

// here provide difinition to friend function
void show_user(User userObj){
  cout << "User Info :" << endl;
  cout << "User Name : " << userObj.name << endl;
  cout << "User Age : " << userObj.age;
}

int main() {
  User user;
  user.set_user();
  
  // call friend function and pass Object.
  show_user(user);
  
  return 0;
} 
Output
Enter User Name : Rahul Rajput
Enter User Age : 24
User Info :
User Name : Rahul Rajput
User Age : 24

बैसे देखा जाए तो हम class object से ही private members को access कर रहे हैं , बस friend function द्वारा एक link बन जाता है members को access करने का।

हालाँकि OOPs concept कहता है कि private members class के बाहर access नहीं हो सकते हैं इसलिए C++ में friend function के through private members access करना OOPs concept के लिए एक अपवाद (Exception) है।


ध्यान रहे आप friend function static properties को access नहीं कर सकते हैं।


Important

Technically function class का part नहीं होते हैं , अगर class में किसी function को define करते हैं तो उसे method कहते हैं और friend functions किसी भी class का part नहीं हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers