1 / 78

Hank Childs, University of Oregon

lorant
Download Presentation

Hank Childs, University of Oregon

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. CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 10: C strings Changes in C++ for structs Hank Childs, University of Oregon April 30th, 2014

  2. Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)

  3. Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)

  4. Announcements: Grades • Grades for project 1A, 1B, 1C all available on Blackboard • Emails sent out for missing projects, late projects • Please check and make sure everything you submitted is graded • Grades for projects 2A and 2D will be available soon • Suggestions for reviewing HWs? • Do it in class and point out common mistakes?

  5. Announcements: Late Passes • All projects on Blackboard have full score, even if late • I propose you all keep track of your own late passes, and tell me how you want to apply them at the end of the quarter. • Alternate: you can tell me now if you want to apply. • Not considered: I figure out optimal solution for you. So what should we do?

  6. Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)

  7. Review on compilation • gcc –c: build an object file (.o), i.e., binary code that can directly run on the architecture • Then the binary can be generated from the object files. • Libraries are a mechanism for grouping up a bunch of related object files • They are assembled together using a program called an archiver (ar) • You can also just use object files directly when linking.

  8. Makefiles • Consists of rules • Rule syntax: target: dependency1 dep2 … depN <tab>command1 <tab>command2 Quiz: write down a Makefile for a program called proj2B. Again, the file names are prototypes.h, driver.c, rectangle.c

  9. Preprocessor Phases • Resolve #includes • (we understand #include phase already) • Conditional compilation • #define / #ifdef / #ifndef / #endif • Macro replacement • Special macros • __FILE__, __LINE__, __FUNCTION__

  10. #define via gcc command-line option

  11. #ifndef/ #define to the rescue struct.h #ifndef RECTANGLE_330 #define RECTANGLE_330 struct Rectangle { double minX, maxX, minY, maxY; }; #endif Why does this work? This problem comes up a lot with big projects, and especially with C++.

  12. Problem with C…

  13. Problem with C… No checking of type…

  14. Problem is fixed with C++…

  15. Problem is fixed with C++…

  16. Mangling • Mangling refers to combing information about arguments and “mangling” it with function name. • Way of ensuring that you don’t mix up functions. • Why not return type too? • Causes problems with compiler mismatches • C++ compilers haven’t standardized. • Can’t take library from icpc and combine it with g++.

  17. C++ will let you overload functions with different types

  18. C++ also gives you access to mangling via “namespaces” Functions or variables within a namespace are accessed with “::”

  19. C++ also gives you access to mangling via “namespaces” The “using” keyword makes all functions and variables from a namespace available without needing “::”. And you can still access other namespaces.

  20. Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)

  21. Project #3A: background • Definitions: • Image: 2D array of pixels • Pixel: A minute area of illumination on a display screen, one of many from which an image is composed. • Pixels are made up of three colors: Red, Green, Blue (RGB) • Amount of each color scored from 0 to 1 • 100% Red + 100% Green + 0% Blue = Yellow • 100% Red + 0% Green + 100 %Blue = Purple • 0% Red + 100% Green + 0% Blue = Cyan • 100% Red + 100% Blue + 100% Green = White

  22. Project #3A: background • Colors are 0->1, but how much resolution is needed? How many bits should you use to represent the color? • Can your eye tell the difference between 8 bits and 32 bits? •  No. Human eye can distinguish ~10M colors. • 8bits * 3 colors = 24 bits = ~16M colors. • Red = (255,0,0) • Green = (0,255,0) • Blue = (0,0,255)

  23. Project 3A • Prompt was posted on Sunday • Due on Saturday, May 3rd • Tasks: • Struct to contain image • Read image from file (simplified format) • Write image to file (simplified format) • Function to modify image (yellow diagonal) • Program that puts it all together We will be modifying this code throughout the quarter… (expect you will have to retrofit what you are writing now)

  24. Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)

  25. ASCII Character Set There have been various extensions to ASCII … now more than 128 characters Many special characters are handled outside this convention image source: granneman.com

  26. Unix and Windows difference • Unix: • “\n”: goes to next line, and sets cursor to far left • Windows: • “\n”: goes to next line (cursor does not go to left) • “\m”: sets cursor to far left • Text files written in Windows often don’t run well on Unix, and vice-versa • There are more differences than just newlines vi: “set ff=unix” solves this

  27. signed vs unsigned chars • signed char (“char”): • valid values: -128 to 127 • size: 1 byte • used to represent characters with ASCII • values -128 to -1 are not valid • unsigned char: • valid values: 0 to 255 • size: 1 byte • used to represent data

  28. character strings • A character “string” is: • an array of type “char” • that is terminated by the NULL character • Example: char str[12] = “hello world”; • str[11] = ‘\0’ (the compiler did this automatically) • The C library has multiple functions for handling strings

  29. Character strings example

  30. Useful C library string functions • strcpy: string copy • strncpy: string copy, but just first N characters • strlen: length of a string

  31. Useful C library string functions • strcpy: string copy • strncpy: string copy, but just first N characters • strlen: length of a string What happened here?

  32. More useful C library string functions source: cplusplus.com

  33. memcpy I mostly use C++, and I still use memcpy all the time

  34. sscanf • like printf, but it parses from a string sscanf(str, "%s\n%d %d\n%d\n", magicNum, &width, &height, &maxval); on: str=“P6\n1000 1000\n255\n”; gives: magicNum = “P6”, width = 1000, height = 1000, maxval = 255

  35. Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)

  36. C++ • Reminder: almost every C program is a valid C++ program. • We now know C pretty well • So we are going to focus on new things in C++ • Classes • Templates • Operator overloading • Standard library • (will also talk about other changes)

  37. References • A references is a simplified version of a pointer. • Key differences: • You cannot do pointer manipulations • A reference is always valid • a pointer is not always valid • Accomplished with & (ampersand) • &: address of variable (C-style, still valid) • &: reference to a variable (C++-style, also now valid) You have to figure out how ‘&’ is being used based on context.

  38. Examples of References

  39. References vs Pointers vs Call-By-Value ref_doubler and ptr_doubler are both examples of call-by-reference. val_doubler is an example of call-by-value.

  40. References • Simplified version of a pointer. • Key differences: • You cannot manipulate it • Meaning: you are given a reference to exactly one instance … you can’t do pointer arithmetic to skip forward in an array to find another object • A reference is always valid • No equivalent of a NULL pointer … must be a valid instance

  41. Different Misc C++ Topic: initialization during declaration using parentheses This isn’t that useful for simple types, but it will be useful when we start dealing with objects.

  42. Outline • Announcements • Review • Project 3A • C-strings • Misc. C++ • Structs in C++ • Additional topics (if time)

  43. Learning classes via structs • structs and classes are closely related in C++ • I will lecture today on changes on how “structs in C++” are different than “structs in C” • … at the end of the lecture, I will describe how classes and structs in C++ differ.

  44. 3 Big changes to structs in C++ • You can associate “methods” (functions) with structs

  45. Methods vs Functions • Methods and Functions are both regions of code that are called by name (“routines”) • With functions: • the data it operates on (i.e., arguments) are explicitly passed • the data it generates (i.e., return value) is explicitly passed • stand-alone / no association with an object • With methods: • associated with an object & can work on object’s data • still opportunity for explicit arguments and return value

  46. Function vs Method (left) arguments and return value are explicit (right) arguments and return value are not necessary, since they are associated with the object (left) function is separate from struct (right) function (method) is part of struct

  47. Tally Counter 3 Methods: Increment Count Get Count Reset

  48. Methods & Tally Counter • Methods and Functions are both regions of code that are called by name (“routines”) • With functions: • the data it operates on (i.e., arguments) are explicitly passed • the data it generates (i.e., return value) is explicitly passed • stand-alone / no association with an object • With methods: • associated with an object & can work on object’s data • still opportunity for explicit arguments and return value

  49. C-style implementation of TallyCounter

  50. C++-style implementation of TallyCounter

More Related