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 Student Data First Name Last Name ID GPA DOB Phone ...
How to store student data in our programs?

3 Store Student Data in Variables
// For one student at a time float gpa; string firstName, lastName, id;

4 Store Students Data in Parallel Arrays
const int MAX_SIZE = 30; // To keep data for all students Float gpas[MAX_SIZE]; string firstNames[MAX_SIZE], lastNames[MAX_SIZE], ids[MAX_SIZE]; . . .

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

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

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

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

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

10 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

11 Semantics Student is a new data type!
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.

12 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!

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

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

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

16 Calling Class Methods // 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.";

17 Calling Class Methods // 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.";

18 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 ); return 0; }

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

20 Schedule Quiz5-5: Due today Lab 7 Quiz7-1: Due Monday
Test 2: 60 points Notes Program 4 Due Thursday, Nov 5

21 Quiz2 Now


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

Similar presentations


Ads by Google