Article by: Manish Methani
Last Updated: November 14, 2023 at 2:04pm IST
Introduction
In C++, you can create arrays of objects that are initialized with a constructor. This allows you to allocate and manage a dynamic array of objects with specific properties. In this step-by-step guide, we will walk you through the process of dynamically declaring an array of objects with a constructor in C++, complete with an example code to illustrate the concept.
Table of Contents:
Start by including the necessary headers for C++ programming:
#include <iostream> #include <vector>
MyObject
. This class represents the type of object that we want to store in the dynamic array. The class has the following components:
id
and name
, and initializes the object's properties.display()
: This function is marked as const
to indicate that it does not modify the object. It is responsible for displaying the object's properties using std::cout
.class MyObject { public: MyObject(int id, std::string name) : id(id), name(name) {} void display() const { std::cout << "ID: " << id << ", Name: " << name << std::endl; } private: int id; std::string name; };
main()
function, we declare a variable arraySize
and set it to 5, indicating the desired size of the dynamic array.std::vector
named objectArray
to store instances of the MyObject
class. This is a dynamic array that can grow or shrink as needed.int main() { int arraySize = 5; std::vector objectArray;
for
loop to populate the dynamic array. For each iteration, we create a new MyObject
instance with a unique id
and a name constructed as "ObjectX," where X is the loop iteration.push_back
function is used to add the newly created MyObject
to the objectArray
.for (int i = 1; i <= arraySize; i++) { objectArray.push_back(MyObject(i, "Object" + std::to_string(i))); }
for
loop to iterate through the objectArray
.const
qualifier ensures that the display
function is called on a constant object, preventing any modification of the objects within the loop.display
function for each MyObject
instance, which prints the object's properties.for (const MyObject& obj : objectArray) { obj.display(); }
return 0; }
In summary, this code demonstrates how to dynamically declare and initialize an array of objects using a std::vector
. The MyObject
class represents the objects to be stored in the array, and the code shows how to create, populate, and display the objects within the dynamic array.
#include <iostream> #include <vector> class MyObject { public: MyObject(int id, std::string name) : id(id), name(name) {} void display() const { std::cout << "ID: " << id << ", Name: " << name << std::endl; } private: int id; std::string name; }; int main() { int arraySize = 5; std::vector objectArray; for (int i = 1; i <= arraySize; i++) { objectArray.push_back(MyObject(i, "Object" + std::to_string(i))); } for (const MyObject& obj : objectArray) { obj.display(); } return 0; }
ID: 1, Name: Object1 ID: 2, Name: Object2 ID: 3, Name: Object3 ID: 4, Name: Object4 ID: 5, Name: Object5
Dynamically declaring an array of objects with constructors in C++ allows you to create and manage arrays of objects with specific properties. It provides flexibility and efficiency when working with objects in your programs.
Marking the display function as const in the MyObject class indicates that the function does not modify the objects state. It allows you to call the function on constant objects, ensuring that the objects are not changed when displaying their properties.
Using std::vector in C++ for dynamic arrays of objects offers safety, versatility, and automatic memory management. It simplifies dynamic array creation, resizing, and element management, making it a preferred choice for many C++ programmers.
To include additional properties in the MyObject class, you can add member variables in the class definition and modify the constructor to initialize these properties. Make sure to update the display function to display the new properties as well.