. Advertisement .
..3..
. Advertisement .
..4..
As far as you know, the C++ programming language is very well-known in software development technology. From the beginning of C++, you need to know how to declare a variable and how to use it in programming. You can have trouble when using an array or an array of objects in C++. If you are interested in arrays of objects C++ and want to know what it is and how to use it in your program. Let’s continue with our blog to discover!
What is an array of objects in C++?
A familiar thing we all know is the concept of arrays. An array is declared as a collection of elements of the same data type. Similar to arrays of data, we have arrays of objects in C++. It is an array of a type class, which contains the objects of class as its individual factors. Therefore, an array of a class type is also considered an array of objects in C++.
How to declare an array of objects in C++?
Arrays of objects can also be declared and used as arrays of variables of regular type. We have 2 type: Declare a static array of objects and Dynamic array declaration with pointers.
1. Declare a static array of objects
Arrays of objects are declared according to the syntax:
<Class name> <Array variable name>[<size>];
For example:
Phone Phones[10];
Is to declare an array of 10 objects of the same type Phone.
Note:
- It is possible to declare a static array of objects without declaring the array length, this is often used when the exact length of the array is unknown: Phone Phones[];
- To declare a static array of objects, the corresponding class must have a constructor with no parameters. Because when declaring arrays, it is equivalent to declaring an array of objects with a constructor that takes no parameters.
2. Declaring dynamic arrays with pointers
An array of objects can also be declared and dynamically allocated via the object pointer as follows:
<Class Name> *<Dynamic array variable name> = new <Class name>[<Array length>];
For example:
Phone *phones = new Phone[10];
After being used, the dynamic array of objects also needs to free memory:
delete [] <Dynamic array variable name>;
For example:
Phone *phones = new Phone[10]; // Dynamic declaration and allocation
… // Use dynamic array variables
delete [] phones; // Freeing the memory of dynamic array
In short, we use the class name and name for the array with the size of the array or the number of objects we want to create, following the syntax:
class_name array_name [size] ;
Now, see the below example. We created an array for a class type, it is named MyClass
, this array had a name that is obs
and the size had 4 elements.
#include <iostream>
class MyClass {
int x;
public:
void setX(int i) { x = i; }
int getX() { return x; }
};
void main()
{
MyClass obs[4];
int i;
for(i=0; i < 4; i++)
obs[i].setX(i);
for(i=0; i < 4; i++)
cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n";
getch();
}
Save the code and run it. This console screen will be displayed results for 4 elements with the value we set:
Output:
obs[0].getX(): 0
obs[1].getX(): 1
obs[2].getX(): 2
obs[3].getX(): 3
Conclusion
This blog was shown about the array of objects in C++ and the way to declare it in your program. An example written in this post will help you understand more about it. We hope this post is useful for you! We would appreciate it if you share your thoughts with us via the comment box below! Thank you!
Leave a comment