Title: CMSC 202
1CMSC 202
- Lesson 25
- Miscellaneous Topics
2Warmup
- User iterators to print each item in a vector
- __________________________________________________
__________ - __________________________________________________
__________ - for ( ____________________________________________
__________ ) - cout ltlt ________________ ltlt endl
vectorltintgt integers // assume we initialize
this
vectorltintgtiterator iter
iter integers.begin() iter ! integers.end()
iter
iter
3Announcments
- 2 more days of class (after today)
- Thursday
- Overload the dereferencing operator
- Bit-wise operators (important for 341)
- Tuesday
- Review for final (cumulative, focus on 2nd half)
- Homework
- Go over review questions
- Go over last semesters final
- Bring a question that you want answered
4Inline Functions
- Problem
- Calling a 1-line function is inefficient
- Solution
- Inline functions
- Compiler replaces function call with function
body - Drawbacks?
- Mix definition and implementation
- Make executables bigger (ack!)
5Writing Inline Functions
- 2 ways
- Put the function body in the class definition
- class A
-
- public
- int GetData() return data
- private
- int data
-
- Put keyword inline before the signature line
- inline void foobar()
-
- / some code /
6Friend Classes
- Problem
- Class A relies heavily on Class b
- Tightly coupled lots of interconnectivity
- Both classes required to represent 1 data
structure - Inefficient to use methods to access data
- Solution
- Declare Class A as a friend of Class B
- State that Class A has access to private data of
Class B - Drawbacks
- Break Encapsulation and Information Hiding
7Friend Classes
- template lt class T gt
- class ListltTgt // a "forward declaration"
- templatelt class T gt
- class Node
-
- private
- Node( )
- Node m_next
- int m_data
- friend class ListltTgt
-
- template lt class T gt
- class List
-
- public
- List( )
- // other public stuff
- private
- NodeltTgt m_header
-
8Nested Classes
- Same Problem tightly coupled classes
- Solution
- Nested Classes
- Class defined INSIDE of another class
- If private?
- Entirely hidden from everyone else
- If public?
- Accessible THROUGH the outer-class
9Nested Classes
- templatelt class T gt
- class List
-
- public
- List( ) m_header NULL
- // other public stuff
- private
- template lt class T1 gt
- class Node
-
- private
- Node( )
- NodeltT1gt m_next
- int m_data
-
Node is nested inside of List class If it were
public, the Nodes classname (and type!) would
be ListltTgtNodeltTgt The node is scoped INSIDE
the List class
10Namespaces
- Problem
- Your class-names, function-names, or
variable-names clash with existing names - Example
- You want your own cout object
- You want to define your own vector class
- Solution
- Namespaces
- Groups of classes, functions, or variables
- Allow you to specify exactly which version
- Kind of like overloading
11Namespaces
- Assume Fred is a namespace
- There are different ways to use Fred
- Using everything from a namespace
- using namespace Fred
- Use only f from Fred
- using Fredf
- Qualify each use of something from Fred
- Fredf()
- Instead of just f()
- Example
- using namespace std
- using stdstring
- stdstring myString Hello World!
- stdcout ltlt myString ltlt stdendl
12Creating a Namespace
- Really simple
- namespace ltnamegt
-
- / functions, classes, or variables /
-
- Example
- namespace CMSC202
-
- / functions, classes, or variables /
13Constants and Pointers
- Problem
- What happens with the following?
- const int age 42
- int pAge age
- pAge 57
- Solution
- Pointers to Constants
- const int age 42
- int age2 37
- const int pAge age // OK!
- pAge 57 // compiler error!
- pAge age2 // OK!
14Const Pointers
- Problem
- Want to define a pointer that cannot be changed
- i.e. it cannot point to a different object!
- Solution
- Const Pointers
- int width 56
- int length 42
- int const pLength length
- // initialized, unchangeable
- pLength 86 // ok - length is not const
- pLength height
- // error - pLength is const
15Const Pointer, Const Object
- Problem
- Can we make an unmovable pointer that points to
an unchangeable object? - Solution
- Const Pointer to a Const Object(ack!)
- int size 43 // non-const
int - const int weight 89 // const int
- const int const pWeight weight
- // const pointer to a const int
- pWeight 88 // compiler
error - pWeight size // compiler
error - cout ltlt pWeight ltlt endl // ok -
just //dereferencing
16Consts and Pointers
- 4 different ways
- int pInt
- const int pInt
- int const pInt
- const int const pInt
What do each of these mean??? Hint read from the
inside to the outside
Pointer to integer
Pointer to constant integer
Constant pointer to integer
Constant pointer to constant integer
17STL Algorithms
- STL provides many algorithms for use with
container classes - include ltalgorithmgt
- Some are
- for_each() performs a function on each item
- Pass by reference if you want to change the item
- transform() performs for_each, but stores
result in another container - fill() fills every item with a supplied value
- replace() replaces a subset of the items with
value - sort() uses Quicksort to sort items based on
some comparison function
18Examples
- Assume appropriate containers exist
- Square, print, and GreaterThan are user-defined
functions (i.e. you must write them!) - for_each( myList.begin(), myList.end(), square )
- for_each( myList.begin(), myList.end(), print )
- transform( v1.begin(), v1.end(), v2.begin(),
square) - fill( vString.begin() 1, vString.begin() 3,
"tommy") - replace( vString.begin(), vString.end(),
string("steve"), string("bill")) - sort( v1.begin(), v1.end()) // default, uses
operatorlt - sort( v1.begin(), v1.end(), GreaterThan)
19Function Objects
- Problem
- Can we dynamically build functions at runtime?
- Solution
- Function Objects
- Classes (objects) that behave like functions
- Overloading the operator( )
- I told you it was possible!
20Function Objects
- Why?
- Functions that have more properties than just the
operator() - Can store a state
- Separate copies, each with own state
- Can be initialized in constructor
21Function Objects
Code in lecture notes used set instead of
vector. Why wont that work?
- int main ( )
-
- vectorltintgt iVector
- // insert some data
- for (int i 1 i lt 10 i)
- iVector.push_back( int(i) )
- // print elements
- // 1 2 3 4 5 6 7 8 9
- // create a function adds 42 to parameter
- Add add42( 42 )
- // add 42 to each element of the set
- for_each( iVector.begin(),
- iVector.end(), add42)
- // print the elements
- class Add
-
- public
- // const
- Add (int value)
- m_value( value )
-
- void operator( ) (int n ) const
-
- n m_value
-
- private
- int m_value // value to add
-
- void print(const int i)
-
22Function Objects
Key understanding Create functions on the
fly! Often used in Artifical Intelligence
applications!
- class RNG // Random Number Generator
-
- public
- RNG (unsigned int seed 1)
- m_lastValue( seed ), m_seed( seed )
- srand(seed)
- unsigned int operator( ) ()
-
- // modify last Value for new value
- m_lastValue rand() m_seed
- return m_lastValue
-
- private
- unsigned int m_lastValue
- unsigned int m_seed
- int main ( )
-
- RNG rng ( 42 )
- for (int i 0 i lt 10 i)
- cout ltlt rng() ltlt endl
23Practice
- Which of the following is legal?
- Assume that illegal statements are skipped
- BeachBall a(7.0)
- BeachBall b(6.0)
- const BeachBall c(5.0)
- const BeachBall p a
- BeachBall const q b
- p-gtSetRadius(1.0) // 1
- q-gtSetRadius(2.0) // 2
- p c // 3
- q c // 4
- p-gtSetRadius(1.0) // 5
- q-gtSetRadius(2.0) // 6
24Challenge
- Use a Function Object to create a function that
computes the polynomial of its parameter - n2, n3, n4,
- Exponent is parameter in constructor
- Use for_each to compute the square and cube of
each float in a vector