CSE333 SECTION 7. Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T.

Slides:



Advertisements
Similar presentations
Java for C++ Programmers Second Night. Overview First Night –Basics –Classes and Objects Second Night –Enumerations –Exceptions –Input/Output –Templates.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
C/c++ 4 Yeting Ge.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
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.
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.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
CS Advanced C++ Exception Handling Topic #5.
Memory Management Object Life Cycle:  Construction  Allocation  Preinitialization  Initialization  Use  Destruction  Cleanup  Post cleanup  Deallocation.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
. Plab – Tirgul 10 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
© Copyright Eliyahu Brutman Exceptions. © Copyright Eliyahu Brutman Exceptions and Design Patterns - 2 Introduction to Exception Handling Definition:
Plab – Tirgul 8 Exceptions. Error handling in C Up to now we handled our errors in the “C way”: assert return codes global error variable ( errno and.
CSE333 SECTION 7. Midterm Debrief Hex View 1. Find a hex editor. 2. Learn ‘goto offset’ command. 3. See HW3 pictures. The header: Magic word Checksum.
Java and C++, The Difference An introduction Unit - 00.
CPSC 252 Exception Handling Page 1 Exceptions and exception handling Client programmers can make errors using a class attempting to dequeue an item from.
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
1 CSC241: Object Oriented Programming Lecture No 27.
Chapter 13. Binary Files In binary files we do not need to format data File streams include two member functions specifically designed to input and output.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
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,
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Defining New Types Lecture 21 Hartmut Kaiser
6. Testing and Verification
C++ Memory Overview 4 major memory segments Key differences from Java
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.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS212: Object Oriented Analysis and Design Lecture 20: Exception Handling-II.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Digging into the GAT API Comparing C, C++ and Python API‘s Hartmut Kaiser
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
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.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Exceptions Exception handling.
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
CHAPTER 18 C – C++ Section 1: Exceptions. Error Handling with Exceptions Forces you to defend yourself Separates error handling code from the source.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Yan Shi CS/SE 2630 Lecture Notes
C ++ MULTIPLE CHOICE QUESTION
Exception handling.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
CS 2704 Object Oriented Software Design and Construction
Pointers and Dynamic Arrays
Jim Fawcett CSE687 – Object Oriented Design Spring 2001
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Object-Oriented Programming (OOP) Lecture No. 45
Why exception handling in C++?
HW3 Hex View Inheritance Constructors/Destructors
CMSC 202 Lesson 21 Exceptions II.
CSE 333 – Section 7 HW3 Hex View Inheritance Constructors/Destructors
CSE 333 – Section 7 HW3 Hex View Inheritance Constructors/Destructors
Chapter 15 Pointers, Dynamic Data, and Reference Types
Overview of Memory Layout in C++
Exceptions 2 CMSC 202.
CSC 270 – Survey of Programming Languages
Pointers and References
SPL – PS3 C++ Classes.
Presentation transcript:

CSE333 SECTION 7

Template vs Generics C++ templates are similar to preprocessor macros A template will be “materialized” into multiple copies with T replaced Accepts primitive data types and classes Java generic can only be used on classes

C++ Exceptions Provide a way to react to exceptional in programs by transferring control to special handlers try { throw 20; } catch (int e) { cout << "An exception occurred. " << e << '\n'; }

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

C++ Exceptions std::exception is the base class specifically designed to declare objects to be thrown as exceptions Has a virtual member function called “what” that returns a null-terminated character sequence (of type char *) containing some sort of description of the exception. class myexception: public exception { virtual const char* what() const throw() { return "My exception happened"; } } myex;

C++ Exceptions C++ Standard Library also uses exceptions: int main () { try { int* myarray= new int[1000]; } catch (exception& e) { cout << "Standard exception: " << e.what() << endl; } return 0; }

C++ vs Java Exceptions In C++, all types (including primitive and pointer) can be thrown as exception only throwable objects in Java In C++, there is a special catch called “catch all” that can catch all kind of exceptions catch (...) // catch all In Java, there is a block called finally that is always executed after the try-catch block. no such block in C++ A few other subtle differences

Exception Safety No-throw guarantee Strong exception safety: commit or rollback Basic exception safety: no-leak guarantee No exception safety: no guarantees are made

Resource Acquisition Is Initialization Holding a resource is tied to object lifetime: Resource allocation (acquisition) is done during object creation (specifically initialization), by the constructor, Resource deallocation (release) is done during object destruction, by the destructor. If objects are destructed properly, resource leaks do not occur.

Example void write_to_file (const std::string & message) { // mutex to protect file access static std::mutex mutex; // lock mutex before accessing file std::lock_guard lock(mutex); // try to open file std::ofstream file("example.txt"); if (!file.is_open()) throw std::runtime_error("unable to open file"); // write message to file file << message << std::endl; }

Hex View 1. Find a hex editor. 2. Learn ‘goto offset’ command. 3. See HW3 pictures. The header: Magic word Checksum Doctable size Index size

Hex View The doctable

Hex View The doctable (part 1): Num buckets ( Chain len Bucket offset )*

Hex View The doctable

Hex View The doctable (part 2): ( (Element offset) n ( DocID Filename len Filename ) n )*

Hex View The doctable

Hex View The index

Hex View The docID table