0
Is there a way in c++ to directly access balance as a getter without having to do a function call but only be able to modify it through related methods?
for example in Python you would do something like class BankAccount: def __init__(self): self._balance = 0 @property def balance (self): return self._balance def deposit(self, amount): self._balance += amount def withdraw(self, amount): self._balance -= amount you could then access a read-only value of balance by some_account = BankAccount() print some_account.balance I.e. without having to call a get_balance function after reading ahead: they use a getter function, but is there anyway to access l it like a property?
2 ответов
+ 2
Encapsulate and hide literally means access control. But in Python, you can still access the methods or attributes marked as private. Functionality and understanding of Object Oriented programing is the same in many ways.
+ 1
C++ does the same thing you would use some_account.getbalance which would go and get the balance for you. Balance as a variable is defined as private in class and only accessible through a getter method.