4.7 Student Class: Nested ifelse Statements

The example of Figs. 4.64.7 demonstrates a nested ifelse statement that determines a student’s letter grade based on the student’s average in a course.

Class Student

Class Student (Fig. 4.6) stores a student’s name and average and provides member functions for manipulating these values. The class contains:

  • Data member name of type string (line 65) to store a Student’s name.

  • Data member average of type int (line 66) to store a Student’s average in a course.

  • A constructor (lines 8–13) that initializes the name and average.

  • Member functions setName and getName (lines 16–23) to set and get the Student’s name.

  • Member functions setAverage and getAverage (lines 26–39) to set and get the Student’s average—in Section 5.11, you’ll learn how to express lines 29–30 more concisely with logical operators that can test multiple conditions.

  • Member function getLetterGrade (lines 42–63), which uses nested ifelse statements to determine the Student’s letter grade based on the Student’s average.

After the constructor initializes name in the member-initializer list, the constructor calls member function setAverage, which uses nested if statements (lines 29–33) to validate the value used to set the average. These statements ensure that the value is greater than 0 and less than or equal to 100; otherwise, average’s value is left unchanged. Each if statement contains a simple condition—i.e., one that makes only a single test. In Section 5.11, you’ll see how to use logical operators to write compound conditions that conveniently combine several simple conditions. If the condition in line 29 is true, only then will the condition in line 30 be tested, and only if the conditions in both lines 29 and 30 are true will the statement in line 31 execute.


Fig. 4.6 Student class that stores a student name and average.

Alternate View

 1   // Fig. 4.6: Student.h
 2   // Student class that stores a student name and average.
 3   #include <string>
 4
 5   class Student {
 6   public:
 7       // constructor initializes data members
 8       Student(std::string studentName, int studentAverage)
 9          : name(studentName) {
10
11          // sets average data member if studentAverage is valid
12          setAverage(studentAverage);
13       }
14
15       // sets the Student's name
16       void setName(std::string studentName) {
17          name = studentName;
18       }
19
20       // retrieves the Student's name
21       std::string getName() const {
22          return name;
23       }
24
25       // sets the Student's average
26       void setAverage(int studentAverage) {
27          // validate that studentAverage is > 0 and <= 100; otherwise,
28          // keep data member average's current value
29          if (studentAverage > 0) {                               
30             if (studentAverage <= 100) {                         
31                average = studentAverage; // assign to data member
32             }                                                    
33          }                                                       
34      }
35
36      // retrieves the Student's average
37      int getAverage() const {
38         return average;
39      }
40
41      // determines and returns the Student's letter grade
42      std::string getLetterGrade() const {
43         // initialized to empty string by class string's constructor
44         std::string letterGrade;
45
46         if (average >= 90) {     
47            letterGrade = "A";    
48         }                        
49         else if (average >= 80) {
50            letterGrade = "B";    
51         }                        
52         else if (average >= 70) {
53            letterGrade = "C";    
54         }                        
55         else if (average >= 60) {
56            letterGrade = "D";    
57         {                        
58         else {                   
59            letterGrade = "F";    
60         }                        
61
62         return letterGrade;
63     }
64   private:
65      std::string name;
66      int average{0}; // initialize average to 0
67   } // end class Student

Class StudentTest

To demonstrate the nested ifelse statements in class Student’s getLetterGrade member function, the main function (Fig. 4.7) creates two Student objects (lines 8–9). Next, lines 11–16 display each Student’s name, average and letter grade by calling the objects’ getName, getAverage and getLetterGrade member functions, respectively.

Fig. 4.7 Create and test Student objects.

Alternate View

 1   // Fig. 4.7: StudentTest.cpp
 2   // Create and test Student objects.
 3   #include <iostream>
 4   #include "Student.h"
 5   using namespace std;
 6
 7   int main() {
 8      Student account1{"Jane Green", 93};
 9      Student account2{"John Blue", 72};
10
11      cout << account1.getName() << "'s letter grade equivalent of "
12         << account1.getAverage() << " is: "
13         << account1.getLetterGrade() << "
";
14      cout << account2.getName() << "'s letter grade equivalent of "
15         << account2.getAverage() << " is: "
16         << account2.getLetterGrade() << endl;
17   }

Jane Green's letter grade equivalent of 93 is: A
John Blue's letter grade equivalent of 72 is: C
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset