class private accessing issues

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
using namespace std;

void populateArray();

class VItem{

public:

void Array();	
	
private:
string word;
string pos;//Part of Speech, not position
};



const int VOCAB_SIZE = 11;
VItem EngVocab[VOCAB_SIZE];


void Array()
{
EngVocab[0].word = "man";
EngVocab[0].pos = "noun";

EngVocab[1].word = "fish";
EngVocab[1].pos = "noun";

EngVocab[2].word = "woman";
EngVocab[2].pos = "noun";

EngVocab[3].word = "caught";
EngVocab[3].pos = "noun";

EngVocab[4].word = "kissed";
EngVocab[4].pos = "verb";

EngVocab[5].word = "is";
EngVocab[5].pos = "verb";

EngVocab[6].word = "strong";
EngVocab[6].pos = "adjective";

EngVocab[7].word = "short";
EngVocab[7].pos = "adjective";

EngVocab[8].word = "red";
EngVocab[8].pos = "adjective";

EngVocab[9].word = "a";
EngVocab[9].pos = "article";

EngVocab[10].word = "the";
EngVocab[10].pos = "article";

}

void populateArray() 
{
Array();
}
//etc.. 

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
Last edited on
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.

http://www.cplusplus.com/doc/tutorial/classes/
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:

1
2
3
4
5
6
void VItem::Array()
{
    EngVocab[0].word = "man";
    EngVocab[0].pos = "noun";
    EngVocab[1].word = "fish";
    EngVocab[1].pos = "noun";


This should probably be a static class function as it doesn't (or shouldn't) require an instance of VItem to invoke.
Last edited on
Thank you both for the replies,
With cire's correction I was able to resolve my issue
Topic archived. No new replies allowed.