uni

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

commit 5a9bbb5ad8783735feed7f53baf96ad402f41acf
parent 21b642f690479eb76100f87210b803b9eccd430c
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed,  8 Apr 2020 16:55:03 +0300

fixed pointers

Diffstat:
Massignment-2.3-operoverloading/bin/operoverloading | 0
Massignment-2.3-operoverloading/obj/main.o | 0
Massignment-2.3-operoverloading/obj/student.o | 0
Massignment-2.3-operoverloading/src/main.cpp | 67+++++++++++++++++++++++++++++--------------------------------------
Massignment-2.3-operoverloading/src/student.cpp | 31++++++++++++++-----------------
Massignment-2.3-operoverloading/src/student.h | 4++--
6 files changed, 45 insertions(+), 57 deletions(-)

diff --git a/assignment-2.3-operoverloading/bin/operoverloading b/assignment-2.3-operoverloading/bin/operoverloading Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/main.o b/assignment-2.3-operoverloading/obj/main.o Binary files differ. diff --git a/assignment-2.3-operoverloading/obj/student.o b/assignment-2.3-operoverloading/obj/student.o Binary files differ. diff --git a/assignment-2.3-operoverloading/src/main.cpp b/assignment-2.3-operoverloading/src/main.cpp @@ -18,7 +18,7 @@ static void detprint (const Student& s3); static void setters(Student& s3); static void addgrd(Student& s3); static void submsubj(Student& s3); -static void plusequals_overload(Student& s3, const Subject& s); +static void plusequals_overload(Student& s3, Subject *s); static void getters(const Subject& s3); static void setters(Subject& sb); @@ -56,7 +56,7 @@ int main(int argc, char **argv) std::string c = "356431"; std::string n = "OOP"; Subject *oop = new Subject(c, n, 2); - plusequals_overload(*s3, *oop); cont(); + plusequals_overload(*s3, oop); cont(); getters(*oop); cont(); setters(*oop); @@ -211,49 +211,40 @@ static void submsubj(Student& s3) std::string n1 = "Discrete Math"; std::string n2 = "Physics"; - Subject *a = new Subject{c1, n1, 1}; - Subject *b = new Subject{c2, n2, 1}; - Subject **sb = new Subject *[2]; - sb[0] = a; - sb[1] = b; + Subject *a = new Subject(c1, n1, 1); + Subject *b = new Subject(c2, n2, 1); + Subject **sb = new Subject *[2]{a, b}; s3.set_num_submitted_subjects(2); s3.set_submitted_subjects(sb); - delete a; - delete b; - - //std::cout << "Submitted subjects example" << std::endl; - //std::cout << "----------------------------" << std::endl; - //std::cout << "Input: s3.set_num_submitted_subjects(2)" << std::endl; - //std::cout << "Number of submitted subjects: " << s3.get_num_submitted_subjects() << std::endl << std::endl; + std::cout << "Submitted subjects example" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: s3.set_num_submitted_subjects(2)" << std::endl; + std::cout << "Number of submitted subjects: " << s3.get_num_submitted_subjects() << std::endl << std::endl; - //Subject **ssj = s3.get_submitted_subjects(); - //std::cout << "Input: {\"Discrete Math\", \"Physics\"}" << std::endl; - //std::cout << "Submitted subjects: "; - //for (unsigned int i = 0; i < s3.get_num_submitted_subjects(); i++) - //{ - //if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i].get_sname() << ", "; - //else std::cout << ssj[i].get_sname() << std::endl; - //} - - for (int i = 0; i < 2; i++) - delete[] sb[i]; - delete[] sb; + Subject **ssj = s3.get_submitted_subjects(); + std::cout << "Input: {\"Discrete Math\", \"Physics\"}" << std::endl; + std::cout << "Submitted subjects: "; + for (unsigned int i = 0; i < s3.get_num_submitted_subjects(); i++) + { + if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i][0].get_sname() << ", "; + else std::cout << ssj[i][0].get_sname() << std::endl; + } } -static void plusequals_overload(Student& s3, const Subject& s) +static void plusequals_overload(Student& s3, Subject *s) { - //s3 += s; - //Subject **ssj = s3.get_submitted_subjects(); - //std::cout << "+= operator overload" << std::endl; - //std::cout << "----------------------------" << std::endl; - //std::cout << "Input: s3 += \"OOP\"" << std::endl;; - //std::cout << "Updated submitted subjects: "; - //for (unsigned int i = 0; i < s3.get_num_submitted_subjects(); i++) - //{ - //if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i].get_sname() << ", "; - //else std::cout << ssj[i].get_sname() << std::endl; - //} + s3 += s; + Subject **ssj = s3.get_submitted_subjects(); + std::cout << "+= operator overload" << std::endl; + std::cout << "----------------------------" << std::endl; + std::cout << "Input: s3 += \"OOP\"" << std::endl;; + std::cout << "Updated submitted subjects: "; + for (unsigned int i = 0; i < s3.get_num_submitted_subjects(); i++) + { + if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i][0].get_sname() << ", "; + else std::cout << ssj[i][0].get_sname() << std::endl; + } } static void getters(const Subject& sb) diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp @@ -28,10 +28,14 @@ Student::Student(const Student& s) int sl = strlen(s.AM); this->AM = new char[sl + 1]; memcpy(AM, s.AM, sizeof(s.AM) + (sl+1)); - this->grades = new float[psubj]; - memcpy(grades, s.grades, sizeof(s.grades) * psubj); + this->grades = new float[s.psubj]; + memcpy(grades, s.grades, sizeof(s.grades) * s.psubj); - if (numSubmittedSubjects <= 0) submittedSubjects = nullptr; + if (s.numSubmittedSubjects <= 0) + { + numSubmittedSubjects = 0; + submittedSubjects = nullptr; + } else memcpy(submittedSubjects, s.submittedSubjects, sizeof(s.submittedSubjects) * s.numSubmittedSubjects); } @@ -39,25 +43,18 @@ Student::~Student() { delete[] this->AM; delete[] this->grades; - - for (int i = 0; i < numSubmittedSubjects; i++) - delete[] this->submittedSubjects[i]; delete[] this->submittedSubjects; } -void Student::operator+= (const Subject& s) +Student& Student::operator+= (Subject* s) { - Subject **tmp = new Subject *[numSubmittedSubjects+1]; - memcpy(tmp, submittedSubjects, sizeof(Subject) * numSubmittedSubjects); - tmp[numSubmittedSubjects][0] = s; - if (submittedSubjects != nullptr) - { - for (int i = 0; i < numSubmittedSubjects; i++) - delete[] submittedSubjects[i]; - delete[] submittedSubjects; - } + Subject **tmp = new Subject *[numSubmittedSubjects]; + memcpy(tmp, submittedSubjects, sizeof(Subject *) * numSubmittedSubjects); + tmp[numSubmittedSubjects] = s; + if (submittedSubjects != nullptr) delete[] submittedSubjects; submittedSubjects = tmp; numSubmittedSubjects++; + return *this; } Student& Student::operator= (const Student& s) @@ -80,7 +77,7 @@ void Student::set_submitted_subjects(Subject **submittedSubjects) { // handle 0 subj this->submittedSubjects = new Subject *[numSubmittedSubjects]; - memcpy(this->submittedSubjects, submittedSubjects, sizeof(Subject) * numSubmittedSubjects); + memcpy(this->submittedSubjects, submittedSubjects, sizeof(Subject *) * numSubmittedSubjects); } char *Student::convert_AM(const char *AM) diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h @@ -30,7 +30,7 @@ class Student ~Student(); friend std::ostream& operator<< (std::ostream& stream, const Student& s); - void operator+= (const Subject& s); + Student& operator+= (Subject *s); Student& operator= (const Student& s); inline bool operator== (const Student& s) const {return (this->semester == s.semester) ? true : false;} @@ -54,8 +54,8 @@ class Student inline void set_psubj(unsigned int psubj) {this->psubj = psubj;} inline void set_grades(float *grades) {this->grades = convert_PSG(grades);} inline void set_num_submitted_subjects(unsigned int numSubmittedSubjects) {this->numSubmittedSubjects = numSubmittedSubjects;} - void set_submitted_subjects(Subject **submittedSubjects); + void set_submitted_subjects(Subject **submittedSubjects); char *convert_AM(const char *AM); float *convert_PSG(const float *grades); void add_grade(float grade);