Here is my problem. I have created a class student which is to add/remove student names from a dynamic array student_name. Suppose each student will also has a list of courses created with linked list. So, I created a struct course but I don't know how to link a course list to each student.
Could someone please show me how to link the course to each student?
Is it better to create another course class and implement it with link list?
Are there any better ways?
Thank you in advance.
========
student.h
========
Code:
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
namespace jk1998_A
{
class student
{
public:
static const int DEFAULT_CAPACITY;
student(int initial_capacity = DEFAULT_CAPACITY);
void reserve(int new_capacity);
void insert(const std::string& entry);
bool erase_one(const std::string& target);
struct course
{
std::string name;
double GPA;
double credit;
course* node;
};
private:
std::string* student_name;
int capacity;
int used;
};
}
#endif
=========
implet_student.cpp
=========
Code:
#include <string>
#include <algorithm>
#include "student.h"
using namespace std;
using namespace jk1998_A;
const int student::DEFAULT_CAPACITY = 20;
student::student(int initial_capacity)
{
student_name = new string[initial_capacity];
capacity = initial_capacity;
used = 0;
}
void student::reserve(int new_capacity)
{
string* larger_array;
if (new_capacity == capacity)
return;
if (new_capacity < used)
new_capacity = used;
larger_array = new string[new_capacity];
copy(student_name, student_name + used, larger_array);
delete [] student_name;
student_name = larger_array;
capacity = new_capacity;
}
void student::insert(const string& entry)
{
if (used == capacity)
reserve(used + 10);
student_name[used] = entry;
++used;
}
bool student::erase_one(const string& target)
{
int index = 0;
while ((index < used)&&(student_name[index].compare(target)!=0))
++index;
if (index == used)
return false;
--used;
student_name[index] = student_name[used];
return true;
}