The arrays can be used as member variables in a class,
like any other array variable.
We can also perform any operations on it.
We know that an array can be of any data type including struct.
Similarly, we can also have arrays of variables that are of the type class.
Such variables are called Arrays of objects.
Consider the following class definition:
In above example
The array manager contains three objects(managers).
Similarly, the foreman array contains 15 objects(foremen) and
the worker array contains 75 objects(workers).
Namely, manager[0], manager[1] and manager[2], of type employee class.
An array of objects is stored inside the memory
in the same way as a multi-dimensional array.
The array manager is represented in below fig.
Note that only the space for data items of the objects is created.
Member functions are stored separately and will be used by all the objects.
• Memory space for objects is allocated when they are declared and not when the class is specified.
• The member functions are created and placed in the memory space only once when they are defined as a part of a class specification.
• All the objects belonging to that class use the same member functions; no separate space is allocated for member functions when the objects are created.
• Only space for member variables is allocated separately for each object.
• Separate memory locations for the objects are essential, because the member variables will be hold different data values for different objects as is shown in below Fig.
- A data member of a class can be qualified as static.
- The properties of a static member variable are similar to that of a C static variable.
- These are:
A static member variable has certain special characteristics.
- It is initialized to zero when the first object of its class is created. No other initialization is permitted.
- Only one copy of that member is created for the entire class and is shared by all the objects of that class, no matter how many objects are created.
- It is visible only within the class, but its lifetime is the entire program.
- For example, a static data member can be used as a counter that records the occurrence of all the objects.
- Static variables are normally used to maintain values common to the entire class.
- Below Program illustrates the use of a static data member.
Like static member variable,
we can also have static member functions.
• A static function can have access to only other static members (functions or variables) declared in the same class.
• A static member function can be called using the- class name (instead of its objects) as follows:
A member function that is declared static has the following properties:
class-name:: function -name;