Hey all,
I'm currently learning about private/public parts of a class, and have some basic understanding about them.
While making a simple translator program, I've made a sort of dictionary which would be accessed by functions to figure out what certain words are (nouns, adjectives, etc). The dictionary would be an array of a class, where the members of the class that need to be accessed by the dictionary ( strings word and pos in VItem) must remain private.
The issue I get is, when compiling, I am told .pos and .word are private and cannot be accessed, but I feel that since I've made an array of that same class, they should be accessible? How should I go about making them accessible, without removing them from private?
Thanks in advance for your time
The thing about private variables is that they are only available to be seen inside other members of that class or friends (such as member functions and friend functions). Member variables are typically made private to prevent them from being modified outside of where they should be.
You could add a member function called "setWord" and "setPos" that take a string as an argument and inside the definition of that function is where you'll be able to actually change the value of those variables.
The thing about member functions is that you can have non-member functions outside the class with the same name. And those non-member functions won't have the same access to private variables as the member functions do. So, maybe you want to define the actual member function instead of a non-member function: