Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1430: Programming in C++.

Similar presentations


Presentation on theme: "CS 1430: Programming in C++."— Presentation transcript:

1 CS 1430: Programming in C++

2 Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
Day today; today = MON; if (today == FRI) cout << "We have a quiz or a test!"; else if (today == THU) cout << “We have a Lab!"; else if (today == SAT) cout << “Party!"; else cout << “What to do?”;

3 Enumeration Data Type and const int
enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) const int SUN = 0; const int MON = 1; const int TUE = 2; const int WED = 3; const int THU = 4; const int FRI = 5; const int SAT = 6; int today; today = 10;

4 Enumeration Data Type Operations on enum type assignment comparison
Function return value Function parameters: & No Input/Output!

5 Enumeration Data Type enum Status {FRESHMAN, SOPHOMORE, JUNIOR,
SENIOR}; enum Status {FRESHMAN = 1, SOPHOMORE, JUNIOR, enum Status {SENIOR = 4, JUNIOR = 3, FRESHMAN = 1, SOPHOMORE = 2}; enum Status {JUNIOR = 3, SENIOR, FRESHMAN = 1, SOPHOMORE};

6 Using enum Type in C++ Classes
enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; class StudentType { private: string id; string firstName, lastName; float gpa; Status standing; public: . . . };

7 Method Read of Class StudentType
void Read() { cin >> id >> firstName >> lastName >> gpa; int credits; cin >> credits; if (credits <= 30) standing = FRESHMAN; else if (credits <= 60) standing = SOPHOMORE; else if (credits <= 90) standing = JUNIOR; else standing = SENIOR; return; }

8 Private Method Status ReadStanding() { int credits;
cin >> credits; if (credits <= 30) return FRESHMAN; else if (credits <= 60) return = SOPHOMORE; else if (credits <= 90) return = JUNIOR; else return = SENIOR; }

9 Method Read of Class StudentType
void Read() { cin >> id >> firstName >> lastName >> gpa; standing = ReadStanding(); return; }

10 Method GetStandig class StudentType { private: public: Status GetStanding() const return standing; } };

11 Method Write of Class StudentType
void Write() const { cout << endl << setw(25) << id << endl << setw(25) << firstName << endl << setw(25) << lastName << endl << setw(25) << gpa << endl << setw(25) << GetStanding(); return; } // Correct? // No! // No input/output on enum types!

12 Method Write of Class StudentType
void Write() const { cout << endl << setw(25) << id << endl << setw(25) << firstName << endl << setw(25) << lastName << endl << setw(25) << gpa; if (standing == FRESHMAN) cout << endl << setw(25) << “FRESHMAN”; else if (standing == SOPHOMORE) cout << endl << setw(25) << “SOPHOMORE”; else if (standing == JUNIOR) cout << endl << setw(25) << “JUNIOR”; else cout << endl << setw(25) << “SENIOR”; return; }

13 Private Method void DisplayStanding() const {
if (standing == FRESHMAN) cout << endl << setw(25) << “FRESHMAN”; else if (standing == SOPHOMORE) cout << endl << setw(25) << “SOPHOMORE”; else if (standing == JUNIOR) cout << endl << setw(25) << “JUNIOR”; else cout << endl << setw(25) << “SENIOR”; return; }

14 Method Write of Class StudentType
void Write() const { cout << endl << setw(25) << id << endl << setw(25) << firstName << endl << setw(25) << lastName << endl << setw(25) << gpa; DisplayStanding(); return; }

15 Private Method string Standing() const { string s;
if (standing == FRESHMAN) s = “FRESHMAN”; else if (standing == SOPHOMORE) s = “SOPHOMORE”; else if (standing == JUNIOR) s = “JUNIOR”; else s = “SENIOR”; return s; }

16 Method Write of Class StudentType
void Write() const { cout << endl << setw(25) << id << endl << setw(25) << firstName << endl << setw(25) << lastName << endl << setw(25) << gpa; << endl << setw(25) << Standing(); return; }

17 enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
class StudentType { private: string id, firstName, lastName; float gpa; Status standing; Status ReadStanding() string Standing() public: StudentType() StudentType(string sid, string first, string last, float sgpa) StudentType(const StudentType& s) void Read() void Write() const Status GetStanding() const bool Equals(const StudentType& s) const // if (s1.Equals(s2)) . . . };

18 Class SectionType const int MAX_SIZE = 30;
enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; class StudentType { . . . }; class SectionType private: int numStudents; StudentType students[MAX_SIZE]; public: void Read()

19 Class SectionType const int MAX_SIZE = 26;
enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; class StudentType class SectionType { private: int numStudents; StudentType students[MAX_SIZE]; public: void Read() cin >> numStudents; for (int i = 0; i < numStudents; i ++) students[i].Read(); } . . . };

20 const int MAX_SIZE = 26; enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; class StudentType class SectionType int main() { SectionType CS143_S2; CS143_S2.Read(); // Find the freshman student with the highest GPA among // all freshman students in CS143_S2 }

21 class SectionType { private: int numStudents; StudentType students[MAX_SIZE]; . . . public: StudentType FreshmanStudentOfMaxGPA() const int index; float maxGPA = 0.0; for (int i = 0; i < numStudents; i ++) if (students[i].GetStanding() == FRESHMAN) float gpa = students[i].GetGPA(); if (gpa >= maxGPA) index = i; maxGPA = gpa; } return students[index]; // assuming at least one freshman };

22 const int MAX_SIZE = 26; enum Status {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR}; class StudentType class SectionType int main() { SectionType CS143_S2; CS143_S2.Read(); StudentType s = CS143_S2.FreshmanStudentOfMaxGPA(); cout << “The Freshman student with highest GPA ” << “ among all freshman students in Section 2 of CS143: ”; s.Write(); . . . }

23 Schedule Lab 9 Lab 10 Program 5 Group Assignment

24 Helper Lab Assistants Lab Pals Extra Helper: Zach Gerner
6 PM – 8 PM, Mon - Thu Lab Pals 9 AM – 3 PM, Thu Extra Helper: Zach Gerner 4 PM – 6 PM, Tuesday


Download ppt "CS 1430: Programming in C++."

Similar presentations


Ads by Google