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? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

8th Mar 2016, 6:18 PM
Josie Thompson
Josie Thompson - avatar
2 Answers
+ 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.
4th May 2016, 7:17 PM
Zahid Ali
Zahid Ali - avatar
+ 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.
5th May 2016, 5:15 AM
Svarog