Dots - What are they?

Here's the code, but I only really need help with line 31; wasn't sure if you needed all the code.
Here it is anyway. (Question continues after code)
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
const int MAX_ITEMS = 10;
    string inventory[MAX_ITEMS];

    int numItems = 0;
    inventory[numItems++] = "sword";
    inventory[numItems++] = "armor";
    inventory[numItems++] = "shield";

    cout << "Your items:\n";
    for (int i = 0; i < numItems; ++i)
	{
        cout << inventory[i] << endl;
	}

    cout << "\nYou trade your sword for a battle axe.";
    inventory[0] = "battle axe";
    cout << "\nYour items:\n";
    for (int i = 0; i < numItems; ++i)
	{
        cout << inventory[i] << endl;
	}

    cout << "\nThe item name '" << inventory[0] << "' has ";
    cout << inventory[0].size() << " letters in it.\n";

    cout << "\nYou find a healing potion.";
    if (numItems < MAX_ITEMS)
	{
        inventory[numItems++] = "healing potion";
	}
    else
	{
        cout << "You have too many items and can't carry another.";
	}
    cout << "\nYour items:\n";
    for (int i = 0; i < numItems; ++i)
	{
        cout << inventory[i] << endl;
	}

	return 0;
}

I just don't understand what the dot (.) is used for. And the whole phrase.size() because every time he explains code like that he says "I called size() and blah blah blah" And I'm thinking "Wait he's called it but that's the first time he's used that in the program. If he's called it should be somewhere else in the program (Right? Or is that not necessarily true)".
Please help, I'm confused.
Some objects have members (member variables and member functions). For example,

1
2
3
4
5
6
7
struct breakfast
{
  int eggs;
  string bacon;
}

breakfast anObject;


How do you access anObject's eggs variable?
anObject.eggs = 7;

. is the syntax used to access a member of an object.

To use your code as an example,
inventory[0] is a specific object of type string.

inventory[0].size(); is calling the member function size of the specific string object inventory[0]
Topic archived. No new replies allowed.