Can you please explain how this code outputs the following answers IN DETAIL!? Q
ID: 3817515 • Letter: C
Question
Can you please explain how this code outputs the following answers IN DETAIL!?
Q36. Hint: Writing out the private members' values for each object may be helpful [10 Marks] House keeping and elan claration include using namespace std, class Course public: course (string name, int maxstudents) -Course (void) void add student (string student) private: string courseName; string coursePtr int class size int studentcounter1 Member function definitons Constructor function definition Course: Course (string name, int maxStudents) course Name name studentcounter 0p classsize maxstudents; course Ptr new string[classsizel //destructor function definition Course: course (void) coutExplanation / Answer
I’ll explain the output line by line.
main function has created to Course objects. First is with courseName ES1036 and classSize 3, Second is with courseName ES1051 and classSize 2.
Using these two function multiple addStudent() is called with 4 different strings.
Output wrt each line is explained below: -
es1036.addStudent(hr) first checks for student counter which is 0 as of now and compares it with classSize. As it is less than classSize (which is 3), String hr(Harry) is added to coursePtr array and then this method prints the courseName,studentCounter and element coursePtr[studentCounter-1], i.e. first element of coursePtr which is harry. So the output is justified
es1036.addStudent(ro) first checks for student counter which is 1 as of now and compares it with classSize. As it is less than classSize (which is 3), String ro(Ron) is added to coursePtr array and then this method prints the courseName,studentCounter and element coursePtr[studentCounter-1], i.e. second element of coursePtr which is Ron. So the output is justified
es1036.addStudent(mn) first checks for student counter which is 2 as of now and compares it with classSize. As it is less than classSize(which is 3) , String mn(Hermione) is added to coursePtr array and then this method prints the courseName,studentCounter and element coursePtr[studentCounter-1], i.e. third element of coursePtr which is Hermione. So the output is justified
es1051.addStudent(dc) first checks for student counter which is 0(because it is different object) as of now and compares it with classSize. As it is less than classSize(which is 2) , String dc(Draco) is added to coursePtr array and then this method prints the courseName,studentCounter and element coursePtr[studentCounter-1], i.e. first element of coursePtr(array of second object) which is Draco. So the output is justified
es1051.addStudent(hr) first checks for student counter which is 1 as of now and compares it with classSize. As it is less than classSize(which is 2) , String hr(Harry) is added to coursePtr array and then this method prints the courseName,studentCounter and element coursePtr[studentCounter-1], i.e. second element of coursePtr(array of second object) which is Harry. So the output is justified
es1051.addStudent(ro) first checks for student counter which is 2 as of now and compares it with classSize. As it is now equal to classSize(which is 2) , So it will not enter the if block and cout prints the output as above.
es1051.addStudent(mn) first checks for student counter which is 2 as of now and compares it with classSize. As it is already equal to classSize(which is 2) , So it will not enter the if block and cout prints the output as above.
Last two lines are because of destructor called as main method has finished the object calling.
Destructor of ES1051 is called first because its constructor was called second and after than destructor of ES1036 is called. This phenomenon is explained below: -
If two objects of a class is used and object 1 is initialized before object 2, then destructor of object 2 will be called before destructor of object 1. it follows LIFO principle.