commit 7c8857a7f725ee7bf3b6486f86a6b27057c7d37b
parent 9381892015b6d3f14482b783c80efb5efdc82e1e
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 9 Apr 2020 17:53:20 +0300
more bug fixes
Diffstat:
11 files changed, 34 insertions(+), 24 deletions(-)
diff --git a/assignment-2.2-classes/a.out b/assignment-2.2-classes/a.out
Binary files differ.
diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp
@@ -32,7 +32,7 @@ class Student
AM = new char[sl + 1];
memcpy(AM, s.AM, sizeof(s.AM) + (sl+1));
this->grades = new float[s.psubj];
- memcpy(grades, s.grades, sizeof(s.grades) * s.psubj);
+ memcpy(grades, s.grades, sizeof(float) * s.psubj);
}
~Student()
@@ -75,7 +75,7 @@ float *Student::convert_PSG(const float *grades)
if (psubj > 0)
{
float *tmp = new float[psubj];
- memcpy(tmp, grades, sizeof(grades) * psubj);
+ memcpy(tmp, grades, sizeof(float) * psubj);
return tmp;
}
else return nullptr;
@@ -84,9 +84,12 @@ float *Student::convert_PSG(const float *grades)
void Student::add_grade(float grade)
{
float *tmp = new float[psubj+1];
- memcpy(tmp, grades, sizeof(grades) * psubj);
+ if (grades != nullptr)
+ {
+ memcpy(tmp, grades, sizeof(float) * psubj);
+ delete[] grades;
+ }
tmp[psubj] = grade;
- if (grades != nullptr) delete[] grades;
grades = tmp;
psubj++;
}
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/obj/subject.o b/assignment-2.3-operoverloading/obj/subject.o
Binary files differ.
diff --git a/assignment-2.3-operoverloading/src/main.cpp b/assignment-2.3-operoverloading/src/main.cpp
@@ -52,6 +52,7 @@ int main(int argc, char **argv)
setters(*s3);
addgrd(*s3); cont();
submsubj(*s3); cont();
+ Student *s4 = new Student(*s3);
std::string c = "356431";
std::string n = "OOP";
@@ -61,6 +62,7 @@ int main(int argc, char **argv)
getters(*oop); cont();
setters(*oop);
+ delete s4;
delete s3;
delete oop;
diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp
@@ -29,14 +29,18 @@ Student::Student(const Student& s)
this->AM = new char[sl + 1];
memcpy(AM, s.AM, sizeof(s.AM) + (sl+1));
this->grades = new float[s.psubj];
- memcpy(grades, s.grades, sizeof(s.grades) * s.psubj);
+ memcpy(grades, s.grades, sizeof(float) * s.psubj);
if (s.numSubmittedSubjects <= 0)
{
numSubmittedSubjects = 0;
submittedSubjects = nullptr;
}
- else memcpy(submittedSubjects, s.submittedSubjects, sizeof(s.submittedSubjects) * s.numSubmittedSubjects);
+ else
+ {
+ this->submittedSubjects = new Subject *[s.numSubmittedSubjects];
+ memcpy(submittedSubjects, s.submittedSubjects, sizeof(s.submittedSubjects) * s.numSubmittedSubjects);
+ }
}
Student::~Student()
@@ -46,18 +50,20 @@ Student::~Student()
delete[] this->submittedSubjects;
}
-Student& Student::operator+= (Subject* s)
+void Student::operator+= (Subject *s)
{
- Subject **tmp = new Subject *[numSubmittedSubjects];
- memcpy(tmp, submittedSubjects, sizeof(Subject *) * numSubmittedSubjects);
+ Subject **tmp = new Subject *[numSubmittedSubjects+1];
+ if (submittedSubjects != nullptr)
+ {
+ memcpy(tmp, submittedSubjects, sizeof(Subject *) * numSubmittedSubjects);
+ delete[] submittedSubjects;
+ }
tmp[numSubmittedSubjects] = s;
- if (submittedSubjects != nullptr) delete[] submittedSubjects;
submittedSubjects = tmp;
numSubmittedSubjects++;
- return *this;
}
-Student& Student::operator= (const Student& s)
+Student Student::operator= (const Student& s)
{
if (this == &s) return *this;
this->AM = convert_AM(s.AM);
@@ -93,7 +99,7 @@ float *Student::convert_PSG(const float *grades)
if (psubj > 0)
{
float *tmp = new float[psubj];
- memcpy(tmp, grades, sizeof(grades) * psubj);
+ memcpy(tmp, grades, sizeof(float) * psubj);
return tmp;
}
else return nullptr;
@@ -102,9 +108,12 @@ float *Student::convert_PSG(const float *grades)
void Student::add_grade(float grade)
{
float *tmp = new float[psubj+1];
- memcpy(tmp, grades, sizeof(grades) * psubj);
+ if (grades != nullptr)
+ {
+ memcpy(tmp, grades, sizeof(float) * psubj);
+ delete[] grades;
+ }
tmp[psubj] = grade;
- if (grades != nullptr) delete[] grades;
grades = tmp;
psubj++;
}
diff --git a/assignment-2.3-operoverloading/src/student.h b/assignment-2.3-operoverloading/src/student.h
@@ -3,7 +3,6 @@
#include <iostream>
#include <iomanip>
-#include <algorithm>
#include <string>
#include <string.h>
@@ -30,8 +29,8 @@ class Student
~Student();
friend std::ostream& operator<< (std::ostream& stream, const Student& s);
- Student& operator+= (Subject *s);
- Student& operator= (const Student& s);
+ void operator+= (Subject *s);
+ Student operator= (const Student& s);
inline bool operator== (const Student& s) const {return (this->semester == s.semester) ? true : false;}
inline bool operator!= (const Student& s) const {return (this->semester != s.semester) ? true : false;}
diff --git a/assignment-2.3-operoverloading/src/subject.cpp b/assignment-2.3-operoverloading/src/subject.cpp
@@ -1,12 +1,10 @@
#include "subject.h"
-Subject::Subject() {}
+Subject::Subject() {std::cout << "Constructor 1" << std::endl;}
Subject::Subject(const std::string& code, const std::string& sname, unsigned int subjsemester)
- :code(code), sname(sname), subjsemester(subjsemester) {}
+ :code(code), sname(sname), subjsemester(subjsemester) {std::cout << "Constructor 2" << std::endl;}
Subject::Subject(const Subject& s)
- :code(s.code), sname(s.sname), subjsemester(s.subjsemester) {}
-
-Subject::~Subject() {}
+ :code(s.code), sname(s.sname), subjsemester(s.subjsemester) {std::cout << "Copy Constructor" << std::endl;}
diff --git a/assignment-2.3-operoverloading/src/subject.h b/assignment-2.3-operoverloading/src/subject.h
@@ -15,7 +15,6 @@ class Subject
Subject();
Subject(const std::string& code, const std::string& sname, unsigned int subjsemester);
Subject(const Subject& s);
- ~Subject();
inline const std::string& get_code(void) const {return this->code;}
inline const std::string& get_sname(void) const {return this->sname;}