uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

main.cpp (10597B)


      1 #include <memory>
      2 #include "student.hpp"
      3 
      4 static std::ostream& operator<< (std::ostream& stream, const Student& s);
      5 static void cont();
      6 static void constructor1(const Student& s1);
      7 static void ostream_overload(const Student& s1);
      8 static void constructor2(const Student& s2);
      9 static void copy_constructor(const Student& copystud);
     10 static void conditional_overload(const Student& s1, const Student& s2, const Student& copystud);
     11 static void equals_overload(const Student& s1, Student& s2);
     12 static void constructor3(const Student& s3);
     13 static void detprint(const Student& s3);
     14 static void setters(Student& s3);
     15 static void addgrd(Student& s3);
     16 static void submcourses(Student& s3);
     17 static void plusequals_overload(Student& s3, Course *c);
     18 static void getters(const Course& s3);
     19 static void setters(Course& c);
     20 
     21 /*
     22  * main uses smart pointers since there are quite a few dynamically
     23  * allocated objects and so it looks cleaner to let them handle the
     24  * deletions.
     25  */
     26 int
     27 main(int argc, char **argv)
     28 {
     29     std::unique_ptr<Student> s1 = 
     30         std::make_unique<Student>("12345678", std::string("Name Surname"));
     31     system("clear || cls");
     32     constructor1(*s1); cont();
     33     ostream_overload(*s1); cont();
     34     
     35     std::unique_ptr<Student> s2 = 
     36         std::make_unique<Student>("92345678", std::string("Name Surnamington"), 2);
     37     constructor2(*s2); cont();
     38 
     39     std::unique_ptr<Student> copystud = std::make_unique<Student>(*s2);
     40     copy_constructor(*copystud); cont();
     41     conditional_overload(*s1, *s2, *copystud); cont();
     42     equals_overload(*s1, *s2); cont();
     43 
     44     std::unique_ptr<float[]> grd(new float[4]{9.4f, 8.4f, 5.5f, 6.3f});
     45     std::unique_ptr<Student> s3 = std::make_unique<Student>("72345678",
     46             std::string("Name Surnaming"), 2, 4, grd.get());
     47     constructor3(*s3); cont();
     48     detprint(*s3); cont();
     49     setters(*s3);
     50     addgrd(*s3); cont();
     51     submcourses(*s3); cont();
     52 
     53     std::unique_ptr<Student> s4 = std::make_unique<Student>(*s3);
     54     std::unique_ptr<Course> oop =
     55         std::make_unique<Course>(std::string("356431"), std::string("OOP"), 2);
     56     plusequals_overload(*s3, oop.get()); cont();
     57     getters(*oop); cont();
     58     setters(*oop); 
     59 
     60     return 0;
     61 }
     62 
     63 std::ostream&
     64 operator<< (std::ostream& stream, const Student& s)
     65 {
     66     return stream <<
     67         "ID: " << s.get_id() << std::endl <<
     68         "Name: " << s.get_name() << std::endl <<
     69         "Semester: " << s.get_semester();
     70 }
     71 
     72 void
     73 cont()
     74 {
     75     std::cout << std::endl;
     76     std::cout << "Press <ENTER> to continue. . .";
     77     if (std::cin.get()) system("clear || cls");
     78 }
     79 
     80 void
     81 constructor1(const Student& s1)
     82 {
     83     std::cout << "Constructor for s1 (ID, Name)" << std::endl;
     84     std::cout << "----------------------------" << std::endl;
     85     std::cout << "ID: " << s1.get_id() << std::endl;
     86     std::cout << "Name: " << s1.get_name() << std::endl;
     87     std::cout << "Semester (default value): " << s1.get_semester() << std::endl;
     88     std::cout << "Courses passed (default value): " << s1.get_pcourses() << std::endl;
     89 }
     90 
     91 void
     92 ostream_overload(const Student& s1)
     93 {
     94     std::cout << "std::ostream overload" << std::endl;
     95     std::cout << "----------------------------" << std::endl;
     96     std::cout << s1 << std::endl;
     97 }
     98 
     99 void
    100 constructor2(const Student& s2)
    101 {
    102     std::cout << "Constructor for s2 (ID, Name, Semester)" << std::endl;
    103     std::cout << "----------------------------" << std::endl;
    104     std::cout << "ID: " << s2.get_id() << std::endl;
    105     std::cout << "Name: " << s2.get_name() << std::endl;
    106     std::cout << "Semester: " << s2.get_semester() << std::endl;
    107     std::cout << "Courses passed (default value): " << s2.get_pcourses() << std::endl;
    108 }
    109 
    110 void
    111 copy_constructor(const Student& copystud)
    112 {
    113     std::cout << "Copy Constructor using copystud object as a copy of s2" << std::endl;
    114     std::cout << "----------------------------" << std::endl;
    115     std::cout << "ID: " << copystud.get_id() << std::endl;
    116     std::cout << "Name: " << copystud.get_name() << std::endl;
    117     std::cout << "Semester: " << copystud.get_semester() << std::endl;
    118     std::cout << "Courses passed (default value): " << copystud.get_pcourses() << std::endl;
    119 }
    120 
    121 void
    122 conditional_overload(const Student& s1, const Student& s2, const Student& copystud)
    123 {
    124     std::cout << "Conditional operator overloading" << std::endl;
    125     std::cout << "----------------------------" << std::endl;
    126     if (s2 == copystud) std::cout << "s2 == copystud" << std::endl;
    127     if (s1 != s2)       std::cout << "s1 != s2" << std::endl;
    128     if (s1 < s2)        std::cout << "s1 < s2" << std::endl;
    129     if (s1 <= s2)       std::cout << "s1 <= s2" << std::endl;
    130     if (s2 > s1)        std::cout << "s2 > s1" << std::endl;
    131     if (s2 >= s1)       std::cout << "s2 >= s1" << std::endl;
    132     std::cout << std::endl;
    133 }
    134 
    135 void
    136 equals_overload(const Student& s1, Student& s2)
    137 {
    138     s2 = s1;
    139     std::cout << "= operator overload (s2 = s1)" << std::endl;
    140     std::cout << "----------------------------" << std::endl;
    141     std::cout << "ID: " << s2.get_id() << std::endl;
    142     std::cout << "Name: " << s2.get_name() << std::endl;
    143     std::cout << "Semester: " << s2.get_semester() << std::endl;
    144     std::cout << "Courses passed (default value): " << s2.get_pcourses() << std::endl;
    145 }
    146 
    147 void
    148 constructor3(const Student& s3)
    149 {
    150     std::cout << "Constructor for s3 (ID, Name, Semester, Courses passed, Grades)" << std::endl;
    151     std::cout << "----------------------------" << std::endl;
    152     std::cout << "ID: " << s3.get_id() << std::endl;
    153     std::cout << "Name: " << s3.get_name() << std::endl;
    154     std::cout << "Semester: " << s3.get_semester() << std::endl;
    155     std::cout << "Courses passed: " << s3.get_pcourses() << std::endl;
    156     std::cout << "Grades: ";
    157 
    158     float *gr = s3.get_grades();
    159     if (gr != nullptr)
    160     {
    161         for (unsigned int i = 0; i < s3.get_pcourses(); i++)
    162         {
    163             if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", ";
    164             else std::cout << gr[i] << std::endl;
    165         }
    166     }
    167 }
    168 
    169 void
    170 detprint(const Student& s3)
    171 {
    172     std::cout << "Detailed print of s3's grades" << std::endl;
    173     std::cout << "----------------------------" << std::endl;
    174     s3.detailed_print();
    175 }
    176 
    177 void
    178 setters(Student& s3)
    179 {
    180     s3.set_id("010101");
    181     s3.set_name("AAAA");
    182     s3.set_semester(100);
    183     s3.set_pcourses(2);
    184     std::unique_ptr<float[]> gg(new float[2]{0.1f, 2.2f});
    185     s3.set_grades(gg.get());
    186 
    187     std::cout << "Setters example using s3" << std::endl;
    188     std::cout << "----------------------------" << std::endl;
    189     std::cout << "Input: 010101 " << std::endl;
    190     std::cout << "New ID: " << s3.get_id() << std::endl;
    191     std::cout << std::endl;
    192     std::cout << "Input: AAAA " << std::endl;
    193     std::cout << "New name: " << s3.get_name() << std::endl;
    194     std::cout << std::endl;
    195     std::cout << "Input: 100" << std::endl;
    196     std::cout << "New semester: " << s3.get_semester() << std::endl;
    197     std::cout << std::endl;
    198     std::cout << "Input: 2" << std::endl;
    199     std::cout << "New courses passed: " << s3.get_pcourses() << std::endl;
    200     std::cout << std::endl;
    201     std::cout << "Input: 0.1, 2.2" << std::endl;
    202     std::cout << "New grades: ";
    203 
    204     float *gr = s3.get_grades();
    205     if (gr != nullptr)
    206     {
    207         for (unsigned int i = 0; i < s3.get_pcourses(); i++)
    208         {
    209             if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", ";
    210             else std::cout << gr[i] << std::endl << std::endl;
    211         }
    212     }
    213 }
    214 
    215 void
    216 addgrd(Student& s3)
    217 {
    218     s3.add_grade(7.5f);
    219     float *gr = s3.get_grades();
    220     if (gr != nullptr)
    221     {
    222         std::cout << "Input: 7.5" << std::endl;
    223         std::cout << "Updated new grades: ";
    224         for (unsigned int i = 0; i < s3.get_pcourses(); i++)
    225         {
    226             if (i != s3.get_pcourses()-1) std::cout << gr[i] << ", ";
    227             else std::cout << gr[i] << std::endl;
    228         }
    229     }
    230 }
    231 
    232 void
    233 submcourses(Student& s3)
    234 {
    235     Course *a = new Course(std::string("11231"), std::string("Discrete Math"), 1);
    236     Course *b = new Course(std::string("56562"), std::string("Physics"), 1);
    237     Course *c[] = {a, b};
    238     s3.set_num_submitted_courses(2);
    239     s3.set_submitted_courses(c);
    240 
    241     std::cout << "Submitted courses example" << std::endl;
    242     std::cout << "----------------------------" << std::endl;
    243     std::cout << "Input: 2" << std::endl;
    244     std::cout << "Number of submitted courses: " << s3.get_num_submitted_courses();
    245     std::cout << std::endl << std::endl;
    246     
    247     Course **sc = s3.get_submitted_courses();
    248     if (sc != nullptr)
    249     {
    250         std::cout << "Input: Discrete Math, Physics" << std::endl;
    251         std::cout << "Submitted courses: ";
    252         for (unsigned int i = 0; i < s3.get_num_submitted_courses(); i++)
    253         {
    254             if (i != s3.get_num_submitted_courses()-1) std::cout << sc[i][0].get_cname() << ", ";
    255             else std::cout << sc[i][0].get_cname() << std::endl;
    256         }
    257     }
    258 }
    259 
    260 void
    261 plusequals_overload(Student& s3, Course *c)
    262 {
    263     s3 += c;
    264     Course **sc = s3.get_submitted_courses();
    265     if (sc != nullptr)
    266     {
    267         std::cout << "+= operator overload" << std::endl;
    268         std::cout << "----------------------------" << std::endl;
    269         std::cout << "Input: OOP" << std::endl;;
    270         std::cout << "Updated submitted courses: ";
    271         for (unsigned int i = 0; i < s3.get_num_submitted_courses(); i++)
    272         {
    273             if (i != s3.get_num_submitted_courses()-1) std::cout << sc[i][0].get_cname() << ", ";
    274             else std::cout << sc[i][0].get_cname() << std::endl;
    275         }
    276     }
    277 }
    278 
    279 void
    280 getters(const Course& c)
    281 {
    282     std::cout << "Getters example using Course object" << std::endl;
    283     std::cout << "----------------------------" << std::endl;
    284     std::cout << "Constructor: Course(\"356431\", \"OOP\", 2)" << std::endl;
    285     std::cout << "Code: " << c.get_code() << std::endl;
    286     std::cout << "Course name: " << c.get_cname() << std::endl;
    287     std::cout << "Course semester: " << c.get_csemester() << std::endl;
    288 }
    289 
    290 void
    291 setters(Course& c)
    292 {
    293     c.set_code(std::string("14556"));
    294     c.set_cname(std::string("Calculus I"));
    295     c.set_csemester(1);
    296 
    297     std::cout << "Setters example using Course object" << std::endl;
    298     std::cout << "----------------------------" << std::endl;
    299     std::cout << "Input: 14556" << std::endl;
    300     std::cout << "New code: " << c.get_code() << std::endl;
    301     std::cout << std::endl;
    302     std::cout << "Input: Calculus I" << std::endl;
    303     std::cout << "New course name: " << c.get_cname() << std::endl;
    304     std::cout << std::endl;
    305     std::cout << "Input: 1" << std::endl;
    306     std::cout << "New course semester: " << c.get_csemester() << std::endl;
    307 }