简体   繁体   中英

c++ dynamic classes without using vector

So I happened to finish my homework program but today during lecture my good ol professor told us we are not allowed to use STL as in vectors or list to create our database. We were also told we need all our variables private. I was doing this program completely wrong. This is what I have so far.

in class.h

class Student {
   private:
    string last;
    string first;
    int student_id;
    int enroll_id;
    int *grades;
}

class Gradebook {
   public:
    Gradebook();
    void newCourse(Gradebook *info);
   private:
    string name;
    int course_id;
    int count_course;
    int enroll;
    Student *students;
   public:
    //Constructor
}

I know I can access private members of Gradebook by using a constructor so I can set every member in my Gradebook .

function to create a newCourse

Gradebook::Gradebook() {

  students = new Student;
  course_id=0;
  count_course=0;
  enroll = 0;
}

Gradebook::newCourse(Gradebook *info) {

  int i, loop=0;

  cout << "Enter Number of Courses: ";
  cin >> loop;

  info = new Gradebook[loop];

  for(i=0; i<loop; i++) {

    cout << "Enter Course ID: ";
    cin >> info[info->count_course].course_id;

    cout << "Enter Course Name: ";
    cin >> info[info->count_course].name;

    info->count_course++

  }
}

So Courses are now sets. Since the variables in Student are private. I can't just use a pointer to the variables to access them. Can someone show me how to do this?

class Student {
   private:
    string last;
    string first;
    int student_id;
    int enroll_id;
    int *grades;
  public:
    string &getLast(){ return Last; }
    ...
    ...
};

Call Student::getLast() when you need to access your last variable etc

or
void setLast(string sLast){last = sLast;} for writing and
string getLast(){return last;} for reading

And example of dynamic array:

    struct Student;
int Count = 0;
Student *Students = nullptr;

int AddStudent(Student nStudent)
{
    Student *Buffer = new Student[Count + 1];
    for (int a = 0; a < Count; a++)
        Buffer[a] = Student[a];
    Buffer[Count] = nStudent;
    if(Students)
        delete[] Students;
    Students = Buffer;
    return ++Count -1
}
void RemoveStudent(int Index)
{
    Student *Buffer = new Student[Count - 1];
    for (int a = 0; a < Index; a++)
        Buffer[a] = Students[a];
    for (int a = Index; Index < Count - 1; a++)
        Buffer[a] = Students[a - 1];
    if (Students)
        delete[] Students;
    Students = Buffer;
}

Ok I didnt know how to ask this question but I actually answered it. But I want everyones opinion on my method.

class Student {
  public: 
   void setID(int ID){student_id = ID; };
   int getID(){return student_id);
  private:
   string last;
   string first;
   int student_id;
   int enroll_id;
   int *grades;
};

class Gradebook {
 public:
  Gradebook();
  void newCourse(Gradebook *info);
  void newStudent(Gradebook *info);
private:
  string name;
  int course_id;
  int count_course;
  int count_student;
  int enroll;
  Student *students;
public:
//Constructor
}

void Gradebook::newStudent() {

    int i, loop=0;

int student=0;
string lastName;

cout << "Enter number of students: ";
cin >> loop;

for(i=0; i<loop; i++) {

    cout << "Enter Student ID: ";
    cin >> student;

    info->students[info->count_student].setID(student);

    cout << "Enter Last Name: ";
    cin >> lastName;

    info->students[info->count_student].setLast(lastName);

    info->count_student++;

}
}

Is there anything wrong of doing it this way?

edit: You can't use 'Student *students' for your container for multiple Students...

You could use your own lists. Something like

struct Participant{ // or class (and maybe more clever name)
    Student *student;
    Participant *prev;
    Participant *next;
};

You have to do little pointer-acrobatics, but maybe that's the idea for this exercise.. And like in previous answer, use get and set functions in your Student class.

Ok, I'm sorry but your code is a mess... I can't do this homework for you, but here is few tips that came in mind

  1. Are you sure that it wouldn't be easier to make different classes for Student, Course and Gradebook? I don't know your homework specifications, so I can't be sure what it is that your program should actually do.

  2. You can not use int *grades to store all of one students grades. Same goes for Student *students. You can not access iterate *students like an array students[i].something() If you use some help class(or struct) like Participant, you have to find right student by iterating in loop. Notice, that you have store 'first' participant inside your class and 'int students' keep in your student count (also inside your class)

     Participant *current = first; int id = Id; // Id is from function/method call for(unsigned int i = 0; i < students; ++i){ if(current->getId()== id){ // do something and break; } else if(current->next != NULL){ // test this just in case current = current->next; } else break; // something went wrong } 

It might be good idea to store all students in one list (that you make) and use pointers in Course or in Gradebook or where ever... If you know 'static' here is a hint

    class Student{
        public:
           .   // get and set functions
          static Student* getStudent(int id); // return NULL if no student with id
        private:
          static Student *first;
          static Student *last;
          static unsigned int counter; // helps with loops, but you can always use next != NULL
          Student *next;
          Student *prev;
    }

Put this inside the Student constructor with first student that you create you set first and last to point him/her, and next and prev to NULL. After that, always when you create a new student you set

    this->prev = last;
    last->next = this;
    last = this;
    this->next = NULL;
  1. Do not implement program logic inside class. Do it in main() or in some other function, but i think main() is fine for now. If you need to add new student in gradebook, ask all the necessary info in main() and make some function like Gradebook::newStudent(int &Id, string &last_name){ // store student info in gradebook }

Usually these homework programming exercises don't have be too fancy or streamlined or fully optimized. So don't overdo them ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM