In C++, generating and modifying an array of objects is a simple procedure. This article discusses the implementation of an array of objects in detail.
Implementing an Array of Objects in C++
To implement this concept, we have to first create an object class that has the objects that we are going to store in the array. Using the C++ functions, we first input the values from the user for each element of the object class and then display them on the terminal. In the main section of the program, an array of objects of the class is created with a constant size that is defined to access the members of the class.
Now, let’s see its practical implementation.
using namespace std;
class paintings
{
char label[30];
int price;
public:
void getdata();
void printdata();
};
void paintings ::getdata()
{
cout << "Label of painting = ";
cin >> label;
cout << "Price of painting = ";
cin >> price;
}
void paintings ::printdata()
{
cout << "Label : " << label <<"\n";
cout << "Price : " << price <<"\n";
}
int main()
{
paintings p[3];
for(int i = 0; i < 3; i++)
{
cout << "Paintings : " <<(i + 1)<< "\n";
p[i].getdata();
}
cout<<endl;
for(int i = 0; i < 3; i++)
{
cout << "Painting Details : " <<(i + 1) << "\n";
p[i].printdata();
}
return 0;
}
The “iostream” library is included in the program initially for the input/output operations that we will perform in the program. The “std” namespace is used to identify the scope of the functions to be employed.
The class paintings are created. The two elements of the class are declared. We have a price with “int” data type which stores the price of the painting. The other is a label with a “char” data type to store the label of the painting which can store a character array of size 30. Then, we use some methods. But before that, we define the access identifier public. Now, whatever functions we declare can be made accessible outside the class because of this public access identifier.
The two-member functions that we have here are getdata() and printdata(). As the name suggests, the getdata() function takes the user input for the previously-declared members of the class, and the printdata() function puts the obtained details on display.
It is now necessary to define these functions outside of the class. We must use the scope resolution operator (::) to define the class’s member functions outside. So, we write the class, scope resolution operator, and the associated function that we are defining. The void is used for the function to not return any value. The first function that we define is the getdata() to get the details from the user. The input/output operations are simply carried out here.
The “cout” is used to display something whereas “cin” takes the input value. Here, we have to take two inputs: the label of the painting to store in a string label and the price of the painting which is stored in the “price” variable. Both are declared in the “object” class. When we do not specify the access identifier, the members of the class are made public by default. We can access these here as they are public.
Then, we move to the other function which is the printdata(). We define the function. The simple “cout” statements are carried out to display the details like the label and price that is provided by the user.
We define both of our functions. Now, we call them in the “main” function to see the output.
The main() section of the program begins. When a class is defined, it does not have any storage. The only defined part of the object is its specification. We need to first generate a class object in order to access and make use of the member functions of that class. If we want to display the data for a single value, the functions are invoked simply with the object name.
In our case, we learn to create an array of objects which means that we want to run the defined functions for multiple values. So, create the array of objects where the syntax is the class name and array name. Within the array brackets, define a constant value like the size of the array. It is required to define the array size at compiling time and it must be a constant value.
The class name is “paintings”, the array object name is “p”, and the maximum limit is set to 3. The objects in the array are then iterated over using the for-loop. The loop starts with a variable “i” = 0, where “I” is any value in the array. The condition is then checked if “i” is less than 3. Every time the condition meets, the control moves inside the loop where it has to print a “Painting” statement and a number that is generated by adding 1 into the current value of “i”.
The p[i].getdata() function is invoked for the ith value of the array. Using the dot (.) operator, the class’s functions can be accessed with the class object and the function call. The getdata() gets the values from the user until the condition of the loop fails, and the loop breaks.
The snapshot shows the details that we input:
Now, we want to put these details that are obtained from the user on display. So, we utilize the for-loop again with the same condition that we previously set. Every time the condition gets “True”, the control moves inside the loop where the printdata() function is called to print the provided data on the screen, with the array object p[i] using the dot operator for the ith element of the array. The loop ends once the condition is no longer true. The control executes the statement that is given outside the loop which returns 0. This indicates that the program is successfully executed.
Here is the data input by the user that is displayed on the terminal:
Conclusion
Creating and using the array of objects in C++ is a very useful yet easy concept. This post demonstrated the practical implementation of using the array of objects in C++. We elaborated on each step that we carried. A class is created, and then we defined the functions of the class outside of that class. We generated a class object in the main() function so that we could access its functions.