Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.

Similar presentations


Presentation on theme: "OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8."— Presentation transcript:

1 OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8

2 OOP Spring 2007 – Recitation 82 Templates Generic Programming

3 OOP Spring 2007 – Recitation 83 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?

4 OOP Spring 2007 – Recitation 84 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.

5 OOP Spring 2007 – Recitation 85 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.

6 OOP Spring 2007 – Recitation 86 Swap() Consider the function swap() – the code is identical if it swaps two int s, two Rational s or two string s. void swap(int& a, int& b) { int temp = a; a = b; b = temp; }

7 OOP Spring 2007 – Recitation 87 Function Templates We can say that to the compiler by defining a function template: template 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.

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

9 OOP Spring 2007 – Recitation 89 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.

10 OOP Spring 2007 – Recitation 810 Class Templates What about classes? Obviously, the code for ListOComplex will work for Rational s, int s or string s, 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.

11 OOP Spring 2007 – Recitation 811 Class List template 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.

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

13 OOP Spring 2007 – Recitation 813 Exceptions Error-handling mechanism

14 OOP Spring 2007 – Recitation 814 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?

15 OOP Spring 2007 – Recitation 815 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.

16 OOP Spring 2007 – Recitation 816 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.

17 OOP Spring 2007 – Recitation 817 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.

18 OOP Spring 2007 – Recitation 818 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.

19 OOP Spring 2007 – Recitation 819 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; }

20 OOP Spring 2007 – Recitation 820 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.

21 OOP Spring 2007 – Recitation 821 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.

22 OOP Spring 2007 – Recitation 822 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.

23 OOP Spring 2007 – Recitation 823 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; }

24 OOP Spring 2007 – Recitation 824 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).

25 OOP Spring 2007 – Recitation 825 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().

26 OOP Spring 2007 – Recitation 826 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.

27 OOP Spring 2007 – Recitation 827 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 catch es.

28 OOP Spring 2007 – Recitation 828 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.


Download ppt "OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8."

Similar presentations


Ads by Google