← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 7 of 11

C++ में Encapsulation

Encapsulation एक object का data private रखना है और दूसरों को इसे सिर्फ safe methods के जरिए इस्तेमाल करने देना, insides छिपाने वाली एक vending machine की तरह। यह data को accidental changes से protect करता है।
Syntax
cpp
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?

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 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;
}

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

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:
	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;
}

Data Validation

क्योंकि setters हर write को एक single function से funnel करते हैं, वे incoming data actually store होने से पहले इसे validate करने के लिए एक natural जगह देते हैं, ऐसी values reject करते हुए जो object को एक invalid state में छोड़ देंगी और bugs को उनके source के करीब पकड़ते हुए।

उदाहरण: Data Validation

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) {
		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;
}

Encapsulation के जरिए Abstraction

Encapsulation abstraction भी enable करता है: complicated internal logic को एक clean public method नाम के पीछे hide करके, आप दूसरे code को एक class की functionality इस्तेमाल करने देते हैं बिना यह समझने — या depend करने — की ज़रूरत के कि वह functionality actually कैसे implement है।

उदाहरण: Abstraction through Encapsulation

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;
	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;
}

Encapsulation के Benefits

Encapsulation का practical payoff यह है कि यह code को secure, modular, और maintain करना आसान रखता है: क्योंकि बाहरी code सिर्फ public interface को touch करता है, आप बाद में एक class का internal implementation बिना पहले से इसे इस्तेमाल करने वाली किसी चीज़ को तोड़े दोबारा लिखने के लिए free हैं।

उदाहरण: Benefits of Encapsulation

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 = 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;
}
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. Data members को public बनाना, जो encapsulation को देने चाहिए वे checks bypass करता है।
  2. एक ऐसा setter लिखना जो invalid values को validate किए बिना accept करता है।
  3. एक private member का एक non-const reference return करना, जो बाहरी code को इसे सीधे modify करने देता है।

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.