CMSC 202

24
CMSC 202 Lesson 25 Miscellaneous Topics

description

CMSC 202. Lesson 25 Miscellaneous Topics. Warmup. User iterators to print each item in a vector: ____________________________________________________________ ____________________________________________________________ for ( ______________________________________________________ ) - PowerPoint PPT Presentation

Transcript of CMSC 202

Page 1: CMSC 202

CMSC 202

Lesson 25Miscellaneous Topics

Page 2: CMSC 202

Warmup

User iterators to print each item in a vector:

____________________________________________________________

____________________________________________________________

for ( ______________________________________________________ )

cout << ________________ << endl;

vector<int>::iterator iter;

iter = integers.begin(); iter != integers.end(); ++iter

vector<int> integers; // assume we initialize this…

*iter

Page 3: CMSC 202

Announcments 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 semester’s final Bring a question that you want answered

Page 4: CMSC 202

Inline 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!)

Page 5: CMSC 202

Writing Inline Functions 2 ways

Put the function body in the class definitionclass A{

public:int GetData() { return data; }

private:int data;

};

Put keyword ‘inline’ before the signature lineinline void foobar(){

/* some code */}

Page 6: CMSC 202

Friend 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

Page 7: CMSC 202

Friend Classestemplate < class T >class List<T>; // a "forward declaration"

template< class T >class Node{ private: Node( );

Node* m_next; int m_data;

friend class List<T>;};

template < class T >class List{ public: List( );

// other public stuff

private: Node<T> *m_header;};

Page 8: CMSC 202

Nested 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

Page 9: CMSC 202

Nested Classestemplate< class T >class List{ public: List( ) { m_header = NULL; }

// other public stuff

private: template < class T1 > class Node { private: Node( );

Node<T1>* m_next; int m_data;

}; Node<T> *m_header;};

Node is “nested” inside of List class

If it were public, the Node’s classname

(and type!) would be:

List<T>::Node<T>

The node is scoped INSIDE the List class…

Page 10: CMSC 202

Namespaces 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…

Page 11: CMSC 202

Namespaces 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 Fred::f;

Qualify each use of something from Fred Fred::f()

Instead of just f() Example:

using namespace std; using std::string; std::string myString = “Hello World!”; std::cout << myString << std::endl;

Page 12: CMSC 202

Creating a Namespace Really simple…

namespace <name>

{

/* functions, classes, or variables */

}

Example:namespace CMSC202

{

/* functions, classes, or variables */

}

Page 13: CMSC 202

Constants 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!

Page 14: CMSC 202

Const 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 constpLength = &height;

// error - pLength is const

Page 15: CMSC 202

Const 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 intconst int weight = 89; // const int

const int *const pWeight = &weight; // const pointer to a

const int

*pWeight = 88; // compiler errorpWeight = &size; // compiler error

cout << *pWeight << endl; // ok - just

//dereferencing

Page 16: CMSC 202

Consts 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

Page 17: CMSC 202

STL Algorithms STL provides many algorithms for use with

container classes #include <algorithm>

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

Page 18: CMSC 202

Examples 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 operator<sort( v1.begin(), v1.end(), GreaterThan);

Page 19: CMSC 202

Function 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!

Page 20: CMSC 202

Function 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

Page 21: CMSC 202

Function Objects

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)

{

cout << i << endl;

}

int main ( ){ vector<int> iVector;

// insert some data for (int i = 1; i < 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 // 43 44 45 46 47 48 49 50 51 for_each( iVector.begin(), iVector.end(), print);};

Code in lecture notes used ‘set’

instead of ‘vector’.

Why won’t that work?

Page 22: CMSC 202

Function Objects

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 < 10; i++) cout << rng() << endl;}

Key understanding:

Create functions on the fly!

Often used in Artifical

Intelligence applications!

Page 23: CMSC 202

Practice 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->SetRadius(1.0); // 1q->SetRadius(2.0); // 2p = &c; // 3q = &c; // 4p->SetRadius(1.0); // 5q->SetRadius(2.0); // 6

Page 24: CMSC 202

Challenge

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