uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

student.hpp (3689B)


      1 #ifndef STUDENT_HPP
      2 #define STUDENT_HPP
      3 
      4 #include <cstring>
      5 #include <iostream>
      6 #include <iomanip>
      7 #include <string>
      8 
      9 #include "course.hpp"
     10 
     11 class Student
     12 {
     13     private:
     14         char *id;              // id
     15         std::string name;      // Name
     16         unsigned int semester; // Current semester
     17         unsigned int pcourses; // Passed courses
     18         float *grades;         // Grades
     19         Course **sc;           // Submitted courses
     20         unsigned int nsc;      // Number of submitted courses 
     21 
     22     public:
     23         Student(const char *id, const std::string& name);
     24         Student(const char *id, const std::string& name,
     25                 const unsigned int semester);
     26         Student(const char *id, const std::string& name,
     27                 const unsigned int semester,
     28                 const unsigned int pcourses, const float *grades);
     29         Student(const Student& s);
     30         ~Student();
     31 
     32         void operator+= (Course *c);
     33         Student operator= (const Student& s);
     34 
     35         /* 
     36          * The functions below are marked as constexpr just so they 
     37          * can be computed at compile time since everything is hardcoded.
     38          */
     39         constexpr bool operator== (const Student& s) const {return this->semester == s.semester;}
     40         constexpr bool operator!= (const Student& s) const {return this->semester != s.semester;}
     41         constexpr bool operator<  (const Student& s) const {return this->semester <  s.semester;}
     42         constexpr bool operator<= (const Student& s) const {return this->semester <= s.semester;}
     43         constexpr bool operator>  (const Student& s) const {return this->semester >  s.semester;}
     44         constexpr bool operator>= (const Student& s) const {return this->semester >= s.semester;}
     45 
     46         const std::string& get_name() const;
     47         constexpr const char *get_id() const {return this->id;}
     48         constexpr const unsigned int get_semester() const {return this->semester;}
     49         constexpr const unsigned int get_pcourses() const {return this->pcourses;}
     50         constexpr float *get_grades() const
     51             {return (this->pcourses > 0) ? this->grades : nullptr;}
     52         constexpr Course **get_submitted_courses() const {return this->sc;}
     53         constexpr const unsigned int get_num_submitted_courses() const {return this->nsc;}
     54 
     55         void set_id      (const char *id);
     56         void set_name    (const std::string& name);
     57         void set_semester(const unsigned int semester);
     58         void set_pcourses(const unsigned int pcourses);
     59         void set_grades  (const float *grades);
     60         void set_num_submitted_courses(const unsigned int nsc);
     61         void set_submitted_courses(Course **sc);
     62 
     63         void add_grade(const float grade);
     64         void detailed_print() const;
     65 
     66     private:
     67         template<typename T> T *conv(const T *arr, std::size_t len) const;
     68         template<typename T> T *resize(const T *arr, std::size_t len);
     69         constexpr std::size_t len(const char *s) const {return std::strlen(s) + 1;}
     70         float avg() const;
     71 };
     72 
     73 /* 
     74  * Makes a copy of a given array of type T and returns a temporary array
     75  * which is meant to be stored in a member variable.
     76  * If the array is empty it returns nullptr.
     77  */
     78 template<typename T> T *
     79 Student::conv(const T *arr, std::size_t len) const
     80 {
     81     if (arr != nullptr)
     82     {
     83         T *tmp = new T[len];
     84         std::memcpy(tmp, arr, sizeof(T) * len);
     85         return tmp;
     86     }
     87     else return nullptr;
     88 }
     89 
     90 template<typename T> T *
     91 Student::resize(const T *arr, std::size_t len)
     92 {
     93     T *tmp = new T[len+1];
     94     if (arr != nullptr)
     95     {
     96         std::memcpy(tmp, arr, sizeof(T) * len);
     97         delete[] arr;
     98     }
     99     return tmp;
    100 }
    101 
    102 #endif /* STUDENT_HPP */