1 / 24

CMSC 202

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

Télécharger la présentation

CMSC 202

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. 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

  4. 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!)

  5. 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 */ }

  6. 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

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

  8. 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

  9. Nested Classes template< 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…

  10. 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…

  11. 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;

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

  13. 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!

  14. 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 const pLength = &height; // error - pLength is const

  15. 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

  16. 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

  17. 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

  18. 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);

  19. 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!

  20. 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

  21. 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); }; Function Objects Code in lecture notes used ‘set’ instead of ‘vector’. Why won’t that work?

  22. 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; } Function Objects Key understanding: Create functions on the fly! Often used in Artifical Intelligence applications!

  23. 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

  24. 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

More Related