![]() |
VOOZH | about |
An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They can be used to store the collection of primitive data types such as int, float, double, char, etc of any particular type. To add to it, an array in C/C++ can store derived data types such as structures, pointers, etc. Given below is the picture representation of an array.
Example:
Let's consider an example of taking random integers from the user.
When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.
Syntax:
ClassName ObjectName[number of objects];The Array of Objects stores objects. An array of a class type is also known as an array of objects.
Example#1:
Storing more than one Employee data. Let's assume there is an array of objects for storing employee data emp[50].
Below is the C++ program for storing data of one Employee:
Let's understand the above example -
This program can take the data of only one Employee. What if there is a requirement to add data of more than one Employee. Here comes the answer Array of Objects. An array of objects can be used if there is a need to store data of more than one employee. Below is the C++ program to implement the above approach-
Output:
Explanation:
In this example, more than one Employee's details with an Employee id and name can be stored.
Example#2:
Output:
Constructor Run Constructor Run Constructor Run Constructor Run Constructor Run