Encapsulation in C++ is the practice of hiding the internal implementation details of a class or data structure, while providing a well-defined public interface for interacting with it. It is one of the fundamental concepts of object-oriented programming (OOP) and is used to improve the reliability, maintainability, and security of software.
In C++, encapsulation is achieved through the use of access specifiers, which are keywords that determine the visibility of class members.
By default, all members of a class are private unless otherwise specified. This means that unless you declare a member as public or protected, it cannot be accessed from outside the class.
Encapsulation allows you to enforce data abstraction, which is the separation of a class’s interface from its implementation details. This makes it easier to modify the implementation of a class without affecting the code that uses it, improving the overall maintainability of the program. It also allows you to control access to the internal state of a class, preventing external code from inadvertently corrupting its data or violating its invariants.
#include
class BankAccount {
private:
std::string accountNumber;
double balance;
public:
// Public methods for interacting with the account
void deposit(double amount) {
balance += amount;
}
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}
double getBalance() const {
return balance;
}
void setAccountNumber(std::string newNumber) {
accountNumber = newNumber;
}
std::string getAccountNumber() const {
return accountNumber;
}
};
int main() {
BankAccount myAccount;
myAccount.setAccountNumber("1234567890");
myAccount.deposit(500.0);
std::cout << "Current balance: " << myAccount.getBalance() << std::endl;
myAccount.withdraw(200.0);
std::cout << "Current balance: " << myAccount.getBalance() << std::endl;
std::cout << "Account number: " << myAccount.getAccountNumber() << std::endl;
return 0; }
In this example, we have a BankAccount class that has two private members, accountNumber and balance. These members are not directly accessible from outside the class, but we provide public methods to interact with the account, such as deposit, withdraw, getBalance, setAccountNumber, and getAccountNumber.
By encapsulating the accountNumber and balance members as private, we ensure that external code cannot modify them directly. Instead, they must go through the public methods provided by the class. This allows us to maintain the integrity of the account data and prevents external code from making unauthorized changes.
The getBalance method provides read-only access to the balance, ensuring that external code cannot modify the balance directly.
The setAccountNumber and getAccountNumber methods allow external code to set and retrieve the account number, but they do not allow direct access to the accountNumber member itself.
Overall, encapsulation allows us to maintain control over the internal state of the BankAccount class and provide a well-defined interface for interacting with it.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.