CS 11 C++ track: lecture 6 Today: Default function arguments friend classes Introduction to exception handling.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Advertisements

Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
CSE 332: C++ exceptions Overview of C++ Exceptions Normal program control flow is halted –At the point where an exception is thrown The program call stack.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Java Exceptions. Types of exceptions  Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot.
1 CSC241: Object Oriented Programming Lecture No 28.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS Advanced C++ Exception Handling Topic #5.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
OOP Spring 2007 – Recitation 81 Object Oriented Programming Spring 2007 Recitation 8.
Exceptions Objectives At the conclusion of this lesson, students should be able to Explain the need for exceptions Correctly write programs that use.
Arrays.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
1 CSC241: Object Oriented Programming Lecture No 27.
CSE 332: C++ functions Review: What = and & Mean In C++ the = symbol means either initialization or assignment –If it’s used with a type declaration, it.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Operator Overloading & Exception Handling TCP1201 OOPDS 1 Lecture 5 1.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Exception Handling. C++ 2 Outline  Throwing and handling exceptions  Exceptions of different types  The new operator and the exceptions  Re-throwing.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Overview of C++ Templates
 2003 Prentice Hall, Inc. All rights reserved. 1 Vectors.
Function Overloading Two different functions may have the same name as long as they differ in the number or types of arguments: int max(int x, int y) and.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
Introduction to Programming Lecture 40. Class Class is a user defined data type.
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
CMSC 202 Lesson 5 Functions I. Warmup Use setw() to print the following (in tabular format) Fred Flintstone Barney Rubble Bugs Bunny Daffy Duck.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Exception Handling How to handle the runtime errors.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
C++ Functions A bit of review (things we’ve covered so far)
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
C++ Arrays SarMag Trimester 31 C++ Arrays. C++ Arrays SarMag Trimester 32 C++ Arrays An array is a consecutive group of memory locations. Each group is.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Introduction to Programming
Exceptions.
CS 2704 Object Oriented Software Design and Construction
Why exception handling in C++?
Exceptions with Functions
Introduction to Programming
Exceptions CSCE 121 J. Michael Moore
Abstraction: Generic Programming, pt. 2
Exceptions 1 CMSC 202.
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
Object-Oriented Programming (OOP) Lecture No. 44
Lecture 9.
CMSC 202 Lesson 20 Exceptions 1.
More C++ Concepts Exception Handling Templates.
Presentation transcript:

CS 11 C++ track: lecture 6 Today: Default function arguments friend classes Introduction to exception handling

Default function arguments (1) class Foo { private: int x, y; public: Foo() { x = y = 0; } Foo(int x, int y) { this->x = x; this->y = y; } //... };

Default function arguments (2) class Foo { private: int x, y; public: Foo(int x=0, int y=0) { this->x = x; this->y = y; } //... };

Default function arguments (3) Now we can do this: Foo f(); // x = y = 0; Foo f(2); // x = 2; y = 0; Foo f(2,3); // x = 2; y = 3; Default args filled in from left to right

Default function arguments (4) CANNOT do this: Foo(int x=0; int y) { /*... */ } All default args must be at end of argument list Otherwise ambiguous

friend classes (1) Last time saw friend functions Way to allow special access privileges for a non-member function Can declare an entire class as a friend

friend classes (2) // in Vector.hh: class Matrix; // empty decl class Vector { friend class Matrix; //... };

friend classes (3) // in Matrix.hh: class Vector; // empty decl class Matrix { friend class Vector; //... };

friend classes (4) Now Vector s and Matrix es can access each others’ internal representations Useful for efficiently defining e.g. vector/matrix multiplication Empty decls avoid #include loop Use sparingly!

Exception handling (1) Exceptions are better way to handle errors Error occurs  throw an exception Can catch an exception and deal with it Can create own exception objects e.g. class ArrayOutOfBounds { /*... */ };

Exception handling (2) // in Matrix.cc int Matrix::getelem(int row, int col) { if ((row < 0) || (col < 0)) { // etc. throw ArrayOutOfBounds(); } // get and return the element }

Exception handling (3) // the try block try { Matrix m(10, 10); cout << m.getelem(100, 100) << endl; } catch (ArrayOutOfBounds) { cout << "Oops!" << endl; }

Exception handling (3) // Can identify the exception object: try { Matrix m(10, 10); cout << m.getelem(100, 100) << endl; } catch (ArrayOutOfBounds a) { // a is now the exception object cout << "Oops!" << endl; }

Exception handling (4) Exception classes can save state: class ArrayOutOfBounds { private: int row, col; public: ArrayOutOfBounds(int row, int col) { this->row = row; this->col = col; } void display(); };

Exception handling (5) void ArrayOutOfBounds::display() { cout << "Invalid array access: " << "row = " << row << "col = " << col << endl; }

Exception handling (6) try { Matrix m(10, 10); cout << m.getelem(100, 100) << endl; } catch (ArrayOutOfBounds a) { a.display(); }

Exception handling (7) Can catch multiple exceptions in one try block: try { //... } catch (Exception1 e1) { //... } catch (Exception2 e2) { //... } catch (...) { // Catches ANY exception! // Try not to use this. }

Exception handling (8) Exceptions not caught propagate up the stack to caller of function, then its caller, etc. etc. all the way up to main() (top level) If not caught there, program aborts

Next week Templates! By far the most interesting and tricky part of C++ Answers the burning question: how do I reuse the same code for Matrix of ints, doubles, etc.?