CS 1430: Programming in C++.

Slides:



Advertisements
Similar presentations
Array. Convert Numbers in Different Base Systems Generate values to a series of numbers in different base systems: Base is between 2 and 9; Maximum number.
Advertisements

Class Scope class Student { private: string id; string firstName, lastName; float gpa; public: void Read() { cin >> id >> firstName >> lastName >> gpa;
Student Data Score First Name Last Name ID GPA DOB Phone... How to store student data in our programs? 1.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
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.
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:
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
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
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
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)
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes!
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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++.
CS 1430: Programming in C++ 1. File Input in VS Project Properties Debugging Command Arguments quiz8-1.out We want to know how to do it ourselves, right?
Arrays float Scores[9]; ? index: element // one dimensional array 1.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
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.
1 C++ Classes and Data Structures Course link…..
Test 2 Review Outline.
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Student Data Score First Name Last Name ID GPA DOB Phone ...
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++ No time to cover HiC.
Arrays An array is a collection of variables that all have the same name and the same data type. Each member of the array is known as an element of the.
CS 1430: Programming in C++.
Introduction to Programming
Standard Input/Output Stream
CS150 Introduction to Computer Science 1
CS 1430: Programming in C++.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 9: Data Structures: Arrays
CS150 Introduction to Computer Science 1
Boolean Variables & Values
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Class StudentList class StudentList { private: int numStudents;
Structure (i.e. struct) An structure creates a user defined data type
CS 1430: Programming in C++.
Selection Sorting S[] : array of int or float
Presentation transcript:

CS 1430: Programming in C++

Class Method Read class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // Yes! void Read() cin >> id >> firstName >> lastName >> gpa; } };

Class Method Write class Student { private: string id; string firstName, lastName; float gpa; public: // Will the method change any data members? // NO! void Write() cout << setw(6) << id << setw(15) << firstName << setw(15) << lastName << setw(8) << gpa; } };

Class Method Modifier const class Student { private: string id; string firstName, lastName; float gpa; Public: // The method will NOT change any data members: const. void Write() const cout << setw(6) << id << setw(15) << firstName << setw(15) << lastName << setw(8) << gpa; } // The method will change data members: no const. void Read() cin >> id >> firstName >> lastName >> gpa; };

Class Method Modifier const class Student { private: string id; string firstName, lastName; float gpa; public: void Write() const float getGPA() const string getFirst() const // No const void Read() void setGPA( float value ) void updateGPA( float amount ) . . . };

Comparing Objects Student s1, s2; // Input data s1.Read(); s2.Read(); //Comparing GPAs of s1 and s2 if (s1.getGPA() > s2.getGPA()) cout << s1.getFirst() << “ has higher GPA.”; else if (s1.getGPA() < s2.getGPA()) cout << s2.getFirst() << “ has higher GPA.”; else cout << s1.getFirst() << “ has the same GPA as ” << s2.getFirst();

Comparing GPA without getGPA Student s1, s2; // Input data s1.Read(); s2.Read(); //Comparing GPAs of s1 and s2 if ( ? ) cout << s1.getFirst() << “ has higher GPA.”; else if ( ? ) cout << s2.getFirst() << “ has higher GPA.”; else cout << s1.getFirst() << “ has the same GPA as ” << s2.getFirst(); Need a New Method!

Class Method HasHigherGPAThan class Student { private: string id; string firstName, lastName; float gpa; public: float getGPA() const string getFirst() const void Read() // Parameter is of class Student bool HasHigherGPAThan( Student s ) if ( gpa > s.gpa ) return true; else return false; } };

Class Method HasHigherGPAThan class Student { private: string id; string firstName, lastName; float gpa; public: /* Correct but not good. bool HasHigherGPAThan( Student s ) if ( gpa > s.gpa ) return true; else return false; } */ // Much better! return gpa > s.gpa; };

Comparing GPA without Knowing GPA Student s1, s2; // Input data s1.Read(); s2.Read(); // Comparing GPAs of s1 and s2 if ( s1.HasHigherGPAThan(s2) ) cout << s1.getFirst() << “ has higher GPA.”; else if (s2.HasHigherGPAThan(s1) ) cout << s2.getFirst() << “ has higher GPA.”; else cout << s1.getFirst() << “ has the same GPA as ” << s2.getFirst(); Don’t know the gpa of s1 or s2!

Class Method HasHigherGPAThan class Student { private: string id; string firstName, lastName; float gpa; public: // Does it change any data member? // NO! bool HasHigherGPAThan( Student s ) return gpa > s.gpa; } };

Class Method HasHigherGPAThan class Student { private: string id; string firstName, lastName; float gpa; public: // Will not change any data member: const bool HasHigherGPAThan( Student s ) const return gpa > s.gpa; } }; Passing Object as Parameter.

Passing Parameters Basic Data Types (char, int, float, string, bool) Pass by Value (without & in prototype) Pass by Reference (with & in prototype) Arrays (of any data type) Always Pass by Reference (Never &) Class Object (and structure) C++ allows pass by value (no &) and by reference (&) Our Rule: Always pass by reference with & Use const for in parameter

Passing Object as Parameter class Student { private: string id; string firstName, lastName; float gpa; public: // Will not change any data member: const // Will not change parameter s: const with & bool HasHigherGPAThan( const Student& s ) const return gpa > s.gpa; } };

Each array element of students[] is the same as variable s. Array of Objects const int MAX_SIZE = 35; int numStudents; // parallel array to keep students’ data float scores[MAX_SIZE]; string firstNames[MAX_SIZE]; // 35 Students Student students[MAX_SIZE]; // Single class object Student s; Each array element of students[] is the same as variable s.

Array of Objects const int MAX_SIZE = 35; int numStudents; Student s, students[MAX_SIZE]; // Input data s.Read(); students[0].Read(); // Output data s.Write(); students[1].Write(); // Update GPA s.UpdateGPA(0.2); students[index].UpdateGPA(0.5);

const int MAX_SIZE = 35; class Student { }; int main() int numStudents; Student s, students[MAX_SIZE]; cin >> numStudents; for (int i = 0; i < numStudents; i ++) students[i].Read(); . . . return 0; }

Using a function to read data to an array of Student Function Prototype Function Name InputToStudentArray Function Type void Parameters s[] : array of Student size: int, number of students of s[] // Parameters: (Out, Out) void InputToStudentArray(Student s[], int& size);

//-------------------------------------------------- // The function inputs value of size and then reads // data for size students into array s[]. // Parameters: (Out, Out) void InputToStudentArray(Student s[], int& size) { cin >> size; for (int i = 0; i < size; i++) s[i].Read(); return; }

//-------------------------------------------------- // The function displays data of size students // of array s[]. // Parameters: (In, In) void PrintStudentArray(const Student s[], int size) { for (int i = 0; i < size; i++) s[i].Write(); return; }

//-------------------------------------------------- // The function returns the index of the first // array element of s[] that has the same // firstName and lastName as student m. // Parameters: (In, In, In) int FindStudent(const Student s[], int size, const Student& m) { for (int i = 0; i < size; i++) if (s[i].getFirst() == m.getFirst() && s[i].getLast() == m.getLast()) return i; return -1; }

const int MAX_SIZE = 35; class Student { }; void InputToStudentArray(Student s[], int& size); void PrintStudentArray(const Student s[], int size); int FindStudent(const Student s[], int size, const Student& m); int main() . . . return 0; } // Function definitions

void InputToStudentArray(Student s[], int& size); void PrintStudentArray(const Student s[], int size); int FindStudent(const Student s[], int size, const Student& m); int main() { int numStudents, index; Student students[MAX_SIZE]; InputToStudentArray(students, numStudents); PrintStudentArray(students, numStudents); Student s(“Qi”, “Yang”, 2.9); index = FindStudent(students, numStudents, s); students[index].UpdateGPA(1.0); return 0; }

Starting Tuesday evening Schedule Quiz7-2 Due today by 10 PM Quiz7-4 Due Wednesday by 10 PM Lab 8 3 points by 5 PM, Tuesday Lab 9 Starting Tuesday evening

Pair Signup by Wednesday Schedule Prog4 Grace Time: 10 PM, Tuesday Prog5 Must do it in pairs! Pair Signup by Wednesday