Data encapsulation examples
- C++
-
Class
A
has an integer member calledindex
. This member will be hidden from all code in the project except the code for the classA
itself.Class
A
has a functiondoIncrement()
which alters theindex
member. It has another functiongetIndex()
that returns the value of theindex
member.All other code may alter the
index
member of anA
object only by calling thedoIncrement()
function, and can only know the value of theindex
member 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