Need help implementing a class

This is a discussion on Need help implementing a class within the C++ Programming forums, part of the General Programming Boards category; I am writing a program for keeping a course list for each student in a college. The information about each ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Need help implementing a class

    I am writing a program for keeping a course list for each student in a college. The information about each student should be kept in an object that contains the student's name and a list of courses completed by the student.

    * must use a DYNAMIC ARRAY for storing students
    * must use LINKED LISTS for storing courses (1 course list for each student)

    I have written a course class which is a linked list to store the courses with name, credit and GPA; a student class to contain a student's name and a course list. I am writing the third class: school which should be a dynamic array of student.

    The prgram should prompt for user inputs for student name, course name, course GPA, and course credit. All these will be stored in student_list. I kind of wrote a student_insert function, but it doen't seem to work. Could someone please help me out in writing a school member function to insert a student into the student_list with the user inputs? Thank you in advance.

    ========
    student.h
    ========
    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    #include <string>
    
    namespace jk1998_A
    {
          class course
          {     
                public:
                       course(const std::string& name=NULL, const double& GPA=0, const double& credit=0, course* link = NULL);
                       void set_name(const std::string& name) {course_name = name;}
                       void set_GPA(const double& GPA) {course_GPA = GPA;}
                       void set_credit(const double& credit) {course_credit = credit;}
                       void set_node(course* new_node) {node=new_node;}
                       course* link() {return node;}
                       const course* link() const {return node;}
                       std::string get_name() const {return course_name;}
                       double get_GPA() const {return course_GPA;}
                       double get_credit() const {return course_credit;}
                       
                       
                       
                private:
                        std::string course_name;
                        double course_GPA;
                        double course_credit;
                        course* node;      
          };    
          
          void list_head_insert(course*& head_ptr, const std::string& name, const double& GPA, const double& credit);
                
          course* list_search(course* head_ptr, const std::string& target); 
          
          const course* list_search(const course* head_ptr, const std::string& target);
          
          void list_remove(course* previous_ptr); 
          
          void list_head_remove(course*& head_ptr);
              
          void list_insert(course* previous_ptr, const std::string& name, const double& GPA, const double& credit);
              
          void list_copy(const course* source_ptr, course*& head_ptr, course*& tail_ptr);    
          
          void list_clear(course*& head_ptr);
              
              
              
          class student
          {
                public:
                        student(const std::string& name=NULL);
                        student(const student& source);
                        ~student();
                        void operator =(const student& source);
                        void set_student_name(const std::string& name) {student_name=name;}
                        std::string get_student_name() {return student_name;}
                        
                        void course_insert(const std::string& name, const double& GPA, const double& credit);
                        bool course_erase(const std::string& target);
                        
                        
    
                private:
                        std::string student_name;
                        course* head_ptr;
                        int many_nodes;
                              
                
          };    
          
          
          
          class school
          {
                public:
                       static const int DEFAULT_CAPACITY;
                       
                       school(int initial_capacity = DEFAULT_CAPACITY);
                       school(const school& source);
                       ~school();
                       void reserve(int new_capacity);
                       void student_insert(const student& entry);
                       
                private:      
                        student* student_list;
                        int used;
                        int capacity;
                
          };
    }
    
    #endif

    ========
    implement.cpp
    ========
    Code:
    #include <string>
    #include <algorithm>
    #include "student.h"
    
    using namespace std;
    
    namespace jk1998_A
    {
    /******COURSE*******/
          course::course(const string& name, const double& GPA, const double& credit, course* link)
          {
                course_name = name;
                course_GPA  = GPA;
                course_credit = credit;
                node = link;
          }
    
          void list_head_insert(course*& head_ptr, const string& name, const double& GPA, const double& credit)
          {
                head_ptr = new course(name, GPA, credit, head_ptr);
          }
          
          course* list_search(course* head_ptr, const string& target)
          {
                course* cursor;
                
                
                for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
                    if (target.compare(cursor->get_name())==0)
                       return cursor;
                return NULL;  
          }
          
          const course* list_search(const course* head_ptr, const std::string& target)
          {
                const course* cursor;
                
                
                for (cursor = head_ptr; cursor != NULL; cursor = cursor->link())
                    if (target.compare(cursor->get_name())==0)
                       return cursor;
                return NULL;       
          }      
          
          void list_remove(course* previous_ptr)
          {
                course* remove_ptr;
                
                remove_ptr = previous_ptr->link();
                previous_ptr->set_node(remove_ptr->link());
                delete remove_ptr;     
          }
    
          void list_head_remove(course*& head_ptr)
          {
                course* remove_ptr;
                
                remove_ptr = head_ptr;
                head_ptr = head_ptr->link();
                delete remove_ptr;     
          }
          
          void list_insert(course* previous_ptr, const string& name, const double& GPA, const double& credit)
          {
                course* insert_ptr;
                
                insert_ptr = new course;
                insert_ptr->set_name(name);
                insert_ptr->set_GPA(GPA);
                insert_ptr->set_credit(credit);
                insert_ptr->set_node(previous_ptr->link());
                previous_ptr->set_node(insert_ptr);                    
          }
          
          void list_copy(const course* source_ptr, course*& head_ptr, course*& tail_ptr)
          {
                head_ptr = NULL;
                tail_ptr = NULL;
                
                if(source_ptr == NULL)
                  return;
                
                list_head_insert(head_ptr, source_ptr->get_name(), source_ptr->get_GPA(), source_ptr->get_credit());  
                tail_ptr = head_ptr;
                
                source_ptr = source_ptr->link();
                while(source_ptr != NULL)
                {
                      list_insert(tail_ptr, source_ptr->get_name(), source_ptr->get_GPA(), source_ptr->get_credit());
                      tail_ptr = tail_ptr->link();
                      source_ptr = source_ptr->link();                 
                }     
          }
          
          void list_clear(course*& head_ptr)
          {
                while (head_ptr != NULL)
                      list_head_remove(head_ptr);     
          }
          
          
          
    /*******STUDENT******/      
          
          student::student(const std::string& name)
          {
                student_name = name;
                head_ptr = NULL;
                many_nodes=0;                       
          }
          
          student::student(const student& source)
          {
                course* tail_ptr;
                list_copy(source.head_ptr, head_ptr, tail_ptr);
                student_name=source.student_name;
                many_nodes=source.many_nodes;                       
          }
          
          student::~student()
          {
                list_clear(head_ptr);
                many_nodes=0;                   
          }
          
          void student::operator =(const student& source)
          {
                course* tail_ptr;
                if(this == &source)
                  return;
                  
                list_clear(head_ptr);
                many_nodes = 0;
                list_copy(source.head_ptr, head_ptr, tail_ptr);
                many_nodes = source.many_nodes;     
          }
          
          void student::course_insert(const string& name, const double& GPA, const double& credit)
          {
                list_head_insert(head_ptr, name, GPA, credit);
                ++many_nodes;
          }
          
          bool student::course_erase(const string& target)
          {
                course* target_ptr;
                target_ptr = list_search(head_ptr, target);
                if (target_ptr == NULL)
                   return false;
                target_ptr->set_name(head_ptr->get_name());
                target_ptr->set_GPA(head_ptr->get_GPA());
                target_ptr->set_credit(head_ptr->get_credit());
                list_head_remove(head_ptr);
                --many_nodes;
                return true;    
          }
          
          
          
    /*********SCHOOL********/
          
          const int school::DEFAULT_CAPACITY=20;
          
          school::school(int initial_capacity)
          {
                student_list = new student[initial_capacity];
                capacity = initial_capacity;
                used=0;           
          }
          
          school::school(const school& source)
          {
                student_list = new student[source.capacity];
                capacity = source.capacity;
                used = source.used;
                copy(source.student_list, source.student_list + used, student_list);                     
          }
          
          school::~school()
          {
                delete [] student_list;                 
          }
          
          void school::reserve(int new_capacity)
          {
                student* larger_array;
                
                if(new_capacity == capacity)
                  return;
                  
                if(new_capacity < used)
                  new_capacity = used;
                  
                larger_array = new student[new_capacity];
                copy(student_list, student_list + used, larger_array);
                delete [] student_list;
                student_list = larger_array;
                capacity = new_capacity;
          }
          
          void school::student_insert(const student& entry)
          {
                if(used == capacity)
                   reserve(used + 10);
                student_list[used] = entry;
                ++used;
          }
    }

    =========
    main
    ========
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "student.h"
    
    using namespace std;
    using namespace jk1998_A;
    
    int main()
    {   
        school students;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Be a bit more precise than "but it doen't seem to work" please. What did you try and what error message did you get ?
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    I have figured out the first question.

    But I have an bigger problem.

    I have a print_student(name) function which suppose search the students for match of name and print out the student name and all the courses of the student. I compiled the program without error, but the program terminated itself without printing out anything when I ran the program. I am using Dev C++. Help PLEASE!

    ========
    main
    ========
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "student.h"
    
    using namespace std;
    using namespace jk1998_A;
    
    int main()
    {   
        school students;
        
        
        student First;
        First.set_student_name("First Student");
        First.course_insert("Algebra",3.2,3.0);
        First.course_insert("Geometry",3.6,3.0);
        
        student Second;
        Second.set_student_name("Second Student");
        Second.course_insert("Algebra",3.0,3.0);
        
        students.student_insert(First);
        students.student_insert(Second);
        
        students.print_student("First Student");
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    =========
    implement.cpp
    ==========
    Code:
    void school::print_student(const string& name)
          {
               for(int i=0; i=used; ++i)
               {
                     if((student_list[i].get_student_name()).compare(name)==0)
                     {
                            cout<<student_list[i].get_student_name();
                            break;                                                         
                     }
                     else
                     {
                           cout<<"No match found";    
                     }        
               }     
          }

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    Code:
               for(int i=0; i=used; ++i)
               {
    which it never will unless used is 0...

    we are one

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    In the main() function, I just tested course A, and again the program terminated itself without printing out any errors. Is this mean the bug is in the course class?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This looks wrong:
    Code:
    course(const std::string& name=NULL, const double& GPA=0, const double& credit=0, course* link = NULL);
    A NULL pointer is probably not good for a string.
    (The string constructor expects a valid char* pointer, and it probably tries to find the char array's lenght first. Boom, dereferencing NULL.)

    The correct default value for an empty string is ""

    Edit: to be more precise, std::string throws an exception, if passed a NULL pointer to the constructor: basic_string::_S_construct NULL not valid.
    Last edited by anon; 04-05-2007 at 02:03 PM.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Yeah, that fixed the problem. Thank You very much.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The best choice would probably be:
    Code:
    const std::string& name=std::string()
    since that is what you are defaulting to, a default constructed (and therefore empty) string.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    good stuff Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21