CS 1430: Programming in C++.

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

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.
1. List Static List: no adding or deleting Dynamic List: can add or delete items from the list Both static and dynamic lists: linear search, update item.
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.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT}; Day today; today = WED; if (today == FRI) cout
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
1 Inheritance Concepts n Derive a new class (subclass) from an existing class (base class or superclass). n Inheritance creates a hierarchy of related.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
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.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
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
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.
CS 1430: Programming in C++.
CS 1430: Programming in C++ 1. Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
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++.
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?
Array, ArrayList and List ISYS 350. Array An array allows you to store a group of items of the same type together. Processing a large number of items.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
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…..
Passing Objects to Methods
Array ISYS 350.
Section 3 Review Mr. Crone.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS 1430: Programming in C++.
A solution to a list of records
Reserved Words.
Array ISYS 350.
CS 1430: Programming in C++.
Array ISYS 350.
CS 1430: Programming in C++.
Student Data Score First Name Last Name ID GPA DOB Phone ...
Array ISYS 350.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
CS 1430: Programming in C++.
CS 1430: Programming in C++.
CSC 113 Tutorial QUIZ I.
CS 1430: Programming in C++.
Enumeration Data Type enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
CS 1430: Programming in C++ No time to cover HiC.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Array ISYS 350.
CS 1430: Programming in C++ No time to cover HiC.
Standard Input/Output Stream
CS 1430: Programming in C++.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Nate Brunelle Today: Conditional Decision Statements
Array ISYS 350.
CS150 Introduction to Computer Science 1
(Dreaded) Quiz 2 Next Monday.
Class StudentList class StudentList { private: int numStudents;
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
CS 1430: Programming in C++.
Selection Sorting S[] : array of int or float
Presentation transcript:

CS 1430: Programming in C++

Class Constructors class Student { private: string id, firstName, lastName; float gpa; . . . public: Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) };

Creating Class Objects // Must have the default constructor Student s; Student s1(); // Creating a Student object by calling // Student(string first, string last, float g) Student s2(“Qi”, “Yang”, 2.9); string first, last; float myGPA; cin >> last >> first >> myGPA; Student s3(first, last, myGPA);

Creating Class Objects // Creating a Student object by calling a constructor Student s = Student(“Qi”, “Yang”); // Override data of s s.Read(); // Creating an object and assign it to s s = Student(“13579”, “Joe”, “Clifton”, 3.9); string first, last; float myGPA; cin >> last >> first >> myGPA; s = Student(first, last, myGPA);

Array of Objects int numStudents; string id, first, last; float myGPA; // Must have default constructor Student s, allStudents[30]; cin >> id >> last >> first >> myGPA; // Creating an object and assign it to s s = Student(id, first, last, myGPA); // Copy s to allStudents[0] allStudents[0] = s;

Array of Objects int numStudents; string id, first, last; float myGPA; Student s, allStudents[30]; cin >> numStudents; for (int i = 0; i < numStudents; i ++) { cin >> id >> last >> first >> myGPA; s = Student(id, first, last, myGPA); allStudents[i] = s; }

Array of Objects int numStudents; string id, first, last; float myGPA; Student s, allStudents[30]; cin >> numStudents; for (int i = 0; i < numStudents; i ++) { cin >> id >> last >> first >> myGPA; //s = Student(id, first, last, myGPA); allStudents[i] = Student(id, first, last, myGPA); }

Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: void Read() string id, first, last; float myGPA; cin >> numStudents; for (int i = 0; i < numStudents; i ++) cin >> id >> last >> first >> myGPA; allStudents[i] = Student(id, first, last, myGPA); } };

Class StudentList class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: void Read() cin >> numStudents; for (int i = 0; i < numStudents; i ++) // Assuming class Student has method Read() allStudents[i].Read(); } void Write() const // Print all students one by one };

Class Student and StudentList { private: . . . public: void Read() void Write() const }; class StudentList

Class Student and StudentList { . . . }; class StudentList int main() StudentList cs143_S2; cs143_S2.Read(); cs143_S2.Write(); return 0; }

More Methods for Class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: . . . float MaxGPA() const float max = students[0].GetGPA(); for (int i = 1; i < numStudents; i ++) if (students[i].GetGPA() > max) max = students[i].GetGPA(); return max; } };

Class Student and StudentList { }; class StudentList int main() StudentList cs143_S2; float maxGPA; cs143_S2.Read(); cs143_S2.Write(); maxGPA = cs143_S2.MaxGPA(); cout << “The max GPA for CS143 Section 2 is ” << maxGPA; return 0; }

More Methods for Class StudentList { . . . public: float MaxGPA() const float MinGPA() const float AverageGPA() const void GetStats(float& max, float& min, float& avg) const float gpa; max = min = avg = students[0].GetGPA(); for (int i = 1; i < numStudents; i ++) gpa = students[i].GetGPA(); if (gpa > max) max = gpa; if (gpa < min) min = gpa; avg += gpa; } avg /= numStudents; };

Class Student and StudentList class StudentList int main() { StudentList cs143_S2; float maxGPA, minGPA, avgGPA; cs143_S2.Read(); cs143_S2.Write(); cs143_S2.GetStats(maxGPA, minGPA, avgGPA); cout << “CS143 Section 2” << endl; cout << “ Max GPA: ” << maxGPA << endl << “ Min GPA: ” << minGPA << endl << “ Avg GPA: ” << avgGPA; return 0; }

More Methods for Class StudentList { private: int numStudents; Student students[MAX_SIZE]; public: . . . Student MaxGPAStudent() const int index = 0; for (int i = 1; i < numStudents; i ++) if (students[i].GetGPA() > students[index].GetGPA()) index = i; return students[index]; } };

Class Student and StudentList { }; class StudentList int main() StudentList cs143_S2; Student s; cs143_S2.Read(); cs143_S2.Write(); s = cs143_S2.MaxGPAStudent(); cout << “The student with max GPA for CS143 Section 2 is\n”; s.Write(); return 0; }

More Methods for Class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const for (int i = 0; i < numStudents; i++) if (students[i].GetFirst() == s.GetFirst() && students[i].GetLast() == s.GetLast()) return i; return -1; } public: . . . void UpdateStudentGPA(const Student& s, float g) // Need to find where s is in students[] };

More Methods for Class StudentList { private: int numStudents; Student students[MAX_SIZE]; int find(const Student& s) const public: . . . bool UpdateStudentGPA(const Student& s, float g) int index = find(s); if (index == -1) return false; students[index].UpdateGPA(g); return true; } };

Class Student and StudentList int main() { StudentList cs143_S2; Student s; cs143_S2.Read(); cs143_S2.Write(); s = Student(“Qi”, “Yang”); if (cs143_S2.UpdateStudentGPA(s, 0.5)) cout << endl << “GPA is updated for ” << s.GetFirst() << “ ” << s.GetLast() << ‘.’; } else cout << endl << s.GetFirst() << “ ” << s.GetLast() << “ is not in CS143 Section 2.”; return 0;

Schedule Quiz7-4 Lab 9 Program 5 Pairs Sign up Today