Input & Output

Input & Output

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

m4 m3 e1 e2 e3 Exceptions: Examples Chapter 16

Return code vs. Exceptions Unlike Exception, type for return code should match with methods 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 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

Recently Viewed Presentations

  • Rydych chi'n dditectif preifat llwyddiannus. Rydych chi wedi

    Rydych chi'n dditectif preifat llwyddiannus. Rydych chi wedi

    Rydych chi'n troi'r handlen ac mae'r drws yn gwichian ar agor. Mae hi'n dywyll y tu mewn. Wrth i chi gamu i mewn mae trapddor yn agor ac rydych chi'n plymio i lawr. Pwyswch y botwm 3.5 Cwestiwn 2 b...
  • By Del Siegle, PhD del.siegle@uconn.edu www.delsiegle.info Press the

    By Del Siegle, PhD [email protected] www.delsiegle.info Press the

    I have also created a PowerPoint presentation that demonstrates how to use the Excel spreadsheet. A correlated (or paired) t test is concerned with the difference between the average scores of a single sample of individuals who is assessed at...
  • Civil Engineering - Kentucky Transportation Cabinet

    Civil Engineering - Kentucky Transportation Cabinet

    Materials Engineers Design, analyze, and construct materials such as: Steel Concrete Masonry Asphalt Composites Structural Engineers Design and analyze all man-made objects whose primary function is load resistance: buildings, bridges, aircraft, transmission towers, radar domes and antennas, drilling platforms, etc....
  • Week 6: Globalization

    Week 6: Globalization

    Offers no universalizable political alternative to liberalism. Nationalism. Offers no generalizable critique of liberalism. Only critical of some particular expressions of liberalism through specific non-representative governments . Because it has no generalizable critique of liberalism, nationalism is not necessarily incompatible...
  • Transformations of Equations

    Transformations of Equations

    Transformations of Functions Viviana C. Castellón East Los Angeles College MEnTe Mathematics Enrichment through Technology Absolute Value An Absolute Value graph is always in a "V" shape.
  • REACH 2018 Predlote registran dokumentciu el prezentcie  Tto

    REACH 2018 Predlote registran dokumentciu el prezentcie Tto

    Nachádzajú sa tu aj rôzne možnosti vyhľadávania. Možnosť „quick search by number" (rýchle vyhľadávanie podľa čísla) sa nachádza v hornom banneri na každej stránke v REACH-IT. V rámci tejto vyhľadávacej možnosti sa používa číslo EC alebo CAS látky, podacie číslo...
  • Sports Development Participation to Performance Mr Kirby Olympic

    Sports Development Participation to Performance Mr Kirby Olympic

    PE, School Sport and Club Links Strategy (PESSYP) comprises of a number of programmes, for example: School Sport Partnerships. Specialist Sports Colleges. Also focus on facilities for school sport facilities. Target: 5 to 16 year olds taking part in at...
  • March 2011 Contents Radio Summary 2010 - Useful

    March 2011 Contents Radio Summary 2010 - Useful

    The second Digital Radio Industry Report was released by CRA in March 2011. Visit digitalradioplus.com.au to download. Radio in 2010 Summary - Digital * In the market supply chain (chip maker, manufacturer, and retailer) supported by GfK sales data, and...