CS 1430: Programming in C++.

Slides:



Advertisements
Similar presentations
Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Advertisements

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 6. Simple and User Defined Data Types.
Define our own data types We can define a new data type by defining a new class: class Student {...}; Class is a structured data type. Can we define our.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
Structure TDate struct TDate { int year, month, day; }; // Define a new data type.
Selection Sorting S[] : array of int or float Size: number of elements of s[] Pseudocode for i = 0 to size - 2 find the index of a smallest element between.
1 Cannon_Chapter9 Strings and the string Class. 2 Overview  Standards for Strings  String Declarations and Assignment  I/O with string Variables 
Class Constructors class Student { private: string id, firstName, lastName; float gpa; public: Student() Student(string sID) Student(string first, string.
Quiz // // The function exchanges the two parameters. // Param: ( ) // Param:
Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA(
CS 1430: Programming in C++ 1. Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
CS 1430: Programming in C++.
Data Types Storage Size Domain of all possible values Operations 1.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
Overview of also works.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
CS 1430: Programming in C++.
Nested Structures struct TDate { int year, month, day; }; struct StudentType { string id, firstName, lastName; float gpa; TDate DOB; }; struct SectionType.
Selection Sorting Pseudocode (Forward) for i = 0 to size - 2 find the index of the required element between s[i] and s[size - 1] If i not the same as index.
5 Day Forecast Mon Tues Wed Thu Fri.
LESSON 06.
Jeff West - Quiz Section 2
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
GANTT CHARTS Example Example Example Example text Tasks Example 1
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Mon – 2Tue – 3Wed – 4Thu - 5Fri - 6Sat - 7Sun - 8 Mon – 9Tue – 10Wed – 11Thu - 12Fri – 13Sat – 14Sun -15 Mon – 16Tue – 17Wed – 18Thu - 19Fri – 20Sat –
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...

CS 1430: Programming in C++.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
MON TUE WED THU
Chapter 5 Function Basics
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
CS 1430: Programming in C++.

January Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
January MON TUE WED THU FRI SAT SUN
CS 1430: Programming in C++.
2008 Calendar.
January MON TUE WED THU FRI SAT SUN
Sun Mon Tue Wed Thu Fri Sat
Visual For Weekly and Monthly Schedule
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
January MON TUE WED THU FRI SAT SUN
JANUARY 1 Sun Mon Tue Wed Thu Fri Sat
Sun Mon Tue Wed Thu Fri Sat
Class StudentList class StudentList { private: int numStudents;
1/○~1/○ weekly schedule MON TUE WED THU FRI SAT SUN MEMO
January MON TUE WED THU FRI SAT SUN
S M T W F S M T W F
2016 | 10 OCT SUN MON TUE WED THU FRI SAT
Sun Mon Tue Wed Thu Fri Sat
WEB PAGES: Tables Welcome Back !.
1 January 2018 Sun Mon Tue Wed Thu Fri Sat
CS 1430: Programming in C++.
Structures Chapter 4.
2008 Calendar.
Selection Sorting S[] : array of int or float
Presentation transcript:

CS 1430: Programming in C++

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?”;

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;

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

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};

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: . . . };

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; }

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; }

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

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

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!

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; }

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; }

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; }

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; }

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; }

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)) . . . };

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()

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(); } . . . };

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 }

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 };

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(); . . . }

Schedule Lab 9 Lab 10 Program 5 Group Assignment

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