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

C++ के Access Specifiers

Access specifiers decide करते हैं कि कौन एक class के हिस्से इस्तेमाल कर सकता है, दरवाज़ों पर locks की तरह। public सबके लिए open है, private सिर्फ class के लिए है, और protected family के लिए है।
Syntax
cpp
class ClassName {
public:
  // accessible from anywhere
private:
  // accessible only inside the class
protected:
  // accessible in the class and derived classes
};

Public Specifier

public declared members को program में कहीं से भी access किया जा सकता है, class के अपने methods के अंदर से और उस type का object रखने वाले किसी भी external code से दोनों।

Public members program के बाकी हिस्से के साथ class का external contract बनाते हैं।

उदाहरण: Public Specifier

cpp
#include <iostream>

class Box {
public:
	int size = 5;
};

int main() {
	Box b;
	std::cout << b.size << std::endl; // accessible from outside
	return 0;
}

Private Specifier

private declared members सिर्फ same class के member functions से access किए जा सकते हैं — वे इसके बाहर के किसी भी code से completely hidden हैं।

यह primary mechanism है जो C++ आपको एक object का internal data accidental या unauthorized modification से protect करने के लिए देता है।

उदाहरण: Private Specifier

cpp
#include <iostream>

class Box {
private:
	int size = 5;
public:
	int getSize() { return size; } // only class methods can reach "size" directly
};

int main() {
	Box b;
	std::cout << b.getSize() << std::endl;
	return 0;
}

Getters और Setters

Getters और setters public functions हैं जो अन्यथा-private variables तक controlled access देते हैं: एक getter current value retrieve करता है, जबकि एक setter private field update करने की permission देने से पहले incoming data validate कर सकता है, boundary पर invalid input reject करते हुए।

उदाहरण: Getters and Setters

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

// Define the type Account
class Account {
// Members below are only accessible inside the class
private:
	double balance = 0;
// Members below can be accessed from outside the class
public:
	void setBalance(double amount) { balance = amount; }
	double getBalance() { return balance; }
};

// Program execution starts in main()
int main() {
	Account acc;
	acc.setBalance(100);
	// Print to the console with cout
	std::cout << acc.getBalance() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Protected Specifier

Protected members बाहरी दुनिया के लिए private members जैसे behave करते हैं — completely hidden — लेकिन private members के उलट, वे उन्हें declare करने वाली class से inherit करने वाली किसी भी class के लिए accessible रहते हैं।

यही है जो एक derived class को अपने parent की internal state पर build करने देता है।

उदाहरण: Protected Specifier

cpp
#include <iostream>

class Animal {
protected:
	int age = 1;
};

class Dog : public Animal {
public:
	void showAge() { std::cout << age << std::endl; } // accessible in a derived class
};

int main() {
	Dog d;
	d.showAge();
	return 0;
}

Struct बनाम Class में Default Access

C++ में एक struct और एक class के बीच इकलौता structural फर्क उनका default access level है: class members default से private होते हैं, जबकि struct members default से public होते हैं।

बाकी सब कुछ — inheritance, methods, constructors — दोनों पर identically काम करता है।

उदाहरण: Default Access in Struct vs Class

cpp
#include <iostream>

struct Point { int x; }; // members public by default
class Box { int size; }; // members private by default

int main() {
	Point p;
	p.x = 5;
	std::cout << p.x << std::endl;
	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. बाहर से सीधे private data access करना।
  2. यह भूल जाना कि class members default से private होते हैं।
  3. getters और setters की बजाय public data इस्तेमाल करना।

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.