• 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 4 05.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 12. 05. 2015 before the lecture

EXERCISE1 EMPTYBASECLASSOPTIMIZATION

In this exercice we will examine how much space empty C++ classes and their derived classes use.

First create a class Empty, which doesn’t possess any attributes. Use inheritance to derive a class EmptyDerivedfromEmpty, which as well lacks attributes. Also create a classNonEmpty, which is again derived fromEmpty, but features a singlecharas attribute.

What is the size of objects of the classes Empty, EmptyDerivedand NonEmptyaccording to the sizeofoperator? (By the way: This behavior is called empty base class optimization.)

To understand why the size of empty classes (according to standard) is as observed, consider the following class

struct Composite {

Empty a;

int b;

};

What would happen if Empty wouldn’t claim any storage space? Let c be an object of the class Composite, which addresses would the attributesc.aandc.bhave in this case?

Determine the size of objects belonging to the class Composite! How does it change when you

change the type of the attributebtochar? (Try it!) 6 Points

EXERCISE2 INHERITANCE AND COMPOSITION

Write a classNumVector, which represents a numerical vector. The size of the vector has to be fixed.

Read and write access to the individual entries of the vector should be granted by means of the brackets operator. In addition, the class should have the methoddouble norm() that calculates the Euclidean norm of the vector. Other methods should not be visible. In particular, aNumVector should not be astd::vector<double>!

Design two implementations of this class, both of which store the values in astd::vector<double>. The first implementation has to be derived fromstd::vector<double>and the second one should not use inheritance. Use two different header files for this, and only include one of them at a time when testing with the programs below.

Test your implementation as follows: The classes should work correctly with the following main program.

#include<vector>

#include<iostream>

#include<cmath>

int main(){

NumVector v(3);

(2)

v[0]=1; v[1]=3, v[2]=4;

const NumVector& w=v;

std::cout<<"norm is "<<w.norm()<<std::endl;

std::cout<<"vector is ["<<w[0]<<", "<<w[1]<<", "<<w[2]<<"]"<<std::endl;

}

The following program must not compile though!

#include<vector>

#include<iostream>

#include<cmath>

void vectorTest(std::vector<double>& v){}

int main(){

NumVector v(3);

v.resize(2); // must be hidden, as other methods of std::vector!

vectorTest(v); // this should also lead to a compiler error!

}

10 Points EXERCISE3 INHERITANCE AND FUNCTIONS

Find the errors in the following program. What is the output after the incorrect lines are removed, and why?

1 #include<iostream>

2

3 class A{

4 public:

5 void foo() const { std::cout<<"A::foo"<<std::endl; } 6 };

7

8 class B : public A{

9 public:

10 void foo() { std::cout<<"B::foo"<<std::endl; } 11 };

12

13 class C : private B{

14 public:

15 void bar() { foo();}

16 };

17

18 void test(const A& a){ a.foo(); } 19

20 int main(){

21 A a; B b; C c;

22 a.foo();

23 b.foo();

24 test(b);

25 c.bar();

26 test(c);

27 }

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