السلام عليكم و رحمة الله و بركاته:أرجو شرح هذا الكود بالتقصيل ..الكود سهل لكن عقدتني الدالة student& addbouns
ولماذا استخدمناها؟و متى نستخدم الدوال التي مثل هذه التي ترجع object
#include <iostream>
using namespace std;
class Student{
private:
int mark;
public:
//Default constructor needed to create the array
Student(){}
Student(int m){mark=m;}
int getMark(){return mark;}
void setMark(int m){mark=m;}
void addMarks(int m){mark+= m;}
};
//array of students were given a bonus of 2 marks
Student& addBonus(Student& s){
s.addMarks(2);
return s;
}
//A function that takes an array of 5 students, uses the above function to give
//each student a bonus, then sets his mark to 100 if it exceeds 100
void enforceMax(Student arrayOfStudents[5]){
for(int i=0; i<5; i++){
if(addBonus(arrayOfStudents).getMark()>100){
arrayOfStudents.setMark(100);
}
cout<< "marks of Student: " << arrayOfStudents.getMark()<< endl;
}
}
int main(){
//create empty array of students.
Student s[5];
//populate array with students
s[0]=Student(20);
s[1]=Student(99);
s[2]=Student(70);
s[3]=Student(100);
s[4]=Student(85);
enforceMax(s);
return 0;
}