C++ के Access Specifiers
In this page:
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
#include <iostream>
class Box {
public:
int size = 5;
};
int main() {
Box b;
std::cout << b.size << std::endl; // accessible from outside
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Protected Specifier
Protected members बाहरी दुनिया के लिए private members जैसे behave करते हैं — completely hidden — लेकिन private members के उलट, वे उन्हें declare करने वाली class से inherit करने वाली किसी भी class के लिए accessible रहते हैं।
यही है जो एक derived class को अपने parent की internal state पर build करने देता है।
उदाहरण: Protected Specifier
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
- बाहर से सीधे private data access करना।
- यह भूल जाना कि
classmembers default से private होते हैं। - getters और setters की बजाय public data इस्तेमाल करना।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: