-
Creating Objects + Array
I have a class called "Student". It has members such as first name, last name, gpa...etc. In this program, i need to create and array, and be able to add objects into the array. Now, i know how to create an object before running the program:
Code:
Student Stud1("Williams","John", 4.0);
however, i am not sure how to create an object whenever the user is using the program. Using a struct would be something like:
Code:
new Student;
cin Student.First_Name;
but i'm not sure how to do it with objects.
-
You might consider using a std::vector<Student> for an 'array' of Student objects.
-
I'm not familiar with that. I tried creating an array:
Code:
Student Array1[10];
but it came up with an error: 'Student' : no appropriate default constuctor available.
"Student" is a derived class of the base class "Individual".
Here is my code for the constructors:
http://cboard.cprogramming.com/showthread.php?t=88122
I'm not sure why it's not letting me create the array.
-
Because those classes don't have default constructors. That means that the compiler cannot create 10 objects for the array because it needs strings to call the constructor for each object.
You could create a default constructor and an initialization function, then create the array and set those variables with the init function. You could also use a vector, which lets you use the constructors you have. However, that won't help much either because you would still have to give all 10 Students the same initial data.
So I would create a default constructor (maybe by just giving default empty string values for your strings. Then set the data later.