Topics: Templates Exceptions

Slides:



Advertisements
Similar presentations
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Advertisements

C/c++ 4 Yeting Ge.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
1 Chapter 17-2 Templates and Exceptions Dale/Weems.
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.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
C++ Spring 2000 Arrays1 C++ Arrays. C++ Spring 2000 Arrays2 C++ Arrays An array is a consecutive group of memory locations. Each group is called an element.
Introduction to C++ Templates and Exceptions l C++ Function Templates l C++ Class Templates l Exception and Exception Handler.
(…A FEW OF THEM) C++ DESIGN PATTERNS. WHAT ARE THEY? Commonly occurring constructs Could be part of good software engineering Not universally agreed Good.
Handling ErrorstMyn1 Handling Errors Up to this point we haven't worried much about errors or exceptions. First, let's distinguish between errors and exceptions.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Review Binary Numbers Bit : 0 or 1 Byte: 8 bites 256 different values 2 8 KB : 1024 bytes 2 10 bytes MB : 1024 * 1024 bytes 2 10 * 2 10 (2 20 ) bytes GB.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Exception Handling How to handle the runtime errors.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Introduction to C++ Templates and Exceptions C++ Function Templates C++ Class Templates Exception and Exception Handler.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Lecture 3: Getting Started & Input / Output (I/O)
C++ Lesson 1.
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Exceptions.
Exceptions: When things go wrong
Object-Oriented Programming (OOP) Lecture No. 21
Why exception handling in C++?
OOP-4-Templates, ListType
Part IX Fundamentals of C and C++ Programming Exception Handling
EXCEPTION HANDLING.
Templates.
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
CMSC 202 Lesson 21 Exceptions II.
Object-Oriented Programming (OOP) Lecture No. 32
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
ㅎㅎ Fourth step for Learning C++ Programming Two functions
Topics Introduction to File Input and Output
Exceptions with Functions
Chapter 17 Templates and Exceptions Part 2
CMSC 202 Templates.
Introduction to Programming
Counting Loops.
Exception Handling CSCI293 - C# October 3, 2005.
CMSC 202 Lesson 22 Templates I.
Exceptions CSCE 121 J. Michael Moore
Exception Handling.
Exceptions 1 CMSC 202.
CS150 Introduction to Computer Science 1
C++ Pointers and Strings
CS150 Introduction to Computer Science 1
Object-Oriented Programming (OOP) Lecture No. 43
C++ winter2008(by:J.Razjouyan)
Templates I CMSC 202.
Functions Lecture 5.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Object-Oriented Programming (OOP) Lecture No. 44
Topics Introduction to File Input and Output
Department of Computer and Information Science, School of Science, IUPUI Exception Handling Dale Roberts, Lecturer Computer Science, IUPUI
Lecture 9.
Templates CMSC 202, Version 4/02.
COP 3330 Object-oriented Programming in C++
Lecture 3 More on Flow Control, More on Functions,
CMSC 202 Lesson 20 Exceptions 1.
More C++ Concepts Exception Handling Templates.
Presentation transcript:

Topics: Templates Exceptions Lecture 05: More C++ Topics: Templates Exceptions

Part I. Templates A way to "generalize" a function or class. You pass parameters when using a function or creating a class. Often the compiler can deduce these.

Motivation Suppose we write a function that does a sort of an array of floats: void sort(float * L, int size); If we wanted to make a function that sorts an array of int's we'd have to duplicate the entire function, just changing the parameter type (and the type of any temporary variables) What about a list of strings? of Monsters? Copy/Paste == BAD Templates to the rescue! [Do an example in code]

Template Functions template <class T> void foo(T x) { T temp; // … } // elsewhere foo(5); // Create a version of foo with T = int foo(3.7); // Create a version of foo with T = double foo("ABC"); // Create a version of foo with T = ?? // Probably const char * foo(string("ABC")); // Create a version of foo with T = string foo<string>("ABC"); // Create a version of foo with T = string // and auto builds a string, passing the // const char * as an arg.

Templates Since template functions are created as necessary, it's not possible to put them in a .cpp file. So…you must put them only in a .h file.

Template specialization In certain cases, we need to provide a specialized version of a template [Use strings with the sort function] General syntax // General case template <class T> void foo() { /* … */ } // Specific version (strings only) template <> void foo<string>() { /* … */ }

Template classes You can make a template-ized class The attributes, return types, parameters, etc, can all use the template type T. template <class T> class Foo { public: void setValue(const T new_val) { mVal = new_val; } T getValue(); protected: T mVal; }; // External definition – still needs to be in a .h file. T Foo<T>::getValue() return mVal; }

Part II: Exceptions A way to indicate an error case. Will crash your program…unless you handle (catch) the exception. Two aspects: When writing code, throw an exception if you encounter an error case. When using other code, you can try to execute it and catch an error if it occurs.

Example float myDivide(float a, float b) { if (b == 0.0f) throw(15); return a / b; } int main() float x, y, z; cout << "Enter numerator: "; cin >> x; cout << "Enter denominator: "; cin >> y; try myDivide(x, y); catch (int err) if (err == 15) cout << "Division error!\n"; else cout << "Unknown error!\n";

Custom Exception types Integers aren't very descriptive. A better solution (?) class MyException { public: MyException(string file, int line, string desc) : mFile(file), mLine(line), mDesc(desc) {} void output() {cout << "[" << mFile << "@" << mLine; cout << "]: '" << mDesc << "'" << endl; } string mFile, mDesc; int mLine; }; // Somewhere in our code if ( /* ??? */ ) throw MyException(__FILE__, __LINE__, "Noob error!");

Custom Exception Types, cont. int main() { try // Some code that could fail } catch (int err) catch (MyException & err) err.output(); catch (...) // <- Valid C++ code cout << "An unhandled error!" << endl;