1 / 18

CS 377 Discussion 1

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

Download Presentation

CS 377 Discussion 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Brendan Murphy bemurphy@cs.umass.edu CS 377 Discussion 1

  2. 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)

  3. Today: C++ Review • C++ concepts • Structs • Classes • Pointers • Worksheet + Programming Assignment

  4. Structs • Collection of data, short for data structure • Can have different data types • Example:

  5. Structs • Can have multiple instances • Examples:

  6. Structs • Fields are inherently public • Example:

  7. 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:

  8. Classes: Functions • Two ways to do function definitions • Example of normal way:

  9. Classes: Functions • Example of other way:

  10. 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?

  11. Pointers • Variables that contain memory addresses to other variables • Declaration: type* name • Examples:

  12. 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:

  13. 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:

  14. 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:

  15. Pointers: Structs and Classes • To access a field or function of a struct or class that a pointer points to, use the -> operator • Examples:

  16. 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!

  17. 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/

  18. Programming Assignment

More Related