1 / 18

More C++ Concepts

More C++ Concepts. Exception Handling Templates. Exceptions. An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring at runtime When an exception occurs Do not do anything Cryptic error messages Sytem crash

jarceneaux
Télécharger la présentation

More C++ Concepts

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. More C++ Concepts • Exception Handling • Templates

  2. Exceptions • An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring at runtime • When an exception occurs • Do not do anything • Cryptic error messages • Sytem crash • Handle the error • Issue a warning and exit • Handle exception gracefully and continue • Examples: Divide by zero, Out of memory

  3. Where to handle • Can we handle in the same section of code where exception is raised? • Higher level code will have better idea as how to handle • Different programs using same classes will handle differently Separation of creation and handling of exception • Pass exceptions to calling functions • Need a mechanism for • Need to distinguish the code which can raise exceptions • Bundle and send the information to caller • Methods to operate on the information passed

  4. Handling exceptions • In C++, exception is an object (simple or user-defined) • A way to convey information to caller • Code that detects the abnormality throws the exception • try block – code that can raise (throw) exception • Code that handles catches the exception • catchblock – code that can handle (catch) the exception try { throw <object> } catch (<type> <objname>) { cout << "Exception"; } Exception object, say i Object, say int Name, say e

  5. int main() { int x = 5; int y = 0; int result; int exceptionCode = 25; try { if (y == 0) { throw exceptionCode; } result = x/y; } catch (int e) { if (e == 25) { cout << "Divide by zero" << endl; } else { cout << "Exception of unknown type" << endl; } } cout << "Goodbye" << endl; return 0; } Must agree on datatype Example Bundle information throw “Divide by zero”; catch ( string e ) { cout << e << endl; }

  6. If the exception thrown is of type specified for a handler, then handler is excuted • If not, pass the exception to caller • If no appropriate catching block, program terminates main () { try { foo (); } catch (int e) { printf("from main - %d\n", e); } } void foo ( void ) { try { int i = 10; throw i; } catch (string s) { cout << s ; } } Output: from main - 10

  7. g ( ) f ( ) main ( ) Stack Unwinding void f() { void g(); Foo x; g(); } void g() { Foo x; throw 1; } class Foo { public: Foo() {cout << "Foo constructor" << endl;} ~Foo() {cout << "Foo destructor" << endl;} }; main() { void f(); try // Turn on exception handling { f(); } catch(int) { cout << "Caught exception" << endl; } return 0; } Call Stack main ( ) f ( ) g ( )

  8. Output Foo constructor Foo constructor Foo destructor Foo destructor Caught exception

  9. Templates • Provides support for generic programming • Focus on algorithms and DS rather than on data types • Data types are parameters • Helps in developing generic & flexible behavior • Function Templates • Class Templates • Code for all types is centralized • Easy maintenance • Better re-usage of code

  10. Function Templates • Generic functions that can be used for arbitrary types • Perform identical operations on different types • Approaches • Naïve approach • Function overloading • Function templates void PrintInt( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void PrintChar( char ch ) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void PrintFloat( float x ) { … } void PrintDouble( double d ) { … } Naïve Approach PrintInt (sum); PrintChar (c); PrintFloat (angle);

  11. Function Overloading void Print( int n ) { cout << "***Debug" << endl; cout << "Value is " << n << endl; } void Print( char ch ) { cout << "***Debug" << endl; cout << "Value is " << ch << endl; } void Print( float f ) { ... } void Print( double d ) { ... } Print (sum); Print (c); Print (angle);

  12. Function Templates • Compiler generates multiple versions of a function based on parameterized data types FunctionTemplate Template < TemplateParamList > FunctionDefinition Template Parameters template <class T> void Print( T val ) { cout << "***Debug" << endl; cout << "Value is " << val << endl; } Print<int> (sum); Print<char> (initial); Print<float> (angle); Template Arguments

  13. Naïve Approach Different Function Definitions Different Function Names Function Overloading Different Function Definitions Same Function Name Template Functions One Function Definition (a function template) Compiler Generates Individual Functions

  14. Class Templates • Definition of generic classes with parameterized types ClassTemplate Template < TemplateParamList > ClassDefinition template <class T> class Stack { public: Stack(int n = 10) { stackPtr = new T[n]; } ~Stack() { delete [] stackPtr ; } int push(const T&); int pop(T&) ; // pop an element off the stack private: int size ; // Number of elements on Stack int top ; T* stackPtr ; }; Stack<int> iS; Stack<float> fS; Stack<char> cS; typedef Stack<int> intStk; typedef Stack<char> charStk; intStk iS; charStk cS;

  15. Buffer<char, 128> cbuf; Buffer<int, 100> ibuf; Buffer<Record, 8> rbuf; For each set of template arguments, compiler creates a separate class Process of generation of each class is Instantiation Each new class is Specialization template <class T, int max > class Buffer { public: Buffer ( ) { … } void add ( T item ); … private: T buf [ max ]; }; Record is a class template <class T, int max> void Buffer<T, max>::add ( T item ) { … }

  16. Template Instantiation Z<int> iz; // implicit instantiation of class Z<int> Z<float> fz; iz.g(); // generates function Z<int>::g( ) but not f( ) fz.f(); // generates function Z<float>::f( ) but not g( ) template Z<int>; // explicit instantiation of class Z<int> Z<int> *pi; // instantiation is NOT required template <class T> class Z { public: Z() { … } ; ~Z() { … } ; void f ( ) { … } ; void g ( ) { … }; }; int i = max ( 10, 20 ); // implicit: int max(int, int) char c = max ( ‘s’, ‘k’ ); template int max<int>(int); // explicit: int max(int, int) template <class T> T max ( T a, T b ) { return a > b ? a : b ; } Compiler will not instantiate new classes and methods unless it is required. It is an error to instantiate the template which is not defined.

  17. Type Equivalence • Each instantiation creates new type • Are Stack<int> and Stack<float> have same type? • typedef unsigned int Uint; Are Stack<unsigned int> and Stack<Uint> have same type?

More Related