I have an assignment to create a gradebook program using C++. I am not allowed to use vector or list and I'm suppose to be using new and delete to dynamically allocate the amount of students and courses needed. Then add grades to them and do other stuff. I can probably do the
other stuff if someone can help me on my first hurdle. So every variables in my class needs to be private. I know how to use set and get and constructors but basic knowledge of both. That goes the same with pointers and classes. I don't know how to access my private variables so I can do whatever I needed to do with them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
class Student {
public:
//showing my knowledge i know how set and get works
void setID(int ID) {student_id = ID; };
int getID() {return student_id; };
private:
int student_id;
int count_student;
string lastName;
string firstName;
int enroll;
int *grades;
};
class Course {
public:
private:
int course_id;
int count_course;
string courseName;
Student *students;
}
class Gradebook {
public:
private:
Course *courses;
};
|
I know C++ doesn't have a version of realloc so what I have been doing in the past is create temp pointer and copy my array to the temporary pointer then have my class pointer to temporary pointer like so.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
Course *temp;
for(i=0; i<count; i++) {
temp[i]=courses[i];
}
course = NULL;
delete []courses;
//do stuff with the temporary pointer
//copy temp to original and delete temp
course = temp;
temp = NULL;
delete []temp;
|
So how do I add information in courses and print it out?
One last thing I have my files separated.
main.h/main.cpp/class.cpp/class.h
main.cpp -> with a switch menu to call functions and function prototypes
main.h -> function calls from main.cpp
class.cpp -> functions from class.h
class.h -> function protoype and definition