What purpose does a object-list have?

Hello :)
Currentely learning c++ in "C++ A Beginner’s Guide by Herbert Schildt".

At module 8 it shows the general form of a class

1
2
3
4
5
6
class class-name
{
private data and functions
public:
public data and functions
} object-list;


Was looking around on google a little , but couldn't find a code which actually uses the "object-list", they all end classes without it.

So my question would be: What purpose does it have? Why use it?

Thanks on advance :)
Basically you are instantiating your objects as part of the class definition rather than creating them somewhere else. An example would be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Player
{
public:
	Player()
	{
	}
	~Player()
	{
	}
	int getHitPoints()
	{  return hitPoints;  }
private:
	int hitPoints;
} player1, player2, computer;


In this example we have three objects created that can now be used, player1, player2, and computer. We could also have done it this way where the objects are created elsewhere:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Player
{
public:
	Player()
	{
	}
	~Player()
	{
	}
	int getHitPoints()
	{  return hitPoints;  }
private:
	int hitPoints;
};

int main()
{ 
     Player player1;
     Player player2;
     Player computer;
     return 0;
}


I personally don't find much use for the object list unless I know ahead of time I'm going to need a certain number of objects created. The same rules apply to structs. Make sense?
Last edited on
Hmm ok , might be using the objects lists , kinda like the fact that it can keep main() smaller ^^

Thanks for the explanation ^^
Topic archived. No new replies allowed.