commit 8d37d837a1cadc785283f15bd1933404beb44015
parent 295be380a13d62923b87d52c96429e07af7e0630
Author: Christos Margiolis <christos@margiolis.net>
Date: Wed, 18 Mar 2020 16:34:29 +0200
implement methods outside class
Diffstat:
1 file changed, 45 insertions(+), 39 deletions(-)
diff --git a/assignment-2.2-classes/classes.cpp b/assignment-2.2-classes/classes.cpp
@@ -55,49 +55,55 @@ class Student
inline void set_grades(float *grades) {this->grades = convert_PSG(grades);}
inline void print_3first() {std::cout << AM << " " << name << " " << semester << std::endl;}
- char *convert_AM(const char *AM)
- {
- int len = strlen(AM);
- this->AM = new char[len+1];
- std::copy(AM, AM+len, this->AM);
- return this->AM;
- }
+ char *convert_AM(const char *AM);
+ float *convert_PSG(const float *grades);
+ void add_grade(float grade);
+ void detailed_print();
+ float calc_average();
+};
- float *convert_PSG(const float *grades)
- {
- this->grades = new float[psubj];
- std::copy(grades, grades+psubj, this->grades);
- return this->grades;
- }
+char *Student::convert_AM(const char *AM)
+{
+ int len = strlen(AM);
+ this->AM = new char[len+1];
+ std::copy(AM, AM+len, this->AM);
+ return this->AM;
+}
- void add_grade(float grade)
- {
- float *tmp = new float[psubj+1];
- std::copy(this->grades, this->grades+psubj, tmp);
- tmp[psubj] = grade;
- this->grades = tmp;
- psubj++;
- }
+float *Student::convert_PSG(const float *grades)
+{
+ this->grades = new float[psubj];
+ std::copy(grades, grades+psubj, this->grades);
+ return this->grades;
+}
- void detailed_print()
- {
- for (int i = 0; i < psubj; i++)
- {
- std::cout << "Subject " << i+1 << ": ";
- std::cout << grades[i] << std::endl;
- }
- std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl;
- }
+void Student::add_grade(float grade)
+{
+ float *tmp = new float[psubj+1];
+ std::copy(this->grades, this->grades+psubj, tmp);
+ tmp[psubj] = grade;
+ this->grades = tmp;
+ psubj++;
+}
- float calc_average()
- {
- float sum = 0;
- for (int i = 0; i < psubj; i++)
- sum += grades[i];
- float average = sum / psubj;
- return average;
- }
-};
+void Student::detailed_print()
+{
+ for (int i = 0; i < psubj; i++)
+ {
+ std::cout << "Subject " << i+1 << ": ";
+ std::cout << grades[i] << std::endl;
+ }
+ std::cout << "Average grade: " << std::setprecision(2) << calc_average() << std::endl;
+}
+
+float Student::calc_average()
+{
+ float sum = 0;
+ for (int i = 0; i < psubj; i++)
+ sum += grades[i];
+ float average = sum / psubj;
+ return average;
+}
int main(int argc, char **argv)
{