CS 377 Discussion 1 Brendan Murphy bemurphycs umass
CS 377 Discussion 1 Brendan Murphy bemurphy@cs. umass. edu Computer Science 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 CS 207 Or by appointment (bemurphy@cs. umass. edu) Computer Science 2
Today: C++ Review C++ concepts Structs Classes Pointers Worksheet + Programming Assignment Computer Science 3
Structs Collection of data, short for data structure Can have different data types Example: Computer Science 4
Structs Can have multiple instances Examples: Computer Science 5
Structs Fields are inherently public Example: Computer Science 6
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: Computer Science 7
Classes: Functions Two ways to do function definitions Example of normal way: Computer Science 8
Classes: Functions Example of other way: Computer Science 9
Classes: 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? Computer Science 10
Pointers Variables that contain memory addresses to other variables Declaration: type* name Examples: Computer Science 11
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: Computer Science 12
Pointers: Referencing and Derefencing If num is at memory location 100 and num. Pointer is at memory location 200, what are the values of the following: Computer Science 13
Pointers: Referencing and Derefencing If num is at memory location 100 and num. Pointer is at memory location 200, what are the values of the following: Computer Science 14
Pointers: Structs and Classes To access a field or function of a struct or class that a pointer points to, use the -> operator Examples: Computer Science 15
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! Computer Science 16
Programming Assignment Helpful classes/functions: ifstream: class to read files http: //www. cplus. com/doc/tutorial/files/ getline(ifstream, string): gets the next line in the file, saves to string http: //www. cplus. com/reference/string/getline/ strtok(char* str, const char* delim): multiple calls split the input string into multiple tokens, must specify delimiters http: //www. cplus. com/reference/cstring/strtok/ string 1. compare(string 2): determines if string 1 is alphabetically before or after string 2 http: //www. cplus. com/reference/string/compare/ Computer Science 17
Programming Assignment Computer Science 18
- Slides: 18