commit 4e70607607e0d1fa945314c0f26f1e099a6211aa
parent 23aaa0847bd8c70d343cf95e9282d53bbad499d2
Author: Christos Margiolis <christos@margiolis.net>
Date: Thu, 2 Apr 2020 18:00:38 +0300
fixed bugs
Diffstat:
7 files changed, 110 insertions(+), 16 deletions(-)
diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp
@@ -46,7 +46,7 @@ class Student
inline const std::string& get_name() const {return name;}
inline unsigned int get_semester() const {return semester;}
inline unsigned int get_psubj() const {return psubj;}
- inline float *get_grades() const {return grades;}
+ float *get_grades() const;
inline void set_AM(const char *AM) {this->AM = convert_AM(AM);}
inline void set_name(const std::string& name) {this->name = name;}
@@ -61,6 +61,13 @@ class Student
float calc_average() const;
};
+float *Student::get_grades() const
+{
+ float *ret = new float[psubj];
+ std::copy(grades, grades+psubj, ret);
+ return ret;
+}
+
char *Student::convert_AM(const char *AM)
{
int len = strlen(AM);
@@ -107,7 +114,7 @@ float Student::calc_average() const
return average;
}
-inline std::ostream& operator<< (std::ostream& stream, const Student& s)
+std::ostream& operator<< (std::ostream& stream, const Student& s)
{
return stream << "AM: " << s.get_AM() << std::endl << "Name: " << s.get_name() << std::endl
<< "Semester: " << s.get_semester() << std::endl;
@@ -121,6 +128,7 @@ void copy_constructor(const Student& copystud);
void constructor3(const Student& s3);
void detprint (const Student& s3);
void setters(Student& s3);
+void addgrd(Student& s3);
int main(int argc, char **argv)
{
@@ -142,7 +150,8 @@ int main(int argc, char **argv)
Student *s3 = new Student("12345678", "Name Surname", 2, 4, grd);
constructor3(*s3); cont();
detprint(*s3); cont();
- setters(*s3); cont();
+ setters(*s3);
+ addgrd(*s3); cont();
delete s3;
return 0;
@@ -246,7 +255,14 @@ void setters(Student& s3)
if (i != s3.get_psubj()-1) std::cout << gr[i] << ", ";
else std::cout << gr[i] << std::endl;
}
+
+ delete[] gr;
+}
+
+void addgrd(Student& s3)
+{
s3.add_grade(7.5f);
+ float *gr = new float[s3.get_psubj()];
gr = s3.get_grades();
std::cout << "Input: s3.add_grade(7.5f)" << '\t';
std::cout << "Output: s3.get_grades(): ";
@@ -255,4 +271,6 @@ void setters(Student& s3)
if (i != s3.get_psubj()-1) std::cout << gr[i] << ", ";
else std::cout << gr[i] << std::endl;
}
+
+ delete[] gr;
}
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
@@ -1,7 +1,7 @@
#include "student.h"
#include "subject.h"
-inline std::ostream& operator<< (std::ostream& stream, const Student& s)
+std::ostream& operator<< (std::ostream& stream, const Student& s)
{
return stream << "AM: " << s.get_AM() << std::endl << "Name: " << s.get_name() << std::endl
<< "Semester: " << s.get_semester() << std::endl;
@@ -16,6 +16,10 @@ void conditional_overload(const Student& s1, const Student& s2, const Student& c
void constructor3(const Student& s3);
void detprint (const Student& s3);
void setters(Student& s3);
+void addgrd(Student& s3);
+void submsubj(Student& s3);
+void plusequals_overload(Student& s3);
+void equals_overload(Student& s3);
void getters(const Subject& s3);
void setters(Subject& sb);
@@ -41,7 +45,11 @@ int main(int argc, char **argv)
Student *s3 = new Student("12345678", "Name Surname", 2, 4, grd);
constructor3(*s3); cont();
detprint(*s3); cont();
- setters(*s3); cont();
+ setters(*s3);
+ addgrd(*s3); cont();
+ submsubj(*s3); cont();
+ plusequals_overload(*s3); cont();
+ //equals_overload(*s3); cont();
delete s3;
Subject *sb = new Subject("35643", "OOP", 2);
@@ -118,13 +126,15 @@ void constructor3(const Student& s3)
std::cout << "s3.get_semester(): " << s3.get_semester() << std::endl;
std::cout << "s3.get_psubj(): " << s3.get_psubj() << std::endl;
- float *gr = s3.get_grades();
+ float *gr = new float[s3.get_psubj()];
+ gr = s3.get_grades();
std::cout << "s3.get_grades(): ";
for (unsigned int i = 0; i < s3.get_psubj(); i++)
{
if (i != s3.get_psubj()-1) std::cout << gr[i] << ", ";
else std::cout << gr[i] << std::endl << std::endl;
}
+ delete[] gr;
}
void detprint (const Student& s3)
@@ -163,7 +173,14 @@ void setters(Student& s3)
if (i != s3.get_psubj()-1) std::cout << gr[i] << ", ";
else std::cout << gr[i] << std::endl;
}
+
+ delete[] gr;
+}
+
+void addgrd(Student& s3)
+{
s3.add_grade(7.5f);
+ float *gr = new float[s3.get_psubj()];
gr = s3.get_grades();
std::cout << "Input: s3.add_grade(7.5f)" << '\t';
std::cout << "Output: s3.get_grades(): ";
@@ -173,14 +190,20 @@ void setters(Student& s3)
else std::cout << gr[i] << std::endl;
}
- std::cout << std::endl;
- std::cout << "Input: s3.set_num_submitted_subjects(3)" << std::endl;;
- std::cout << "Output: s3.get_num_submitted_subjects(): " << s3.get_num_submitted_subjects() << std::endl << std::endl;
+ delete[] gr;
+}
+void submsubj(Student& s3)
+{
std::string ss[3] = {"Math", "Physics", "Programming"};
s3.set_num_submitted_subjects(3);
s3.set_submitted_subjects(ss);
+ std::cout << "Submitted subjects example" << std::endl;
+ std::cout << "----------------------------" << std::endl;
+ std::cout << "Input: s3.set_num_submitted_subjects(3)" << std::endl;;
+ std::cout << "Output: s3.get_num_submitted_subjects(): " << s3.get_num_submitted_subjects() << std::endl << std::endl;
+
std::string *ssj = new std::string[s3.get_num_submitted_subjects()];
ssj = s3.get_submitted_subjects();
std::cout << "Input: {\"Math\", \"Physics\", \"Programming\"}" << std::endl;;
@@ -190,13 +213,33 @@ void setters(Student& s3)
if (i != s3.get_num_submitted_subjects()-1) std::cout << ssj[i] << ", ";
else std::cout << ssj[i] << std::endl;
}
+
+ delete[] ssj;
+}
+
+void plusequals_overload(Student& s3)
+{
+ s3 += "Discrete Maths";
+ std::string *ssj = new std::string[s3.get_num_submitted_subjects()];
+ ssj = s3.get_submitted_subjects();
+ std::cout << "+= operator overload" << std::endl;
+ std::cout << "----------------------------" << std::endl;
+ std::cout << "Input: s3 += \"Discrete Maths\"" << std::endl;;
+ std::cout << "Output: s3.get_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] << ", ";
+ else std::cout << ssj[i] << std::endl;
+ }
- gr = nullptr;
- ssj = nullptr;
- delete[] gr;
delete[] ssj;
}
+void equals_overload(Student& s3)
+{
+
+}
+
void getters(const Subject& sb)
{
std::cout << "Getters example using Subject object" << std::endl;
diff --git a/assignment-2.3-operoverloading/src/student.cpp b/assignment-2.3-operoverloading/src/student.cpp
@@ -25,6 +25,39 @@ Student::~Student()
{
delete[] this->AM;
delete[] this->grades;
+ delete[] this->submittedSubjects;
+}
+
+void Student::operator+= (const std::string& s)
+{
+ std::string *tmp = new std::string[numSubmittedSubjects+1];
+ std::copy(submittedSubjects, submittedSubjects+numSubmittedSubjects, tmp);
+ tmp[numSubmittedSubjects] = s;
+ delete[] submittedSubjects;
+ submittedSubjects = new std::string[numSubmittedSubjects+1];
+ std::copy(tmp, tmp+numSubmittedSubjects+1, submittedSubjects);
+ numSubmittedSubjects++;
+ delete[] tmp;
+}
+
+float *Student::get_grades() const
+{
+ float *ret = new float[psubj];
+ std::copy(grades, grades+psubj, ret);
+ return ret;
+}
+
+void Student::set_submitted_subjects(std::string* submittedSubjects)
+{
+ this->submittedSubjects = new std::string[numSubmittedSubjects];
+ std::copy(submittedSubjects, submittedSubjects+numSubmittedSubjects, this->submittedSubjects);
+}
+
+std::string *Student::get_submitted_subjects() const
+{
+ std::string *ret = new std::string[numSubmittedSubjects];
+ std::copy(submittedSubjects, submittedSubjects+numSubmittedSubjects, ret);
+ return ret;
}
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
@@ -24,7 +24,7 @@ class Student
Student(const Student& s);
~Student();
- // += overload
+ void operator+= (const std::string& s);
// = overload
inline bool operator== (const Student& s) const {return (this->semester == s.semester) ? true : false;}
@@ -38,9 +38,9 @@ class Student
inline const std::string& get_name() const {return this->name;}
inline unsigned int get_semester() const {return this->semester;}
inline unsigned int get_psubj() const {return this->psubj;}
- inline float *get_grades() const {return this->grades;}
+ float *get_grades() const;
inline unsigned int get_num_submitted_subjects() const {return this->numSubmittedSubjects;}
- inline std::string *get_submitted_subjects() const {return this->submittedSubjects;}
+ std::string *get_submitted_subjects() const;
inline void set_AM(const char *AM) {this->AM = convert_AM(AM);}
inline void set_name(const std::string& name) {this->name = name;}
@@ -48,7 +48,7 @@ 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;}
- inline void set_submitted_subjects(std::string* submittedSubjects) {this->submittedSubjects = submittedSubjects;}
+ void set_submitted_subjects(std::string* submittedSubjects);
char *convert_AM(const char *AM);
float *convert_PSG(const float *grades);