-1

I would like to know if it's possible to generate multiple objects from a class (person for example)

without having to name each object :

    person p1(arguments);

the number of instances is unknown (the user of my program has the choice to add as many instances as he likes but each one with a different name and phone number)

thanks

  • 6
    Using e.g. a [`std::vector`](https://en.cppreference.com/w/cpp/container/vector)? As in `std::vector persons(3);` to create a vector of three (default constructed) `person` objects. – Some programmer dude Apr 29 '20 at 23:50
  • Or `std::array persons;`, if the number of objects is known at compile-time. – Remy Lebeau Apr 29 '20 at 23:53
  • @Someprogrammerdude I can't use default contructors – Abderrahmane Bouhya Apr 29 '20 at 23:53
  • 4
    Then `std::vector persons(3, person(arguments, ...));` to create a vector of three identically constructed `person` objects. Or `std::vector persons = { person(args1, ...), person(args2, ...), person(args3, ...) };` to create a vector of three different `person` objects. – Some programmer dude Apr 30 '20 at 00:01
  • @nanofarad can you give me an example of code to do this ? or link me where to search for it . – Abderrahmane Bouhya Apr 30 '20 at 00:20
  • Why can't you use default constructors? Is it because the `person` object not default-constructible? What else can you tell us about this `person` object? Is it copyable? Needs more detail. – Raymond Chen Apr 30 '20 at 00:21
  • @Someprogrammerdude the problem here is that the user can specify the name and the phone number of that person everytime he creates a person object – Abderrahmane Bouhya Apr 30 '20 at 00:21
  • 1
    So? Then create an empty vector (`std::vector persons;`) and dynamically add objects as needed (`persons.emplace_back(name, phone_number, other_constructor_arguments);`). You really should spend some time doing research on things like the standard library and what it can do for you. [A few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and [a good reference site](https://en.cppreference.com/w/cpp) should go a long way. – Some programmer dude Apr 30 '20 at 00:23

2 Answers2

0

Like the comments has stated, you could store the objects in an array. That way they have no "name", but still act as a normal object.

And since you want to change the number of objects you have, I suggest using a vector.

std::vector<person> people;

people.push_back(person(arguments));
Alex
  • 462
  • 1
  • 7
  • 19
0

you can store in a vector ie

vector person_vec

then for a person to insert into it you could do something like

person person;
for (int i =0;i<n;i++)   // n is the number of times the user wants to input
{
  person.enterName(); // function to insert name
  person.enterNumber(); // function to insert number
  person_vec.push_back(person); // pushing all info into the vector

}

// then create a print function to print the details and loop over the vector to print
for (int i=0;i<person_vec.size();i++)
{
   cout << person_vec[i].printDetail()<<endl;
}
william_
  • 1,131
  • 6
  • 20