1 / 54

Hank Childs, University of Oregon

senwe
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 18: templates and the standard template library Hank Childs, University of Oregon May 28th, 2014

  2. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  3. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  4. Announcements • Projects: • 3F assigned Wednesday, due today • 3G assigned today, due Tuesday • 3H assigned today, due Monday • 4C assigned last Friday, due Friday, May 30th • Now extra credit (1.5%) • Late? … still 1.5% • 3I assigned on Friday, due Friday June 6th • … or during Finals Week Any 3F questions?

  5. Project 4C • Run valgrind on your 3E project. • Repeat until: • no memory errors • no memory leaks Now extra credit (1.5%)

  6. Announcements: schedule No Hank OH week of 6/2

  7. Project 4A / 4B • All graded, in Blackboard • Most instructive 4A’s selected (6 total) and will be incorporated into Final review • Want to make sure you got it right? • “svn up” • “grep <username> */list”

  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) We understand there is a problem, and are doing our best to resolve it. Don’t forget to email me!!!

  9. Announcements: pool of images • What images should we use? ??? I received zero emails after Friday’s lecture

  10. Rest of this week • 5/30 lecture: I will live code 3B, 3C, 3E, 3F • Pavel OH: tonight, tomorrow night • Hank OH: Thurs 11-12, Fri 10-11, 12:30-1:30

  11. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  12. “this”: pointer to current object • From within any struct’s method, you can refer to the current object using “this”

  13. How methods work under the covers (4/4) The compiler secretly slips “this” onto the stack whenever you make a method call. It also automatically changes “myInt” to this->myInt in methods.

  14. Picking the right virtual function So how to does the compiler know? How does it get “B” for “b” and “A” for “a”? ??????

  15. Virtual Function Table • Let C be a class and X be an instance of C. • Let C have 3 virtual functions & 4 non-virtual functions • C has a hidden data member called the “virtual function table” • This table has 3 rows • Each row has the correct definition of the virtual function to call for a “C”. • When you call a virtual function, this table is consulted to locate the correct definition.

  16. Virtual Function Table • Let C be a class and X be an instance of C. • Let C have 3 virtual functions & 4 non-virtual functions • Let D be a class that inherits from C and Y be an instance of D. • Let D add a new virtual function • D’s virtual function table has 4 rows • Each row has the correct definition of the virtual function to call for a “D”.

  17. More notes on virtual function tables • There is one instance of a virtual function table for each class • Each instance of a class shares the same virtual function table • Easy to overwrite (i.e., with a memory error) • And then all your virtual function calls will be corrected • Don’t do this! ;)

  18. Calling a virtual function • Let X be an instance of class C. • Let the virtual function be the 4th function • Let the arguments to the virtual function be an integer Y and a float Z. • Then call: (X.vptr[3])(&X, Y, Z); The 4th virtual function has index 3 (0-indexing) Secretly pass “this” as first argument to method The pointer to the virtual function pointer (often called a vptr) is a data member of X

  19. Inheritance and Virtual Function Tables This whole scheme gets much harder with virtual inheritance, and you have to carry around multiple virtual function tables. Same as B’s This is how you can treat a C as a B

  20. Virtual Function Table: Summary • Virtual functions require machinery to ensure the correct form of a virtual function is called • This is implemented through a virtual function table • Every instance of a class that has virtual functions has a pointer to its class’s virtual function table • The virtual function is called via following pointers • Performance issue

  21. Upcasting and Downcasting • Upcast: treat an object as the base type • We do this all the time! • Treat a Rectangle as a Shape • Downcast: treat a base type as its derived type • We don’t do this one often • Treat a Shape as a Rectangle • You better know that Shape really is a Rectangle!!

  22. Upcasting and Downcasting what do we get?

  23. Upcasting and Downcasting • C++ has a built in facility to assist with downcasting: dynamic_cast • I personally haven’t used it a lot, but it is used in practice • Ties in to std::exception

  24. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  25. Default Arguments default arguments: compiler pushes values on the stack for you if you choose not to enter them

  26. Booleans • New simple data type: bool (Boolean) • New keywords: true and false

  27. Inline function • inlined functions: • hint to a compiler that can improve performance • basic idea: don’t actually make this be a separate function that is called • Instead, just pull the code out of it and place it inside the current function • new keyword: inline The compiler sometimes refuses your inline request (when it thinks inlining won’t improve performance), but it does it silently.

  28. Inlines can be automatically done within class definitions • Even though you don’t declare this as inline, the compiler treats it as an inline

  29. You should only do inlines within header files Left: function is inlined in every .C that includes it … no problem Right: function is defined in every .C that includes it … duplicate symbols

  30. Stress Test Project • We will have ~60 stress tests • We can’t check in 60 baseline images and difference them all • Will slow ix to a grind • Solution: • We commit “essence of the solution” • We also complement that with everyone posting the image of their solution on their home webpage in case it is needed.

  31. Checksums Most useful when input is very large and checksum is very small From Wikipedia

  32. Our “checksum” • Three integers: • Sum of red channel • Sum of green channel • Sum of blue channel • When you create a stress test, you register these three integers • When you test against others stress tests, you compare against their integers • If they match, you assume you got it right • Otherwise, you check their webpage This will be done with a derived type of Sink.

  33. Web pages • ssh –l <user name> ix.cs.uoregon.edu • cd public_html • put something in index.html •  it will show up as http://ix.cs.uoregon.edu/~<username>

  34. Web pages • You can also exchange files this way • scp 3H.png <username>@ix.cs.uoregon.edu:~/public_html • point people to http://ix.cs.uoregon.edu/~<username>/3H.png Note that ~/public_html/3H.png shows up as http://ix.cs.uoregon.edu/~<username>/3H.png (“~/3H.png” is not accessible via web)

  35. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  36. Project 3G

  37. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  38. Project 3H • Write on stress test • You write main3H.C • It can • cause an exception and catch it • make an image • You provide the right answer • Your 3H will be rejected if it doesn’t fit the interface we’ve established as a class • (loss of credit) • Due Monday, worth 3%

  39. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  40. Motivation

  41. Motivation

  42. First Template

  43. Will now do an example to compare templates and virtual functions • Will take some buildup…

  44. Money Class

  45. License Plate Class

  46. Sorting With Templates

  47. Doing the same with inheritance

  48. Templates vs Virtual Functions • Virtual functions: • Had to affect inheritance hierarchy • Overhead in function call (virtual function table) • Templates: • Did not need to affect inheritance hierarchy, although function names had to coincide • No additional overhead (resolved at compile time)

  49. Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library

  50. Standard Template Library • Standard Template Library: STL • Many, many templated types • Can ease your programming • Can also hide significant performance issues • And you use C/C++ for performance • My recommendation: use with caution for code that needs to be performant

More Related