• 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 2 21. 04. 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 28. 04. 2015 before the lecture

EXERCISE1 POINTERS

Letihave the typeint, andpthe type int *.

Which of the following expressions are correct, which are incorrect? Also list the types of the cor- rect ones. Answer without taking concrete values foriandpinto account.

• i + 1

• *p

• *p + 3

• &i == p

• i == *p

• &p

• p + 1

• &p == i

• **(&p)

• *p + i > i

2 Points

EXERCISE2 DESTRUCTOR

Which of the following statements are true? The destructor of a classCis accountable for. . .

• . . . cleaning up all objects of the classC.

• . . . cleaning up objects of the classCon the heap.

• . . . cleaning up all components of objects of the classC.

• . . . cleaning up components of objects of the classCthat are on the heap.

• deleteis just a special way of calling the destructor: letxbe of typeC*, thendelete x; is the same as(*x).~C()

Explain your reasoning, since the correctness of the statements is at least partially subject to inter-

pretation. 2 Points

EXERCISE3 NEW&DELETE

1. Why is:

int* get_int1 () {

int* p;

p = new int;

return p;

}

a reasonable method to create a reference to a newintvariable, while in contrast

int* get_int2 () {

int i;

int* p = &i;

return p;

}

is completely unsuitable?

2. Assume the following definitions and com- mands have been executed:

int* p;

p = new int;

*p = 17;

What happens when p = 0;

delete p;

or

delete p;

p = 0;

is executed afterwards? Which of the snip- pets is sensible, which isn’t, and why?

4 Points

(2)

EXERCISE4 LINKEDLIST

Using the easy example of a chained list we will practice the interaction of constructors, destructors and pointers.

We want to program a linked list, which can store an arbitrary number of values of typeint. A list consists of an object of classList, which refers to a sequence of objects of class Node. The list elements are stored in a componentint valuewithin each node and a pointerNode* nextpoints at the next node. The end of the list is designated by the pointernexthaving the value0.

1. What is special about a pointer having the value0?

2. Implement the classNode. Make sure that all member variables are always initialized.

3. Implement the classListwith the following methods:

class List {

public:

List (); // create an empty list

~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 void append (int i); // append a value to the end of the list void insert (Node* n, int i); // insert a value before n

void erase (Node* n); // remove n from the list };

Listmust also store the beginning of the list, where would you place it in the class declaration?

Thenextpointer should beprivateto ensure that the list structure isn’t accidentally changed outside of class List. The member value is public to allow read and write access from outside the class. The line

friend class List;

has to be inserted into the declaration of the classNode to give the List class access to the nextpointer. Additionally make sure that the destructor deletes all allocatedNodeobjects.

4. Test your implementation with the following program:

int main () {

List list;

list.append(2);

list.append(3);

list.insert(list.first(), 1);

for (Node* n = list.first(); n != 0; n = list.next(n)) std::cout << n->value << std::endl;

return 0;

}

5. What happens if one copies the list? And what happens if both lists are deleted?

int main () {

List list;

list.append(2);

...

List list2 = list;

return 0;

}

12 Points

Referenzen

ÄHNLICHE DOKUMENTE

11 The [ 1s-2r ] network is the most efficient at a rate of almost 94% and is independent of flow of information as the rates of trust are high in both information

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

ColIterator&amp; operator++(); // move to next entry bool operator ==() const; // comparison of iterators T&amp; operator *(); // access to current entry const T&amp; operator

We now want to extend the calculation of the norm in order to make it work for very different types of numbers.. We intend to realize this using the concept of

What problem would this cause? Have a look at the watt W for this, the unit of power. In this case, no extension of the basic units is necessary, but non-integer exponents for the

In order to hide all the constructions containing StackInterface and StackImpl from the user, the Stack finally receives a templatized constructor which receives an arbitrary