1 / 28

Object Oriented Programming

Object Oriented Programming. Spring 2007 Recitation 8. Templates. Generic Programming. Why?. Often we meet algorithms, data structures or classes that are independent of the concrete type used in them. QuickSort can sort anything (if it can be compared). List can store anything.

nate
Télécharger la présentation

Object Oriented Programming

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. Object Oriented Programming Spring 2007 Recitation 8 OOP Spring 2007 – Recitation 8

  2. Templates Generic Programming OOP Spring 2007 – Recitation 8

  3. Why? • Often we meet algorithms, data structures or classes that are independent of the concrete type used in them. • QuickSort can sort anything (if it can be compared). • List can store anything. • But how can we write generic algorithms and data structures? OOP Spring 2007 – Recitation 8

  4. Bad Solutions • Using void* pointers: • Type-unsafe. • Error-prone. • Very unfriendly. • Deriving all classes from one superclass: • Type-unsafe. • Demands specific interface from all classes. • Both solutions disallow homogenous containers. OOP Spring 2007 – Recitation 8

  5. Templates • A template allows passing a type as a parameter to a class or a function. • This means that we can write classes and functions that are independent of the types they work with. • Actually, a template is a recipe for creating families of classes and functions. OOP Spring 2007 – Recitation 8

  6. Swap() • Consider the function swap() – the code is identical if it swaps two ints, two Rationals or two strings. • void swap(int& a, int& b) { • int temp = a; • a = b; • b = temp; • } OOP Spring 2007 – Recitation 8

  7. Function Templates • We can say that to the compiler by defining a function template: • template <class T> • void swap(T& a, T& b) { • T temp = a; • a = b; • b = temp; • } • Within the template, T is another type, just like int or double. OOP Spring 2007 – Recitation 8

  8. Template Instantiation • The compiler will use this template to instantiateswap() for types it needs: • When it needs to swap ints, it will generate a version for ints. • If it needs to swap strings, it will generate a new version for strings. • The versions it generates will have no connection to one another (apart from similar name). OOP Spring 2007 – Recitation 8

  9. Lack of Conversions • The compiler decides which version to instantiate based on the parameters types. • This is called template argument deduction. • Conversions are not considered for this. • I.e., these calls will not compile: • int i; short s; double d; • swap(i, d); // no version for int and a double • swap(i, s); // no version for int and a short • The only conversions used are • conversion to const pointer or reference, • array and function to pointer. OOP Spring 2007 – Recitation 8

  10. Class Templates • What about classes? Obviously, the code for ListOComplex will work for Rationals, ints or strings, if we didn’t hardwire the type into it. • We can define a class template for a List, and List users will specify the concrete type when creating List objects. • The compiler will create instantiated classes as needed. OOP Spring 2007 – Recitation 8

  11. Class List • template <class T> • class List { • public: • List(const List&); • bool push_front(const T&); • T pop_front(); • … • }; • Within List’s methods, T is another type, just like int or double. OOP Spring 2007 – Recitation 8

  12. Instantiated Classes • With class templates we need to specify the type parameter to create an object: • List<Complex> listOcomplex; • List<int> listOint; • The compiler creates an instance of class List for Complexes and an instance for ints. • They have nothing to do with one another. • Note that copy c’tor in List has a parameter of type List, not List<T>. • Both forms can be used – but the former only within the template itself. OOP Spring 2007 – Recitation 8

  13. Exceptions Error-handling mechanism OOP Spring 2007 – Recitation 8

  14. Why? • Consider what happens when an error occurs in a program that consists of separate modules. • Often the action that needs to be taken depends on the calling module rather than on the module that found the error. • A string doesn’t know what to do if it cannot allocate memory. The user of the string does. • And what if the caller of the caller knows how to fix the error? OOP Spring 2007 – Recitation 8

  15. Exceptions • Exceptions are program anomalies that occur during runtime (out-of-memory, pop-on-empty-stack, division-by-zero, etc.). • Exception handling is a mechanism that allows two separately developed modules to communicate when exception occurs. OOP Spring 2007 – Recitation 8

  16. Throwing and Catching • A module that detects an error has occurred signals it by throwing an exception of appropriate type. • The module in the calling chain that knows how to handle the exception declares that by catching an exception of that type. • A pop() in Stack can throw popOnEmpty, and Stack’s user can catch it and know that pop() didn’t succeed because it’s empty. OOP Spring 2007 – Recitation 8

  17. Why Exceptions? • Exceptions: • Separate exceptional logic from normal logic. • Supported by the compiler. • Can carry any amount of information from thrower to the catcher. • Can be used in constructors. • But impose runtime overhead. • Returning error codes is still used. OOP Spring 2007 – Recitation 8

  18. throw • An exception is an object of some type. • The program part that has detected an error that it cannot handle, throws an exception (which is an object) of some type. • This is done using the throw expression. OOP Spring 2007 – Recitation 8

  19. Example • class popOnEmpty {}; // Exceptions are objects, • class pushOnFull {}; // need to define the classes • int Stack::pop() { // Stack from recitation 1 • if (_top_index == 0) • throw popOnEmpty(); // Used to be "return 0" • return _contents[--_top_index]; • } • void Stack::push(int el) { • if (_top_index == _size) • throw pushOnFull(); // Used to be "return false" • _contents[_top_index++] = el; • return true; • } OOP Spring 2007 – Recitation 8

  20. Example Notes • Notice that now push() doesn’t return a value, and pop() returns only elements of the stack. • Since throw throws an object, the corresponding class needs to be defined beforehand. • It’s best to use separate classes for exceptions. OOP Spring 2007 – Recitation 8

  21. throw Creates an Object • throw creates a temporary object of the given type and “passes it on”. • Note the () in • throw pushOnFull(); • This creates a pushOnFull object using the default constructor. • Had class pushOnFull had a constructor with int parameter (for example), we could write: • throw pushOnFull(el); • The handler would then know what element wasn’t pushed. OOP Spring 2007 – Recitation 8

  22. Catching • To be handled, an exception needs to be caught. • This is done using try-catch construct. • Operations that can throw exception are enclosed in a try block, and following that block a series of catch clauses, each defining how to handle specific type of exception. OOP Spring 2007 – Recitation 8

  23. Example • int main() { • Stack s(10); • try { • s.push(1); • // More pops, pushes, printouts, etc. • } catch (popOnEmpty) { • cout << "Caught popOnEmpty\n"; • } catch (pushOnFull) { • cout << "Caught pushOnFull\n"; • } • cout << "Done\n"; • return 0; • } OOP Spring 2007 – Recitation 8

  24. catch Clauses • Each catch clause defines how to handle a specific type of exception that might have occurred in the preceding try block. • The clauses are examined top down until a matching catch is found. • If none is found, the exception propagates to the caller. • If a matching catch clause is found, its body is executed and execution resumes after the last catch (for that try block). OOP Spring 2007 – Recitation 8

  25. Exception Propagation • Notice that a try-catch block only needs to handle exceptions it knows how to “fix”. • Anything is doesn’t handle propagates to the caller of that function. • When exception tries to propagate out of main(), the STL terminate() function is called, which by default calls abort(). OOP Spring 2007 – Recitation 8

  26. Exception Declaration • The () part in catch is called exception declaration. • It can be either type declaration or an object declaration. • …catch (popOnEmpty) { // Type declaration • cout << "Caught popOnEmpty\n"; • } catch (pushOnFull e) { // Object declaration • cout << e.val() << " cannot be pushed\n"; • } • In object declaration the exception object that was thrown is given a name and can be accessed. • This allows the exception object to carry additional information about the error. OOP Spring 2007 – Recitation 8

  27. Catching Everything • If someone (main(), for example) wants to handle any exception that can be thrown, it can use catch(...): • try { • //… • } catch (...) { • cout << "Unknown exception occurred\n"; • } • Obviously, it should come last in the list of catches. OOP Spring 2007 – Recitation 8

  28. Catching by Reference • Exception declaration is much like function parameter list. • In particular, we can catch exception objects by reference: • …catch (pushOnFull& e) {…} • Otherwise the exception object is passed by-value into the catch clause. • Using “catch-by-reference”: • Prevents unnecessary copying; • Allows changing the exception object; • Permits polymorphism. OOP Spring 2007 – Recitation 8

More Related