Data encapsulation examples
- C++
-
Class
Ahas an integer member calledindex. This member will be hidden from all code in the project except the code for the classAitself.Class
Ahas a functiondoIncrement()which alters theindexmember. It has another functiongetIndex()that returns the value of theindexmember.All other code may alter the
indexmember of anAobject only by calling thedoIncrement()function, and can only know the value of theindexmember by calling thegetIndex()function. - C
You can write a perfectly well encapsulated module in C.
- Perl, Python, JavaScript
These languages really don't encapsulate data well. You can give member data a funny name and plead with users not to alter it, but really all member data in these languages is exposed.
- FORTRAN 90
Yes.
- FORTRAN
Not really meaningful in this language