How to make array of objects of a class but with user giving input?

Viewed 72

If the user gives the input than it should be added into the array of objects and how can I keep track of that array in object oriented programming in C++?

Until and unless user don't give a input there would be one or no input in the array of objects. If user gives input than the array of objects would have one and than other objects can be added in the array of object of that respective class.

1 Answers

So you want to create an array of objects, then according to the user input, you will add this info into the array, you have these work arounds:

  1. Use std::vector, which can dynamically change in size (The best work around) as mentioned by @theWiseBro
  2. Use a big array which you know that nobody can exceed its capacity (This exhausts your RAM).

There are others, but these are the best, you can create an array and keep track of it, once you are going to exceed it make a new array and copy all of the previous elements/objects/data into the new array.

I'd rather use std::vector since you're using C++

Related