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

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;
Introduction to Programming Lecture 39. Copy Constructor.
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
Introduction to Programming Lecture 34. In Today’s Lecture Arrays of objects Arrays of objects Interaction of Arrays with Free Store Interaction of Arrays.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
CS 2430 Day 14. Agenda for today The Bag class No mixing! What if we have a bunch of String s that we want to store in our program? We can’t use the.
Sorting int s[20], size; size = 5; Original array Final array (in Ascending Order) Final array (in Descending Order)
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Data Type string #include // C++ String class string str1, str2; // Default constructor cin >> str1 >> str2; cout
C++ Arrays. Agenda What is an array? What is an array? Declaring C++ arrays Declaring C++ arrays Initializing one-dimensional arrays Initializing one-dimensional.
Data Structures.  Consider storing data for 100 employees by their Social Security Numbers (SSN)  SSN range: –  A fast search:
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 3 More About Classes Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
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.
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.
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 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
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()
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?
CS32 Discussion Section 1B Week 2 TA: Hao Yu (Cody)
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.
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
A solution to a list of records
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.
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};
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.
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++.
CS 1430: Programming in C++.
Array ISYS 350.
(Dreaded) Quiz 2 Next Monday.
Class StudentList class StudentList { private: int numStudents;
CS 1430: Programming in C++.
Selection Sorting S[] : array of int or float
Presentation transcript:

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) Student(const Student& s) }; 1

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; // Creating a Student object by calling // Student(string first, string last, float g) Student s3(first, last, myGPA); 2

Creating Class Objects // Works even without default 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; // Creating an object and assign it to s s = Student(first, last, myGPA); 3

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

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

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

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

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

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

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

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

Class Student and StudentList class Student { }; 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; } 12

More Methods for Class StudentList 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; } }; 13

Class Student and StudentList class Student 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; } 14

More Methods for Class StudentList 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]; } }; 15

Class Student and StudentList class Student { }; 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; } 16

More Methods for Class StudentList 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[] } }; 17

More Methods for Class StudentList 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; } }; 18

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() << ‘.’; CS143_S2.Write(); } else cout << endl << s.GetFirst() << “ ” << s.GetLast() << “ is not in CS143 Section 2.”; return 0; } 19

Schedule Lab 9: Grace Next Wednesday Program 5 Pairs Today 20