0

(OOP) Detailed explination needed!

So I have this code which has basic Object Oriented Programming features within C++, and I understand how it works except for this part: Employee youngest (Employee *e, int n) { Employee min = e[0]; for (int i = 1; i < n; i++) { if (e[i].get_dateOfBirth().compare(min.get_dateOfBirth()) == 1) min = e[i]; } return min; } I don't understand how the if statement works here and how the "." things work. if anyone can explain and/or has similar exercises for me to try out, please feel free. This is the function within the class Date int compare(const Date date) { if (year > date.year) return 1; else if (year < date.year) return -1; else if (month > date.month) return 1; else if (month < date.month) return -1; else if (day > date.day) return 1; else if (day < date.day) return -1; else return 0; } I can't take a picture, and I'm new at this app, so if anyone can DM me, any help is appreciated really.

31st Mar 2020, 7:58 PM
Bate Shuco
Bate Shuco - avatar
2 Antworten
0
The dot operator '.' is used to access attributes and member functions, e.g. the call e[ i ].get_dateOfBirth() invokes the get_dateOfBirth() method on an Employee object and returns the date that is stored within that Employee. So in the if-statement, we fetch the birth dates of the current employee and the one that is currently considered youngest through calls to their get_dateOfBirth() methods, and compare them by invoking the compare() method on the first date, while passing the second date as the parameter. If the birth date of the current employee is bigger than the one of the currently youngest one, that means the current employee is actually younger and we should update that. However, if the compare() method returns -1 instead, the current employee is older and we can continue.
31st Mar 2020, 8:36 PM
Shadow
Shadow - avatar
0
Thanks, although I'll still need more examples, I understand the concept now... What was unclear was how the command in the if statement actually worked, because 2 dot operators looked confusing at first.
31st Mar 2020, 9:06 PM
Bate Shuco
Bate Shuco - avatar