• Keine Ergebnisse gefunden

“Object-Oriented Programming for Scientific Computing”

N/A
N/A
Protected

Academic year: 2021

Aktie "“Object-Oriented Programming for Scientific Computing”"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

IWR, Heidelberg University Summer Semester 2015

Exercise Sheet 5 12. 05. 2015

Exercises for the Lecture Series

“Object-Oriented Programming for Scientific Computing”

Ole Klein

ole.klein@iwr.uni-heidelberg.de

To be handed in on 19. 05. 2015 before the lecture

EXERCISE1 EXCEPTIONS ANDDESTRUCTORS

What happens when a destructor throws an ex- ception? Let’s look at the program to the right.

• Augment the example with the definition of the class my_exception, which is de- rived from std::exception. The class my_exception should receive an error message in astd::stringin its construc- tor. The virtual methodwhat()should re- turn this error message.

• What do you observe when executing the program?

• Try to explain the behavior.

1 #include <iostream>

2 #include <string>

3 #include <exception>

4

5 // class Foo throws in the destructor 6 classFoo {

7 public:

8 ~Foo () {

9 throw my_exception("Foo exception");

10 }

11 };

12

13 // class Bar throws in the constructor 14 classBar {

15 public:

16 Bar () {

17 throw my_exception("Bar exception");

18 }

19 };

20

21 intmain() 22 try{

23 Foo f;

24 Bar b;

25 }

26 catch(conststd::exception & e){

27 std::cout << "ERROR:" << e.what() <<std::endl;

28 }

• Section 15.2, point 3 of theC++standard:

3 The process of calling destructors for automatic objects constructed on the path from a try block to a throw-expression is called “stackunwinding.” [Note: If a destructor called during stack unwinding exits with an exception, terminate is called (15.5.1). So destructors should generally catch exceptions and not let them propagate out of the destructor. —end note]

Why is the behavior detailed in thenotesensible?

Further reading: a collection of the possible options when destructors and exceptions meet can be found under http://www.kolpackov.net/projects/c++/eh/dtor-1.xhtml 8 Points

(2)

EXERCISE2 EXCEPTIONS ANDSANITYCHECKS

The classNumVectoron the last exercise sheet did not do bounds checking, i.e. it was possible to access indices smaller than zero or larger than the largest index. Modify the methodoperator[], so that erroneous access results in an exception being thrown (comparable to the behavior of the methodstd::vector<T>::atinstead ofstd::vector<T>::operator[]).

Also implement a methodoperator*that calculates the scalar product of two vectors. Exceptions that may result from incompatible lengths should be caught and a new, more detailed exception should be thrown.

The exceptions should be classes written by you and have distinct types. Write a program that tests both exceptions and prints a detailed error message in the case of errors without terminating the

program. 8 Points

EXERCISE3 FATHER ANDSON

What do you think the Java code on the right will do? Translate it into equivalent C++ code (extends corresponds to public inheritance andThrowableto an exception).

Comment your program and write down in which order the lines are executed. Is this a va- lid C++ program? If yes, what does it do?

Source: Randall Munroe (xkcd.com)

4 Points

Referenzen

ÄHNLICHE DOKUMENTE

insert(p, t) Inserts the element t in front of the one the iterator p points to, and returns an iterator that points to the inserted element.. insert(p, i, j) As above, but for

replace_copy(b,e,out,v,v2) Create copy of all elements in range [b:e) re- placing elements which are equal to v with v2 , return iterator to end of copy.

• Traits can be used to specify types and values that depend on one or more template parameters. • Policies can be used to specify parts of algorithms as

Since the units are only used as a template parameter, this only affects the class type but does not require memory... Example: Numbers

This header also contains functions that provide a thread ID and the functionality to detach threads that afterwards run as a separate program (fork).. Mutual Exclusion The header

and no exercise group, because things are still being set up, but you are welcome to attend if you have questions about the lecture or exercises or something else to discuss.. E

~List (); // clean up the list and all nodes Node* first() const; // return a pointer to the first entry Node* next( const Node* n) const; // return a pointer to the node after n

Please modify your implementation again to obtain a doubly linked list: each element should also point to its predecessor.. What is