(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 7. Exceptions Templates 2/1. Throwing exceptions 14 }

Similar documents
Exceptions. CandC++ 7. Exceptions Templates. Throwing exceptions. Conveying information

C and C++ 7. Exceptions Templates. Alan Mycroft

Programming in C and C++

Exercise 1.1 Hello world

C and C++ 8. Standard Template Library (STL) Stephen Clark

COMP6771 Advanced C++ Programming

C++ Templates. David Camp

C++ C and C++ C++ fundamental types. C++ enumeration. To quote Bjarne Stroustrup: 5. Overloading Namespaces Classes

C++ Exception Handling 1

COMP6771 Advanced C++ Programming

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Data Structures (INE2011)

(heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford. 1. Types Variables Expressions & Statements 2/23

Before we dive in. Preprocessing Compilation Linkage

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

COMP6771 Advanced C++ Programming

Programming in C and C++

Cpt S 122 Data Structures. Templates

eingebetteter Systeme

Object Oriented Programming with c++ Question Bank

Exception Handling: A False Sense of Security Cargill, P1

Overview of C++: Part 1

A <Basic> C++ Course

Module 9. Templates & STL

C++ Functions. Procedural-based Programming. what s a functions

The paramaterless ctor (aka default ctor)

Design Patterns in C++

Chapter 12 - Templates

Midterm Review. PIC 10B Spring 2018

Object-Oriented Programming for Scientific Computing

Exercise 6.2 A generic container class

Object Oriented Software Design - II

05-01 Discussion Notes

Short Notes of CS201

COMP6771 Advanced C++ Programming

CS201 - Introduction to Programming Glossary By

CSCI-1200 Data Structures Fall 2018 Lecture 22 Hash Tables, part 2 & Priority Queues, part 1

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Part XI. Algorithms and Templates. Philip Blakely (LSC) C++ Introduction 314 / 370

An introduction to. Templates. Generic Programming. Good old C. Metaprogramming 4/13/2017

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

COMP6771 Advanced C++ Programming

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Coding conventions and C++-style

COMP 2355 Introduction to Systems Programming

Evaluation Issues in Generic Programming with Inheritance and Templates in C++

An Introduction to C++

double d0, d1, d2, d3; double * dp = new double[4]; double da[4];

CSCI 104 Templates. Mark Redekopp David Kempe

CPSC 427: Object-Oriented Programming

VALLIAMMAI ENGINEERING COLLEGE

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

AN OVERVIEW OF C++ 1

CPSC 427: Object-Oriented Programming

7.1 Optional Parameters

Tokens, Expressions and Control Structures

Object Oriented Programming. Solved MCQs - Part 2

Programming in C and C++

21. Dynamic Datatypes and Memory Management

Introduction to C++ Systems Programming

Chapter 15 - C++ As A "Better C"

CSCI-1200 Data Structures Fall 2017 Lecture 25 C++ Inheritance and Polymorphism

Structuur van Computerprogramma s 2

Chapter 1 Getting Started

21. Dynamic Datatypes and Memory Management

COMP 2355 Introduction to Systems Programming

STL Quick Reference for CS 241

Interview Questions of C++

Praktikum: Entwicklung interaktiver eingebetteter Systeme

06-Oct-18. Lecture No.05. Infix to Postfix A + B A B (A + B)*(C D ) A B + C D * A B * C D + E/F A B C*D E F/+

CSI33 Data Structures

Chapter 11. Abstract Data Types and Encapsulation Concepts

Programming in C++: Assignment Week 8

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CS250 Final Review Questions

ECE Fall 20l2, Second Exam

CS 240 Data Structure Spring 2018 Exam III 04/25/2018

CS

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

CS 247: Software Engineering Principles. C++ Templates. Reading: Eckel, Vol. 2 Ch. 5 Templates in Depth. U Waterloo CS247 (Spring 2017) p.

CSCI-1200 Computer Science II Spring 2006 Test 3 Practice Problem Solutions

CS 376b Computer Vision

CHAPTER 3 STACKS AND QUEUES. Iris Hui-Ru Jiang Fall 2008

G52CPP C++ Programming Lecture 16

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2015 EOM, HYEONSANG ALL RIGHTS RESERVED

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

EL2310 Scientific Programming

Laboratorio di Tecnologie dell'informazione

17. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

Debug C++ Without Running. Anastasia Kazakova

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

mywbut.com Exception Handling

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

M.C.A DEGREE EXAMINATION,NOVEMBER/DECEMBER 2010 Second Semester MC 9222-OBJECT ORIENTED PROGRAMMING (Regulation 2009)

Objects Managing a Resource

Programming in C++: Assignment Week 8

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Transcription:

Exceptions Some code (e.g. a library module) may detect an error but not know what to do about it; other code (e.g. a user module) may know how to handle it C++ provides exceptions to allow an error to be communicated In C++ terminology, one portion of code throws an exception; another portion catches it. If an exception is thrown, the call stack is unwound until a function is found which catches the exception If an exception is not caught, the program terminates /1 CandC++ 7. Exceptions Templates Stephen Clark University of Cambridge (heavily based on last year s notes (Andrew Moore) with thanks to Alastair R. Beresford and Bjarne Stroustrup) Michaelmas Term 011 1/1 Conveying information The thrown type can carry information: 1 struct MyError { int errorcode; 3 MyError(i):errorcode(i) {} 4 }; 5 6 void f() {... throw MyError(5);... } 7 8 try { 9 f(); } 11 catch (MyError x) { 1 //handle error (x.errorcode has the value 5) 13... 14 } Throwing exceptions Exceptions in C++ are just normal values, matched by type A class is often used to define a particular error type: class MyError {}; An instance of this can then be thrown, caught and possibly re-thrown: 1 void f() {... throw MyError();... }... 3 try { 4 f(); 5 } 6 catch (MyError) { 7 //handle error 8 throw; //re-throw error 4/1 3/1

3 struct SomeError {virtual void print() = 0;}; 4 struct ThisError : public SomeError { 5 virtual void print() { 6 std::cout << "This Error" << std::endl; 7 } 8 }; 9 struct ThatError : public SomeError { virtual void print() { 11 std::cout << "That Error" << std::endl; 1 } 13 }; 14 int main() { 15 try { throw ThisError(); } 16 catch (SomeError& e) { //reference, not value 17 e.print(); 18 } 19 return 0; 0 } Handling multiple errors Multiple catch blocks can be used to catch different errors: 1 try {... 3 } 4 catch (MyError x) { 5 //handle MyError 6 } 7 catch (YourError x) { 8 //handle YourError Every exception will be caught with catch(...) Class hierarchies can be used to express exceptions: 6/1 5/1 Templates Templates support meta-programming, where code can be evaluated at compile-time rather than run-time Templates support generic programming by allowing types to be parameters in a program Generic programming means we can write one set of algorithms and one set of data structures to work with objects of any type We can achieve some of this flexibility in C, by casting everything to void * (e.g. sort routine presented earlier) The C++ Standard Template Library (STL) makes extensive use of templates 8/1 Exceptions and local variables When an exception is thrown, the stack is unwound The destructors of any local variables are called as this process continues Therefore it is good C++ design practise to wrap any locks, open file handles, heap memory etc., inside a stack-allocated class to ensure that the resources are released correctly 7/1

Creating a stack template Aclasstemplateisdefinedas: 1 template<class T> class Stack {... 3 } Where class T can be any C++ type (e.g. int) When we wish to create an instance of a Stack (say to store ints) then we must specify the type of T in the declaration and definition of the object: Stack<int> intstack; We can then use the object as normal: intstack.push(3); So, how do we implement Stack? Write T whenever you would normally use a concrete type / 1 An example: a stack The stack data structure is a useful data abstraction concept for objects of many different types In one program, we might like to store a stack of ints In another, a stack of NetworkHeader objects Templates allow us to write a single generic stack implementation for an unspecified type T What functionality would we like a stack to have? bool isempty(); void push(t item); T pop();... Many of these operations depend on the type T 9/1 1 #include "example16.hh" 3 template<class T> void Stack<T>::append(T val) { 4 Item **pp = &head; 5 while(*pp) {pp = &((*pp)->next);} 6 *pp = new Item(val); 7 } 8 9 //Complete these as an exercise template<class T> void Stack<T>::push(T) {/*... */} 11 template<class T> T Stack<T>::pop() {/*... */} 1 template<class T> Stack<T>::~Stack() {/*... */} 13 14 int main() { 15 Stack<char> s; 16 s.push( a ), s.append( b ), s.pop(); 17 } 1 template<class T> class Stack { 3 struct Item { //class with all public members 4 T val; 5 Item* next; 6 Item(T v) : val(v), next(0) {} 7 }; 8 9 Item* head; 11 Stack(const Stack& s) {} //private 1 Stack& operator=(const Stack& s) {} // 13 14 public: 15 Stack() : head(0) {} 16 ~Stack(); 17 T pop(); 18 void push(t val); 19 void append(t val); 0 }; 1 / 1 11 / 1

Default parameters Template parameters may be given default values 1 template <class T,int i=18> struct Buffer{ T buf[i]; 3 }; 4 5 int main() { 6 Buffer<int> B; //i=18 7 Buffer<int,56> C; 8 } 14 / 1 Template details A template parameter can take an integer value instead of a type: template<int i> class Buf { int b[i];... }; Atemplatecantakeseveralparameters: template<class T,int i> class Buf { T b[i];... }; A template can even use one template parameter in the definition of a subsequent parameter: template<class T, T val> class A {... }; A templated class is not type checked until the template is instantiated: template<class T> class B {const static T a=3;}; B<int> b; is fine, but what about B<B<int> > bi;? Template definitions often need to go in a header file, since the compiler needs the source to instantiate an object 13 / 1 Templated functions A function definition can also be specified as a template; for example: 1 template<class T> void sort(t a[], const unsigned int& len); The type of the template is inferred from the argument types: int a[] = {,1,3}; sort(a,3); = T is an int The type can also be expressed explicitly: sort<int>(a) There is no such type inference for templated classes Using templates in this way enables: better type checking than using void * potentially faster code (no function pointers) larger binaries if sort() is used with data of many different types 16 / 1 Specialization The class T template parameter will accept any type T We can define a specialization for a particular type as well: class A {}; 3 4 template<class T> struct B { 5 void print() { std::cout << "General" << std::endl;} 6 }; 7 template<> struct B<A> { 8 void print() { std::cout << "Special" << std::endl;} ; 1 B<A> b1; 13 B<int> b; 14 b1.print(); //Special 15 b.print(); //General 16 } 15 / 1

Overloading templated functions Templated functions can be overloaded with templated and non-templated functions Resolving an overloaded function call uses the most specialised function call If this is ambiguous, then an error is given, and the programmer must fix by: being explicit with template parameters (e.g. sort<int>(...)) re-writing definitions of overloaded functions Overloading templated functions enables meta-programming: 18 / 1 3 template<class T> void sort(t a[], const unsigned int& len) { 4 T tmp; 5 for(unsigned int i=0;i<len-1;i++) 6 for(unsigned int j=0;j<len-1-i;j++) 7 if (a[j] > a[j+1]) //type T must support "operator>" 8 tmp = a[j], a[j] = a[j+1], a[j+1] = tmp; 1 const unsigned int len = 5; 13 int a[len] = {1,4,3,,5}; 14 float f[len] = {3.14,.7,.54,1.6,1.41}; 15 16 sort(a,len), sort(f,len); 17 for(unsigned int i=0; i<len; i++) 18 std::cout << a[i] << "\t" << f[i] << std::endl; 1 17 / 1 Exercises 1. Provide an implementation for: template<class T> T Stack<T>::pop(); and template<class T> Stack<T>::~Stack();. Provide an implementation for: Stack(const Stack& s); and Stack& operator=(const Stack& s); 3. Using meta programming, write a templated class prime, which evaluates whether a literal integer constant (e.g. 7) is prime or not at compile time. 4. How can you be sure that your implementation of class prime has been evaluated at compile time? 0 / 1 Meta-programming example 3 template<unsigned int N> inline long long int fact() { 4 return N*fact<N-1>(); 5 } 6 7 template<> inline long long int fact<0>() { 8 return 1; 1 std::cout << fact<0>() << std::endl; 13 } 19 / 1