/
Exceptions  & Templates Exceptions  & Templates

Exceptions & Templates - PowerPoint Presentation

conchita-marotz
conchita-marotz . @conchita-marotz
Follow
357 views
Uploaded On 2020-01-14

Exceptions & Templates - PPT Presentation

Exceptions amp Templates httpwwwcpluspluscomdoctutorialexceptions httpwwwcpluspluscomreferencestl Error handling in C define NOSEATSAVIALABLE 1 define CREDITCARD DECLINED 2 ID: 772768

cpp return int pr16 return cpp pr16 int template code stl define exceptions reference classes templates param catch error

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Exceptions & Templates" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Exceptions & Templates http://www.cplusplus.com/doc/tutorial/exceptions/ http://www.cplusplus.com/reference/stl/

Error handling in C #define NO_SEATS_AVIALABLE -1 #define CREDITCARD _DECLINED -2 #define INCORRECT_FLIGHT_NUM -3 #define DATABASE_CONNECTIVITY_ISSUE -10 int book_tickets ( customer_information , itinerary_info , ... ) { .... return DATABASE_CONNECTIVITY_ISSUE; ... return INCORRECT_FLIGHT_NUM; ... return NO_SEATS_AVIALABLE; ... return CREDITCARD _DECLINED; ... return record_number ; }

How that function is used? char * error_messages [] = { "No error", "No Seats Available", "Credit card declined", ... "Database Connectivity Issue" } … if (( ret_code = book_tickets (...)) < 0) // ugly, but works! Another mechanism is switch() statement fprintf ( stderr , error_messages [- ret_code ]); else rec_num = ret_code ; // proceed with normal processing.

try .. catch in C++ try { // code here throw xxx; } catch ( int param ) { cout << " int exception“ << param ; } catch ( char param ) { cout << "char exception“ < param ; } catch (...) { cout << "default exception"; }

Exceptions Almost anything can be thrown and caught. char, int , string, double, or object m1 m2 m3 m4 e3 e2 e1

Exceptions: Examples Chapter 16

Return code vs. Exceptions Unlike Exception, type for “return code” should match with method’s return type. Exception logically goes directly to the code where it is handled, but what happens actually?

Macros (C) – remember? #define max(x, y) (x > y ? x : y) Equivalent method(s)? Following is just one of them. int max( int x, int y) { if (x > y) return x; else return y;}

Function Templates Template <class T> T max(T x, T y) { if (x > y) return x; else return y; } Examples: pr16-07.cpp, pr16-08.cpp, pr16-09.cpp

Standard Template Library Contains both Template Classes and Function Templates Reference: http://www.cplusplus.com/reference/stl/

Template classes in STL Sequential Containers Vector – array list Deque – double ended queue List – linked list We will cover the following in future classes: Container adaptors Associative containers

Vector

STL algorithms (function templates) binary_search count for_each find max_elemet min_element random_shuffle Sort Example: pr16-14.cpp Reference: http://www.cplusplus.com/reference/algorithm/

deque container

list container You can still access the items like an array!

User defined template classes Example: pr16-11.cpp, pr16-12.cpp

STL algorthms Provided as part of C++ Standard Template Library. Example: pr16-14.cpp to pr16-19.cpp

Project 4 Class definition: Two extreme approaches Pointers vs Indices