← Back to C++ Course | Chapter 8: OOP Core | Lesson 10 of 14

C++ के Friend Functions

एक friend एक function या class है जिस पर आप इतना trust करते हैं कि इसे अपनी class के private parts देखने दें, एक neighbor को एक spare key देने जैसा। इसे special access मिलता है।
Syntax
cpp
class ClassName {
  friend return_type function_name(ClassName obj);
  friend class OtherClass;
};

Friend Functions का Introduction

एक friend function एक function है जो एक class के अपने scope के बाहर defined है, लेकिन उस class द्वारा explicitly इसके private और protected members access करने की permission दी गई है।

यह strict encapsulation को एक controlled, opt-in तरीके से तोड़ता है, class खुद इसे चुनती है, बाहर से force नहीं होता।

उदाहरण: Introduction to Friend Functions

cpp
#include <iostream>

class Box {
	int size = 5;
	friend void showSize(Box b);
};

void showSize(Box b) {
	std::cout << b.size << std::endl; // allowed: showSize is a friend
}

int main() {
	Box b;
	showSize(b);
	return 0;
}

Friend Class

एक class एक पूरी दूसरी class को अपना friend declare कर सकती है।

जब यह करती है, उस friend class के हर member function को friendship देने वाली class के private और protected members तक access मिल जाता है, जो एक single function को befriend करने से कहीं ज़्यादा broad grant है।

उदाहरण: Friend Class

cpp
// Include std::cout and std::cin
#include <iostream>

// Define the type Engine
class Engine {
	// Declare power and set it to 100
	int power = 100;
	friend class Car;
};

// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
	void show(Engine e) { std::cout << e.power << std::endl; }
};

// Program execution starts in main()
int main() {
	Engine e;
	Car c;
	c.show(e);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Friend Member Functions

एक पूरी दूसरी class को friend बनाने की बजाय, आप उस class के सिर्फ एक specific member function को friend status दे सकते हैं, इसे private access देते हुए और उस class के बाकी methods को locked out रखते हुए — एक पूरी friend class का एक ज़्यादा surgical alternative।

उदाहरण: Friend Member Functions

cpp
// Include std::cout and std::cin
#include <iostream>

class Car;

// Define the type Mechanic
class Mechanic {
// Members below can be accessed from outside the class
public:
	void inspect(Car &c);
};

// Define the type Car
class Car {
	// Declare mileage and set it to 50000
	int mileage = 50000;
	friend void Mechanic::inspect(Car &c);
};

void Mechanic::inspect(Car &c) {
	// Print to the console with cout
	std::cout << c.mileage << std::endl;
}

// Program execution starts in main()
int main() {
	Car car;
	Mechanic m;
	m.inspect(car);
	// Return 0 to signal that the program finished successfully
	return 0;
}

कई Classes के साथ Friend Functions

एक single friend function को एक साथ कई classes का friend declare किया जा सकता है, इसे एक bridge की तरह act करने देते हुए जो दो अलग, अन्यथा-unrelated classes से belong करने वाला private data पढ़ता या compare करता है — दो types के बीच custom equality या arithmetic operators जैसी चीज़ों के लिए useful।

उदाहरण: Friend Functions with Multiple Classes

cpp
// Include std::cout and std::cin
#include <iostream>

class Wallet;

// Define the type Bank
class Bank {
	// Declare reserve and set it to 1000
	int reserve = 1000;
	friend void compare(Bank b, Wallet w);
};

// Define the type Wallet
class Wallet {
	// Declare cash and set it to 50
	int cash = 50;
	friend void compare(Bank b, Wallet w);
};

void compare(Bank b, Wallet w) {
	// Print to the console with cout
	std::cout << (b.reserve > w.cash) << std::endl;
}

// Program execution starts in main()
int main() {
	Bank bank;
	Wallet wallet;
	compare(bank, wallet);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Friend Functions के लिए Best Practices

Friend declarations convenient हैं लेकिन sparingly इस्तेमाल होनी चाहिए, क्योंकि आपका add किया हर friend आपके private internals तक direct access वाला code का एक और piece है।

इन्हें overuse करना exactly उस encapsulation को कम करता है जिसे enforce करने के लिए private और protected access specifiers exist करते हैं।

उदाहरण: Best Practices for Friend Functions

cpp
#include <iostream>

class Box {
	int size = 5;
public:
	int getSize() { return size; } // prefer a public getter over a friend when possible
	friend void debugPrint(Box b); // reserved for genuinely tight-coupled cases only
};

void debugPrint(Box b) {
	std::cout << "Debug size: " << b.size << std::endl;
}

int main() {
	Box b;
	debugPrint(b);
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि friendship inherited या mutual है, जब यह दोनों में से कुछ नहीं है।
  2. friend को overuse करना, जो encapsulation कमज़ोर करता है।
  3. एक function को friend declare करना लेकिन इसे कभी define न करना, जो एक linker error cause करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.