hank childs university of oregon n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Hank Childs, University of Oregon PowerPoint Presentation
Download Presentation
Hank Childs, University of Oregon

Loading in 2 Seconds...

play fullscreen
1 / 54

Hank Childs, University of Oregon - PowerPoint PPT Presentation


  • 64 Views
  • Uploaded on

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Hank Childs, University of Oregon' - senwe


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
hank childs university of oregon

CIS 330: _ _ _ _ ______ _ _____ / / / /___ (_) __ ____ _____ ____/ / / ____/ _/_/ ____/__ __ / / / / __ \/ / |/_/ / __ `/ __ \/ __ / / / _/_// / __/ /___/ /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / /____/_/ / /__/_ __/_ __/ \____/_/ /_/_/_/|_| \__,_/_/ /_/\__,_/ \____/_/ \____//_/ /_/

Lecture 18:

templates and

the standard template library

Hank Childs, University of Oregon

May 28th, 2014

outline
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
outline1
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
announcements
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
Project 4C
  • Run valgrind on your 3E project.
  • Repeat until:
    • no memory errors
    • no memory leaks

Now extra credit (1.5%)

announcements schedule
Announcements: schedule

No Hank OH week of 6/2

project 4a 4b
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
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
Announcements: pool of images
  • What images should we use?

???

I received zero emails after Friday’s lecture

rest of this week
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
outline2
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
this pointer to current object
“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
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
Picking the right virtual function

So how to does the compiler know?

How does it get “B” for “b” and “A” for “a”?

??????

virtual f unction table
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 f unction table1
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
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
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
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 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
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 downcasting2
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
outline3
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
default arguments
Default Arguments

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

booleans
Booleans
  • New simple data type: bool (Boolean)
  • New keywords: true and false
inline function
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
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
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
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
Checksums

Most useful when input is very large and checksum is very small

From Wikipedia

our checksum
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
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 pages1
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)

outline4
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
outline5
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
project 3h
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%
outline6
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
templates vs virtual functions
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)
outline7
Outline
  • Announcements
  • Review
  • Potpourri
  • Project 3G
  • Project 3H
  • Templates
  • Standard Template Library
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
not a template thing string
(not a template thing): String
  • C++ string class is very useful
  • Great implementation of a class that encapsulates a string
next time
Next time
  • Live code project 3
  • Wrap up course