C++

Array of Objects in C++

An array is a container that contains elements of the same data type. Whereas the objects are the identification of a class or a structure. The objects for the array are declared the same way as they are introduced for the data types that are built in by default. All the elements (objects) that belong to the same class can be combined and declared as an array of objects. This tutorial will contain some elementary examples to explain the concept of an array of objects in the C++ programming language.Note: The user must have some basic knowledge of C++ language and arrays, too. Tools that are required are a Linux environment creating virtual box having an Ubuntu file configured in it. For code writing, we use a text editor. Whereas, for the resultant value, we use the Linux terminal.

How to Declare Arrays of Objects

Syntax

Classname arrayname [size];

First, we use the class name and the name of the object with the size of the array or the number of objects we want to create.

New object [5];

It is declared as an array having 5 objects. An important part of an object declaration in the array is that all the objects in an array should be constructed in the same manner. It is impossible for the declaration aspect that every different object in the same array should be assigned a different value.

Example 1

Moving toward some examples, we will start with a basic example, as we know objects are created for the class or the structures. So here, we will also define a class named function. This class will contain a variable in the private section, whereas two functions are declared in the public part. One function assigns the value that is passed to it, and the second function returns the value after the assignment of the variable.

In the main program, the object of the class is created, wherein the “obj” is the object.

Function obj[4];

This object has a size of 4. As we need to assign values for four times, so we need to use a loop here. A FOR loop is used to send the value to the function that will assign the value to a given variable. Each time, this value will be displayed whenever the function is called. In the loop, a function call is completed with the arrays of an object.

Obj[i].setx(i);

Setx is the name of the function, and the variable containing the current value is sent as a parameter to the function. Similarly, we use the getx function through the FOR loop by following the same method. But this time, the value is received, so the function’s parameters are empty:

Obj[i].getx();

To see the resultant value of the code, save the code with the extension of c and execute the file on the Linux terminal. Use a g++ compiler for the compilation and execution of the code. “object.c” is the name of a file:

$ g++ -o object object.c

$ ./object

Example 2

In this example, we will enter the information of some students regarding their names and marks obtained. This information is then displayed through another function. The basic concept is the same as discussed in the previous example. However, the functionality is explained broadly. At first, the class is declared, which contains two variables. Instead of “cin”, we use a built-in function of strings “getline” to get the value from the main program.

Getline( cin, name);

Both the functions use this feature to get the value separately in the function. The third function displays both the values collectively. It all depends on the usage of objects that is done in the main program.

So going towards that part, the first step is the object creation for any class. A loop is an essential part of assigning values, and getting back as an array contains many elements that cannot be accessed manually in less time. So by using a FOR loop, we have sent values. The variable used with the object identifies the index number where the value is entered in the array:

St[i].getname();

Now to see the result, run the program on the Linux terminal. When the program is executed, you will see two statements are displayed that demand data from the user. This data is directly saved in the variables that are then assigned to the array at a specific point:

When you enter the name along with the marks and the process is repeated three times, we have created an array of 3 objects. After the third entry, you will see that all the data entered is displayed with the sequence. This is only done by the objects we have created as an array of the class:

Example 3

This example is quite different from the previous ones, as the object declaration process is not done directly in the main program. Instead, another function is used for this purpose. So first, we will discuss the main part of the source code. A function call is done.

Arrayofobjects();

After that, when the control goes to the function, the objects of a class are created with an array of 2 objects of one type and a single object of the second type: Student s[2], s1:

A variable declared here receives the value from the function we have used later in the program. This is done by the arrays of an object of one type. Similarly, a function pos() is called by the second object.

S[pos].putdata();

Now, we will describe the class here.

A class contains 3 variables that will store the value that is passed from the main program with the help of the FOR loop:

We have used a separate function here to search a student’s data having the maximum numbers among all the students whose data is provided here. As we have created only 2 objects. So for the sample, we will take the details of only 2 students, and then their comparison will be done. A function will be created to pass the array of objects. From the snap attached below, you can see the functionality as we have used a variable:

Obj[0]. Total will contain the current number of students:

For the comparison method, “for loop” is used. In that loop, the current value of the max variable is compared with the next value entered by the user. If that value is greater than the value present in the variable at that time, then the new value is assigned to the max variable:

Max = obj[i].total;

In the end, a “putdata” function is used to display all the resultant data of the respective student.

Conclusion

This article on “array of objects” contains the discussion of the creation of objects in the form of arrays in the C++ programming language in the Linux environment. We have used some simple examples to increase the knowledge of the end-user regarding the concept of objects in the array. Objects are the special features to access the class. The array of the object can access the class more than one time but in a similar manner. For differentiation, use a separate object. We hope you found this article helpful. Check out Linux Hint for more informative articles.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.