Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson 25 Miscellaneous Topics

Similar presentations


Presentation on theme: "Lesson 25 Miscellaneous Topics"— Presentation transcript:

1 Lesson 25 Miscellaneous Topics
CMSC 202 Lesson 25 Miscellaneous Topics

2 Warmup User iterators to print each item in a vector:
____________________________________________________________ for ( ______________________________________________________ ) cout << ________________ << endl; vector<int> integers; // assume we initialize this… vector<int>::iterator iter; iter = integers.begin(); iter != integers.end(); ++iter *iter

3 Inline Functions Problem Solution Drawbacks?
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!)

4 Writing 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 */ }

5 Friend Classes Problem: Solution: Drawbacks:
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

6 Friend Classes template < 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; };

7 Nested Classes Same Problem – tightly coupled classes Solution:
Class defined INSIDE of another class If private? Entirely hidden from everyone else If public? Accessible THROUGH the outer-class

8 Nested Classes Node is “nested” inside of List class
template< class T > class List { public: List( ) { m_header = NULL; } // other public stuff private: template < class T1 > class Node 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…

9 Namespaces Problem: Solution:
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…

10 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;

11 Creating a Namespace Really simple… Example: namespace <name> {
/* functions, classes, or variables */ } Example: namespace CMSC202

12 Constants and Pointers
Problem: What happens with the following? const int age = 42; int *pAge = &age; *pAge = 57; Solution: Pointers to Constants int age2 = 37; const int *pAge = &age; // OK! *pAge = 57; // compiler error! pAge = & age2; // OK!

13 Const Pointers Problem: Solution
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

14 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 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 << *pWeight << endl; // ok - just //dereferencing

15 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

16 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

17 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);

18 Function Objects Problem: Solution:
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!

19 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

20 Code in lecture notes used ‘set’ instead of ‘vector’.
Function Objects Code in lecture notes used ‘set’ instead of ‘vector’. Why won’t that work? int main ( ) { vector<int> iVector; // insert some data for (int i = 1; i < 10; i++) iVector.push_back( int(i) ); // print elements // // 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 // iVector.end(), print); }; 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;

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

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

23 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


Download ppt "Lesson 25 Miscellaneous Topics"

Similar presentations


Ads by Google