E N D
CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/ Lecture 18: templates and the standard template library Hank Childs, University of Oregon May 28th, 2014
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
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?
Project 4C • Run valgrind on your 3E project. • Repeat until: • no memory errors • no memory leaks Now extra credit (1.5%)
Announcements: schedule No Hank OH week of 6/2
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”
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!!!
Announcements: pool of images • What images should we use? ??? I received zero emails after Friday’s lecture
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
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
“this”: pointer to current object • From within any struct’s method, you can refer to the current object using “this”
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.
Picking the right virtual function So how to does the compiler know? How does it get “B” for “b” and “A” for “a”? ??????
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.
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”.
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! ;)
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
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
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
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!!
Upcasting and Downcasting what do we get?
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
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
Default Arguments default arguments: compiler pushes values on the stack for you if you choose not to enter them
Booleans • New simple data type: bool (Boolean) • New keywords: true and false
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.
Inlines can be automatically done within class definitions • Even though you don’t declare this as inline, the compiler treats it as an inline
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
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.
Checksums Most useful when input is very large and checksum is very small From Wikipedia
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.
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>
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)
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
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%
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
Will now do an example to compare templates and virtual functions • Will take some buildup…
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)
Outline • Announcements • Review • Potpourri • Project 3G • Project 3H • Templates • Standard Template Library
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