2

I used to program in C and I moved to C++ after learning OOP. I am trying to use the things I learned in my new project.

I am working on a school management project. If I want to add 1 student, I can use the constructor:

Student student1 = Student(string firstname, string lastname, int Age, char Gender, int group);

That would work fine in adding 1 student, but if I want to add more then 1 student using a loop, how can I do it?

In C, we use a struct to do this:

struct student{...};
struct student students[n] //n: number of student i want to add
for(int i=0;i<n;i++){
   students[i].name = ...; 
}

Is there a way to achieve this in OOP (I am new in using OOP)?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
AlCo2
  • 45
  • 8
  • 2
    You're very close with your code. Instead of `students[i].name = ...;`, use `students[i] = student(Put_Your_Constructor_Arguments_Here)` – 0liveradam8 Dec 10 '22 at 00:15
  • 2
    In C you are using an *array* of structures (not just a structure). In C++ you would use an array of a particular *class* (i.e. a Student class). See [How do I declare an array with a custom class?](https://stackoverflow.com/q/8579694/4424636). So this is probably a duplicate of that question. – Greenonline Dec 10 '22 at 00:18
  • 2
    Unless `n` is known at compile-time, this won't work in C++. If `n` is a runtime value, use a vector. – Silvio Mayolo Dec 10 '22 at 00:18
  • 1
    @SilvioMayolo so i can do something like : vector students; – AlCo2 Dec 10 '22 at 00:22
  • 1
    The linked question also shows how to use a vector. – Greenonline Dec 10 '22 at 00:24
  • @Greenonline I readied everything in the link, that's only worked for me using a vector. – AlCo2 Dec 10 '22 at 00:42
  • 2
    Sounds about right. The link demonstrates use of an array with an initializer list and explains why you need to initialize all of the elements of the array when you define the array. basically, you can't do this with an array of a class without a default (no arguments) constructor because you need to invoke the constructor on all of the items in the array. Which means you have to already know what data is going in the array. Which you probably don't. Big take-away: Arrays are ancient tech, really simple, and very feature limited. In C++ there is almost always a better tool than an array. – user4581301 Dec 10 '22 at 00:52
  • @user4581301: To be fair, part of the reason that arrays are so bad is that `std::vector` has long been blessed with the magic, otherwise unavailable ability to create arrays without creating all their elements (immediately). In C++20 `new` can finally do this, but that’s still really relevant only for the **rare** person implementing something like `std::vector`. – Davis Herring Dec 10 '22 at 18:31

1 Answers1

2

Yes, you can do the same kind of loop in C++. Except, you need to use new[] to create the array, as variable length arrays are not part of standard C++.

class student{...};

student *students = new student[n];
for(int i = 0; i < n; ++i){
   students[i].name = ...; 
}
...
delete[] students;

A better option is to use std::vector instead and let it handle the array management, eg:

#include <vector>

class student{...};

std::vector<student> students(n);
for(int i = 0; i < n; ++i){
   students[i].name = ...; 
}
...

Alternatively:

#include <vector>

class student{...};

std::vector<student> students;
students.reserve(n);
for(int i = 0; i < n; ++i){
   students.push_back(Student(params...));
   // or: students.emplace_back(params...);
}
...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Side note: `student *students = new student[n];` and `std::vector students(n);` must still construct `n` `student`s, so you'll still run into the same "needs a default constructor" problem – user4581301 Dec 10 '22 at 23:48
  • @user4581301 the compiler will typically auto-generate a default constructor for you, unless other code in the class prevents it from doing so. But, in any case, it is possible to use `vector` with classes that don't have a default constructor. I have updated my answer with an example of that. – Remy Lebeau Dec 11 '22 at 02:45