More C - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

More C

Description:

Simple Game: SDL (man -k SDL) / OpenGL. Interpreter for some (reasonably simple) language ... template class KeyT, class DataT BST{ void add(KeyT k, DataT d) ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 26
Provided by: andrew45
Category:
Tags: bst | more

less

Transcript and Presenter's Notes

Title: More C


1
More C
  • CSE399
  • Feb 13, 2007

2
Before we get started(1)..
  • Project proposals due 3 weeks
  • 1, 2, or 3 people (suggested 2)
  • Project complexity proportional to group size
  • Pick something interesting
  • Simple Game SDL (man -k SDL) / OpenGL
  • Interpreter for some (reasonably simple) language
  • A simple command shell with a couple cool
    features
  • An application of whatever field of CS you like

3
Doing the project
  • Projects will be demoed to me at end of semester
  • April 21,23,25 during the 12-1 timeslot
  • Platform of your choice, as long as can be demoed
    at school (lab or laptop)
  • Coding in C
  • Be ready to explain what C features were helpful

4
Before we get started (2).
  • Homework 2 Solutions online
  • Homework 3 Assigned last week, due next
  • Should free() copy of key in remove
  • Hashtable info on website

5
Into C
  • Last week C class basics
  • Highlights
  • Need virtual keyword for dynamic dispatch
  • public, private rather than on each member
  • Semi-colon at end of class declaration
  • Note use g to compile C programs

6
Destructors
  • Constructors Named same as class, called when
    newed
  • Destructors Named same as class, but with a ,
    called when deleteed
  • class Foo
  • public
  • Foo() .
  • Foo() .

7
Pass by reference
  • C only passes by value
  • void foo(int x) passes a pointer by value
  • In C, can pass by reference
  • void swap (int x, int y)
  • int temp x
  • x y
  • y temp
  • ..
  • swap (a, b)

8
Templated Functions
  • Functions may be parameterized over types or
    values
  • Templates must be instantiated to use
  • Values must be constant
  • Separate code for each set of template parameters
  • Type checking done after instantiation

9
Template Example
  • templateltclass Tgt void swap(T x, T y)
  • T temp x
  • x y
  • y temp
  • int a 3, b 4 double c 3.14, d 42
  • swapltintgt(a, b)
  • swapltdoublegt(c, d)

10
Template Example 2
  • template ltclass T1, class T2, class T3
  • T1 (f) (T2), T2 (g) (T3)gt
  • T1 composition (T3 x)
  • return f(g(x))
  • .
  • int (h)(int) compositionltint, int, int,
    inc,mulBy3gt

11
Template miscellanea
  • Compiler can sometimes infer type arguments
  • int a, b
  • swap(a,b) / compiler knows swapltintgt(a,b) /
  • Compiler can optimize each template instantiation
    separately

12
Templated Classes
  • C void generic data structure
  • struct llnode
  • void data
  • struct llnode next
  • Ugly because
  • Not type safe
  • Always need to allocate memory- even for int
    lists etc

13
Templated Classes
  • In C, use templated classes
  • templateltclass Tgt class LLNode
  • private
  • T data
  • LLNodeltTgt next
  • public
  • T getData() return data
  • .
  • Note LLNode must be instantiated with T for next

14
Template Specialization
  • Can specify a different definition for a specific
    set of parameters
  • templateltclass Tgt class X
  • .
  • templateltgt class Xltintgt
  • .
  • Note This is not inheritance.

15
A benefit of the way templates work
  • Body only needs to be valid for instantiated
    types
  • templateltclass KeyT, class DataTgt BST
  • .
  • void add(KeyT k, DataT d)
  • BSTNodeltKeyT, DataTgt curr root
  • .
  • if(curr-gtkey lt k)
  • .
  • lt not defined on all types- OK as long as you
    dont try to use for KeyT (in a few slides, we
    see how to define lt for other types)

16
A downside of how templates work
  • The compiler must instantiate each template
  • Definition must be visible
  • Cant simply link against it
  • Actual template code is put in .h file
  • Compiler designed to make sure linker is happy
    with this arrangement
  • Seems offensive to C programmers

17
Factorial.. in templates?
  • templateltint Ngt int factorial()
  • return N factorialltN-1gt()
  • templateltgt int factoriallt0gt()
  • return 1
  • .
  • int a factoriallt6gt()
  • With -O3, compiles to a 720

18
Overloaded functions
  • Same name, different arg lists
  • void foo(int x)
  • void foo(double d)
  • Legal in C (not C)
  • Compiler mangles name to make linker happy
  • _Z3fooi, _Z3food
  • extern C dont mangle- for linking with C

19
Overloaded Operators
  • Most operators can be overloaded too
  • All but . . ?
  • Cant overload for only built-in types
  • (I.e. overloaded operator must involve a user
    defined type, such as a class)
  • Cannot make new operators nor change arity,
    precedence, or associativity
  • Use the operator keyword
  • class Foo
  • int operator (int x) .
  • Question What is the other argument?

20
Benefits of operator overloading
  • Remember this
  • templateltclass KeyT, class DataTgt BST
  • .
  • void add(KeyT k, DataT d)
  • BSTNodeltKeyT, DataTgt curr root
  • .
  • if(curr-gtkey lt k)
  • .
  • Any type that overloads operatorlt can be used as
    the key

21
Downsides to operator overloading
  • Overloaded operators dont short circuit
  • Badly overloaded operators can confuse a
    programmer
  • Need to be careful of self-assignment for
    operator
  • Naïve free old, assign new
  • lhs same as rhs reads free()ed mem

22
Namespaces
  • Restrict visibility of a name
  • namespace foo
  • int x
  • void bar()
  • Access members with scope resolution operator
    foobar() or foox
  • Global namespace nothing on lhs of
  • somevariable
  • The using keyword opens a namespace
  • using namespace foo

23
IO C style
  • printf/fgets still work, but
  • C uses iostreams, which overload ltlt and gtgt
  • include ltiostreamgt //declares stdcout,
    stdendl
  • using namespace std // can skip std
  • int main(void)
  • cout ltlt "Hello World" ltlt endl
  • return 0

24
Input cin
  • Input can be read with cin and gtgt
  • string s // C introduces a string class
  • int x
  • cout ltlt Type a string ltlt endl
  • cin gtgt s
  • cout ltlt Type a number ltltendl
  • cin gtgt x //Non-numeric input left for next read

25
Overloading ltlt and gtgt
  • Classes can specify serialization behavior with
    ltlt and gtgt
  • class Foo
  • friend istream operatorgtgt(istream, Foo)
  • friend ostream operatorltlt(ostream, const Foo)
  • ostream operatorltlt(ostream out, const Foo f)
  • out ltlt f.something
  • return out
Write a Comment
User Comments (0)
About PowerShow.com