C++ में Encapsulation
In this page:
class ClassName {
private:
data_type member;
public:
data_type getMember() { return member; }
void setMember(data_type value) { member = value; }
};
Encapsulation क्या है?
Encapsulation एक class के data variables को इस पर operate करने वाले methods के साथ एक self-contained unit में bundle करने की practice है, इन variables को class के बाहर किसी भी code से direct modification से hide करते हुए।
उदाहरण: What is Encapsulation?
// 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 deposit(double amount) { balance += amount; }
double getBalance() { return balance; }
};
// Program execution starts in main()
int main() {
Account acc;
acc.deposit(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
Getters और Setters
Getters और setters एक object के private data से safely interact करने के लिए इस्तेमाल होने वाला standard public interface हैं: getters field खुद expose किए बिना एक value पढ़ते हैं, और setters direct external assignment allow करने की बजाय एक controlled तरीके से एक value लिखते हैं।
उदाहरण: 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:
double getBalance() { return balance; }
void setBalance(double amount) { balance = amount; }
};
// Program execution starts in main()
int main() {
Account acc;
acc.setBalance(500);
// 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
Data Validation
क्योंकि setters हर write को एक single function से funnel करते हैं, वे incoming data actually store होने से पहले इसे validate करने के लिए एक natural जगह देते हैं, ऐसी values reject करते हुए जो object को एक invalid state में छोड़ देंगी और bugs को उनके source के करीब पकड़ते हुए।
उदाहरण: Data Validation
// 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) {
if (amount >= 0) balance = amount;
}
double getBalance() { return balance; }
};
// Program execution starts in main()
int main() {
Account acc;
acc.setBalance(-50);
// 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
Encapsulation के जरिए Abstraction
Encapsulation abstraction भी enable करता है: complicated internal logic को एक clean public method नाम के पीछे hide करके, आप दूसरे code को एक class की functionality इस्तेमाल करने देते हैं बिना यह समझने — या depend करने — की ज़रूरत के कि वह functionality actually कैसे implement है।
उदाहरण: Abstraction through Encapsulation
// 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;
double calculateInterest() { return balance * 0.05; }
// Members below can be accessed from outside the class
public:
void applyInterest() { balance += calculateInterest(); }
double getBalance() { return balance; }
};
// Program execution starts in main()
int main() {
Account acc;
acc.applyInterest();
// 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
Encapsulation के Benefits
Encapsulation का practical payoff यह है कि यह code को secure, modular, और maintain करना आसान रखता है: क्योंकि बाहरी code सिर्फ public interface को touch करता है, आप बाद में एक class का internal implementation बिना पहले से इसे इस्तेमाल करने वाली किसी चीज़ को तोड़े दोबारा लिखने के लिए free हैं।
उदाहरण: Benefits of Encapsulation
// 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 = 100;
// Members below can be accessed from outside the class
public:
double getBalance() { return balance; }
};
// Program execution starts in main()
int main() {
Account acc;
// 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
- Data members को public बनाना, जो encapsulation को देने चाहिए वे checks bypass करता है।
- एक ऐसा setter लिखना जो invalid values को validate किए बिना accept करता है।
- एक private member का एक non-const reference return करना, जो बाहरी code को इसे सीधे modify करने देता है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: