Presentation is loading. Please wait.

Presentation is loading. Please wait.

Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.

Similar presentations


Presentation on theme: "Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1."— Presentation transcript:

1 Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1

2 Store Student Data in Variables // For one student at a time float score, GPA; string firstName, lastName, ID; … 2

3 Store Students Data in Parallel Arrays const int MAX_SIZE = 30; // To keep data for all students float scores[MAX_SIZE], GPA[MAX_SIZE]; string firstName[MAX_SIZE], lastName[MAX_SIZE], ID[MAX_SIZE]; 3

4 C++ Structure struct Student { string id; string firstName, lastName; float gpa; }; // Student is a data type! 4

5 Structure Variables int numStudents; float score; Student s1, s2; // Student is a data type! 5

6 Accessing Members of a Structure Variable Student s1; // Input data into struct cout << "Enter ID: "; cin >> s1.id; cout << "Enter first name: "; cin >> s1.firstName; // Output student data cout << setw(9) << s1.id << setw(25) << s1.firstName; Using the dot notation! 6

7 C++ Class class Student { string id; string firstName, lastName; float gpa; }; // Student is a class (data type)! // By default, all fields are private. 7

8 C++ Class class Student { private: string id; string firstName, lastName; float gpa; public:... }; Create public functions (methods) to access private fields. 8

9 Class Methods class Student { private: string id; string firstName, lastName; float gpa; public: // Make sure the order is correct. void Read() { cin >> id >> firstName >> lastName >> gpa; } }; 9

10 Class Methods class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Print() { cout << endl; cout << setw(9) << id << setw(20) << firstName << setw(20) << lastName << setw(5) << gpa; } }; 10

11 Calling Class Methods Student s1; // Input data into object s1 s1.Read(); // Output data of object s1 s1.Print(); Using the dot notation! 11

12 Syntax and Style class Student { private: string id; string firstName, lastName; float gpa; public: void Read()... }; // class, private, public: key word // Student: Identifier, your choice // Fields : Declaring variables // Braces // Semicolon after } // Read, Print: class methods (function inside class) // Indentation 12

13 Semantics class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa; }... }; Student is a new data type! It has data fields and methods (functions) on the data. 13

14 Semantics class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa; }... }; Data fields have class scope and can be accessed from any class methods! 14

15 C++ Classes cin >> base; while ( !cin.eof() && (base 9) ) { // display message cin.ignore(MAX_LINE_SIZE, ‘\n’); cin >> base: } 15

16 More Class Methods class Student { private:... public:... string getFirst() { return firstName; } void setFirstName( string name ) { firstName = name; } }; 16

17 More Class Methods class Student { private:... public:... string getGPA() { return gpa; } void setGPA( float value ) { gpa = value; } void updateGPA( float amount ) { gpa += amount; } }; 17

18 Calling Class Methods Student s1, s2; // Input data s1.Read(); s2.Read(); // Output data s1.Print(); s2.Print(); 18

19 // Comparing two students if ( s1.getGPA() > s2.getGPA() ) cout << “\nFirst student has higher GPA."; else if ( s1.getGPA() < s2.getGPA() ) cout << “\nSecond student has higher GPA."; else cout << “\nThe two student have the same GPA."; 19 Calling Class Methods

20 // Updating students data s1.setGPA( 2.9 ); s1.updateGPA( 0.5 ); if ( s1.getGPA() > s2.getGPA() ) cout << “\nFirst student has higher GPA."; else if ( s1.getGPA() < s2.getGPA() ) cout << “\nSecond student has higher GPA."; else cout << “\nThe two student have the same GPA."; 20 Calling Class Methods

21 class Student {... } int main() { Student s1, s2; s1.Read(); s2.Read(); s1.Print(); s2.Print(); // Comparing GPA of s1 and s2 s1.updateGPA( 0.5 ); // Comparing GPA of s1 and s2 return 0; } 21

22 // // Comment Block // // Includes // constants class Student {... } // Function prototypes int main() {... return 0; } // Function definitions 22

23 Schedule Quiz5-5: Due today Quiz7-1: Due Wednesday Wednesday: Test2 Review Thursday: Lab 8 Friday Test 2: 60 points Notes Program 4 Due Wednesday, April 6 Grace Friday, April 8 23

24 Quiz5-6 Now 24


Download ppt "Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1."

Similar presentations


Ads by Google