// ΛΥΣΕΙΣ ΘΕΜΑΤΩΝ ΙΟΥΝΗ 2016 // ΘΕΜΑ 4. #include #include #define NMAX 35 using namespace std; #define FOR(i,start,end) for (int i=start; i < end; i++) class Student{ private: string onoma, eponymo; unsigned int AM; // NMAX is a macro defined above float grades[NMAX]; public: Student(string onom, string epon, int am){ onoma = onom; eponymo = epon; AM = am; //the following loop //for (int i = 0; i < NMAX; i++) grades[i] = 0.0; //can been substituted by the macro defined above FOR(i, 0, NMAX) grades[i] = 0.0; } // copy constructor Student(const Student &st){ onoma = st.onoma; eponymo = st.eponymo; AM = st.AM; FOR(i, 0, NMAX) grades[i] = st.grades[i]; } ~Student() { cout << "Διαγραφή !\n"; cout << "Όνομα:" << onoma << "\n"; cout << "Επώνυμο:" << eponymo << "\n"; cout << "AM:" << AM << "\n\n"; //cout << "Μάθημα" << "--" << "Βαθμός\n"; //FOR(i, 0, NMAX) cout << i << " " << grades[i] << "\n"; " } void set(int mathima, float grade){ grades[mathima] = grade; } bool test(float grades[]){ int cpass = 0; FOR(i,0,NMAX) { if (grades[i] >= 5) cpass++; } return (cpass == NMAX); // αν cpass == 35 τότε True, αλλιώς False } float mo_grades(){ float sum = 0.0; FOR(i,0,NMAX) { if (this->grades[i] >= 5) sum += this->grades[i]; } return sum/float(NMAX); } bool operator== (const Student &stu){ if (onoma == stu.onoma && eponymo == stu.eponymo && AM == stu.AM){ FOR(i, 0, NMAX){ if (grades[i] != stu.grades[i]) return false; } return true; } else return false; } friend void print(Student); // τώρα η print() έχει πρόσβαση στις μεταβλητές κ' συναρτήσεις της Student }; void print(Student s){ cout << "Όνομα: " << s.onoma << "\n" << "Επώνυμο: " << s.eponymo << "\n" << "AM: " << s.AM << "\n" << "Μάθημα" << "--" << "Βαθμός\n"; FOR(i, 0, NMAX) cout << i << " " << s.grades[i] << "\n"; cout << "Μέσος όρος: " << s.mo_grades() << "\n"; } int main(){ Student s1("Kostas","Mixos",1050333), s2("Mike","Lamar",1050111), *s3 = new Student("Maria","Pentagioth",1050222); s1.set(1,7.5); s1.set(2,7); s1.set(3,8); s2.set(1,6.0); s2.set(2,3.0); s2.set(3,7.0); s3->set(1,5.0); s3->set(2,9.0); s3->set(3,8.0); // Παράδειγμα χρήσης του τελεστή == if (s1==s2) cout "true\n"; cout << s1.mo_grades() << endl; cout << s2.mo_grades() << endl; cout << s3->mo_grades() << endl; // Προτιμώ να ελευθερώσω την μνήμη καλώντας τον καταστροφέα //delete s3; s3->~Student(); return 0; }