1 / 74

Hank Childs, University of Oregon

bevan
Télécharger la présentation

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 16: Potpourri pt 2: scope, overloading operators, exceptions Hank Childs, University of Oregon May 21st, 2014

  2. Outline • Announcements • Project 3E, 4B • Review • Potpourri • scope • operator overloading • exceptions • Project 3F

  3. Outline • Announcements • Project 3E, 4B • Review • Potpourri • scope • operator overloading • exceptions • Project 3F

  4. Announcements • My office is being painted Weds & Thurs • Thurs OH to be held in Rm 260

  5. Announcements • We evaluate your code on ix • A good idea to test it there and make sure it work • I don’t have C++11 on my personal machine • (This tells you something)

  6. Announcements • Projects: • 3E assigned May 16th, due tomorrow • 4B assigned May 17th, due Saturday • 3F assigned today, due Tuesday, May 27th

  7. Announcements • netpbm • macports • brew

  8. Announcements: CIS441 • Teaching CIS441 in Fall • First 5 weeks: building graphics system in SW • Next 2 weeks: learn OpenGL • Last 3 week: final project (judged by local gaming professionals) (no examples this time)

  9. Announcements: filters • Make a constant color • Crop • Conditional blend • Invert • ???

  10. Announcements: pool of images • We have Puddles • What other images should we use?

  11. Outline • Announcements • Project 3E, 4B • Review • Potpourri • scope • operator overloading • exceptions • Project 3F

  12. Project 3E

  13. Project 3E • You will need to think about how to accomplish the data flow execution pattern and think about how to extend your implementation to make it work. • This prompt is vaguer than some previous ones • … not all of the details are there on how to do it

  14. Project 3E • Worth 5% of your grade • Assigned today, due May 22nd • Also component for “const”

  15. Project 4B • Goal: • Debug 4 other student programs • I choose 6, you do 4 • Details: • Worth 3% of your grade • Assigned Saturday 5/17, due Saturday 5/24 • Submission: check your answers into a Subversion repository

  16. Announcements: schedule Did not get to: 4N: performance reading from sockets 4N: 0% of your grade 4C: skippable (although helps with 3I)

  17. Outline • Announcements • Project 3E, 4B • Review • Potpourri • scope • operator overloading • exceptions • Project 3F

  18. const • const: • is a keyword in C and C++ • qualifies variables • is a mechanism for preventing write access to variables

  19. const example const keyword modifies int The compiler enforces const… just like public/private access controls

  20. Efficiency Are any of the three for loops faster than the others? Why or why not? Answer: X is probably faster than Y … compiler can do optimizations where it doesn’t have to do “i < X“ comparisons (loop unrolling) Answer: NumIterations is slowest … overhead for function calls.

  21. const pointers • Assume a pointer named “P” • Two distinct ideas: • P points to something that is constant • P may change, but you cannot modify what it points to via P • P must always point to the same thing, but the thing P points to may change.

  22. const pointers int X = 4; int *P = &X; Idea #1: violates const: “*P = 3;” OK: “int Y = 5; P = &Y;” const goes before type pointer can change, but you can’t modify the thing it points to

  23. const pointers int X = 4; int *P = &X; Idea #2: violates const: “int Y = 5; P = &Y;” OK: “*P = 3;” const goes after * pointer can’t change, but you canmodify the thing it points to

  24. const pointers int X = 4; int *P = &X; Idea #3: violates const: “*P = 3;” “int Y = 5; P = &Y;” OK: none constin both places pointer can’t change, and you can’t modify the thing it points to

  25. const usage • class Image; • const Image *ptr; • Used a lot: offering the guarantee that the function won’t change the Image ptr points to • Image * constptr; • Helps with efficiency. Rarely need to worry about this. • const Image * constptr; • Interview question!!

  26. const functions with objects If a class method is declared as const, then you can call those methods with pointers. constafter method name

  27. globals • You can create global variables that exist outside functions.

  28. global variables • global variables are initialized before you enter main

  29. Storage of global variables… • global variables are stored in a special part of memory • “data segment” (not heap, not stack) • If you re-use global names, you can have collisions

  30. Externs: mechanism for unifying global variables across multiple files extern: there’s a global variable, and it lives in a different file.

  31. static • static memory: third kind of memory allocation • reserved at compile time • contrasts with dynamic (heap) and automatic (stack) memory allocations • accomplished via keyword that modifies variables There are three distinct usages of statics

  32. static usage #1: persistency within a function

  33. static usage #2: making global variables be local to a file I have no idea why the static keyword is used in this way.

  34. static methods Static data members and static methods are useful and they are definitely used in practice

  35. Outline • Announcements • Project 3E, 4B • Review • Potpourri • scope • operator overloading • exceptions • timings • Project 3F • Project 4N

  36. scope • I saw this bug quite a few times… The compiler will sometimes have multiple choices as to which variable you mean. It has rules to make a decision about which one to use. This topic is referred to as “scope”.

  37. scope This one won’t compile. The compiler notices that you have a variable called X that “shadows” the argument called X.

  38. scope This one will compile … the compiler thinks that you made a new scope on purpose. So what does it print? Answer: 3

  39. scope What does this one print? Answer: 2

  40. scope What does this one print? Answer: 1

  41. scope What does this one print? Answer: 0

  42. Scope Rules • The compiler looks for variables: • inside a function or block • function arguments • data members (methods only) • globals

  43. Shadowing • Shadowing is a term used to describe a “subtle” scope issue. • … i.e., you have created a situation where it is confusing which variable you are referring to

  44. Outline • Announcements • Project 3E, 4B • Review • Potpourri • scope • operator overloading • exceptions • Project 3F

  45. C++ lets you define operators • You declare a method that uses an operator in conjunction with a class • +, -, /, !, ++, etc. • You can then use operator in your code, since the compiler now understands how to use the operator with your class • This is called “operator overloading” • … we are overloading the use of the operator for more than just the simple types.

  46. Example of operator overloading Declare operator ++ will be overloaded for MyInt Call operator ++ on MyInt. Define operator ++ for MyInt

  47. More operator overloading

  48. Beauty of inheritance • ostream provides an abstraction • That’s all Image needs to know • it is a stream that is an output • You code to that interface • All ostream’s work with it

  49. assignment operator

  50. let’s do this again… (ok, fine)

More Related