200 likes | 364 Views
Brendan Murphy bemurphy@cs.umass.edu. CS 377 Discussion 1. Administrative Stuff. Purpose of this discussion section Keep up with reading and lectures (short quizzes) Review course material (working out problems) Office hours Monday: 1:15 – 2:15 Friday: 2:30 – 3:30 Room CS207
E N D
Brendan Murphy bemurphy@cs.umass.edu CS 377 Discussion 1
Administrative Stuff • Purpose of this discussion section • Keep up with reading and lectures (short quizzes) • Review course material (working out problems) • Office hours • Monday: 1:15 – 2:15 • Friday: 2:30 – 3:30 • Room CS207 • Or by appointment (bemurphy@cs.umass.edu)
Today: C++ Review • C++ concepts • Structs • Classes • Pointers • Worksheet + Programming Assignment
Structs • Collection of data, short for data structure • Can have different data types • Example:
Structs • Can have multiple instances • Examples:
Structs • Fields are inherently public • Example:
Classes • Much like structs, except usually contain functions • Fields are inherently private • Fields and functions grouped by access specifiers • Can have multiple instances (objects) • Examples:
Classes: Functions • Two ways to do function definitions • Example of normal way:
Classes: Functions • Example of other way:
Classes: Constructors • Constructors are like Java's • Function name is same as class name • Accepts parameters and assigns them to fields • How can we modify set_values from the previous two examples to become car's constructor?
Pointers • Variables that contain memory addresses to other variables • Declaration: type* name • Examples:
Pointers: Referencing and Derefencing • Assign addresses of variables to pointers using the & operator • Example: • Access values of variables that pointers point to using the * operator • The following example is a true statement:
Pointers: Referencing and Derefencing • If num is at memory location 100 and numPointer is at memory location 200, what are the values of the following:
Pointers: Referencing and Derefencing • If num is at memory location 100 and numPointer is at memory location 200, what are the values of the following:
Pointers: Structs and Classes • To access a field or function of a struct or class that a pointer points to, use the -> operator • Examples:
Programming Assignment • Goal: Given a text file, determine the frequency of each word as well as what lines they're on • Specifications: • Create a binary search tree from the words and record their frequencies and locations • Print the alphabetized list of words, the frequency of each word, and the line numbers to the console • Due before next week's discussion section, submit through Moodle • Skeleton code is provided!
Programming Assignment • Helpful classes/functions: • ifstream: class to read files http://www.cplusplus.com/doc/tutorial/files/ • getline(ifstream, string): gets the next line in the file, saves to string http://www.cplusplus.com/reference/string/string/getline/ • strtok(char* str, const char* delim): multiple calls split the input string into multiple tokens, must specify delimiters http://www.cplusplus.com/reference/cstring/strtok/ • string1.compare(string2): determines if string1 is alphabetically before or after string2 http://www.cplusplus.com/reference/string/string/compare/