Lab 3
Lab 3
Lab Assignment # 3
Lab Objectives:
Learn how to define a simple class and implement its member functions.
Learn how to use getters and setters
Learn how to use a user-defined class in an application.
Q1 50
Q2 50
total 100
QUESTION 1: Assume that we want to track student’s information of a specific major.
Each student takes the same 20 mandatory courses during his/her study. Also, each
student has an ID and a name. Write a C++ Student class to represent a Student as the
following:
1. Define member variables to represent the id of type integer, name of type string, and
grades as an array of integers of size 20. (6 marks)
2. A default constructor that initializes the student id to 0, name to “”, and grades to 0.
(4 marks)
3. An initializer constructor that receives values for id and name, sets and grades to 0.
(4 marks)
4. Define a setter and getter for ID, and name. (12 marks)
5. Define a setter for grades which receives an array of integers that represent the
grades of courses for the students. Assume the size of the received array is 20. Note
that grades take value between 0 and 100. (7 marks)
6. float GetGPA() member function that returns the GPA of the student. This
represents the average of student’s grades. (5 marks)
7. int AboveGPA() member function that returns the number of grades that are higher
than the students GPA. (6 marks)
8. int LessThan(int mark) member function that returns the number of grades that are
less than the given mark parameter. (6 marks)
QUESTION 2: Write a main function that uses the Student class done in the previous
question. In the main function, do the following:
1. Define two Student objects s1 and s2 where s1 uses the default constructor and s2
uses the initializer constructor. Initialize s2 with user inputs for ID and name. (10
marks)
2. Define two arrays of integers of size 20, mark1, and mark2. Let the user fill them.
(10 marks)
3. Set mark1 as grades of student s1, and mark2 as grades for student s2. (6 marks)
4. Write a separate function called HigherGPA that receives two student objects and
return a Boolean value. The function should return true if the first student has higher
GPA, false otherwise. (6 marks)
5. Call the function HigherGPA using students s1, and s2. And print out “s1 has higher
GPA than s2” or “s2 has higher or equal GPA to s1”. (4 marks)
6. Write a separate function called MoreGrades that receives two student objects and
return a Boolean value. The function should return true if the first student has more
grades above his/her GPA than other student’s number of grades higher than his/her
GPA, false otherwise. (6 marks)
7. Call the function MoreGrades using students s1, and s2. And print out “s1 has more
number of grades higher than his/her GPA” or “s2 has more number of grades higher
than his/her GPA”. (4 marks)
8. In the main function print how many marks in object s1 less than 60. (4 marks).
GOOD LUCK