¨ BASEL UNIVERSITAT Thorsten M¨ oller Alexander Haesen Mihai Rapcea cs109: Programming Paradigms – C++ Exercise 3 FS 2015 Deadline: 26.04.2015 23:59:59 Note: Please upload answers to the questions and source code before the deadline via courses.cs.unibas.ch. Running programs have to be demonstrated during the exercise slot on 27.04.2014 and 08.05.2014. Keep in mind that for the various exercises given throughout the course, you must score at least 2/3 of the total points (i.e., the sum of the points over all exercises) to be admitted to the final exam. Modalities of work: The exercise can be done in groups of at most 2 persons. Do not forget to provide the full name of all group members together with the submitted solution. Question 1: Constructor, Destructor, Inheritance (8 points) The accompanying file cons des handout.cpp (provided together with this exercise sheet on the Web site) contains the definition of classes One, Two, Three and Four. a) Complete class One such that it inherits from one of the three precedent classes. Define two variables of the two other classes somewhere in class One such that the main-function prints the subsequent output. As a hint, the variables can be defined at two of the four ...-indicated lines. 1 2 3 4 5 6 −> % . / c o n s d e s h a n d o u t C l a s s Two C o n s t r u c t o r : ONE C l a s s Four C o n s t r u c t o r : TWO C l a s s Three C o n s t r u c t o r : THREE C l a s s One C o n s t r u c t o r : FOUR C l a s s Three D e s t r u c t o r : FIVE 7 8 9 10 11 DESTRUCTION: C l a s s One D e s t r u c t o r : SIX C l a s s Four D e s t r u c t o r : SEVEN C l a s s Two D e s t r u c t o r : EIGHT (4 points) 1 b) Give reasons why the C++ standard defines constructor and destructor calls to be in that order. Think about problems that might arise if the order would be different? (2 points) c) Why is it dangerous to call a virtual function in a constructor or destructor of a base class? (2 points) Question 2: Java classes to C++ (6 points) The accompanying file e3-material.zip (provided together with this exercise sheet on the Web site) contains a folder javaToCpp that contains some Java classes and a handout-file java2cpp handout.cpp. Rewrite the Java classes in C++. Make sure that the functionality is equal. Use the attribute initializer for the constructors. Make sure that everything is always cleaned up correctly and that the deconstructors are called in order. Hint: One can emulate a Java interface in C++ as a class that has pure virtual functions only. The main function is already implemented in the handout-file. However, there is a problem with the the code. Explain what the issue is and correct the code. Question 3: Set as a generic class (5 points) In Exercise 2, you have implemented a set that used integer as a value. This makes the implementation not reusable for other types of values. Reimplement the set as a C++ class and use templates for the type of the value. Implement all functions as methods. Make sure that set instances are cleaned up correctly by freeing allocated memory. Add an empty constructor which can be used to create an empty set. Question 4: Matrix & Iterator as classes (8 points) In Exercise 2, you implemented a matrix and an iterator by means of structures. Now that you are familiar with the concept of classes in C++, implement two classes representing a matrix and an iterator, respectively. Similar to Exercise 2, the iterator iterates over a column of a matrix. Write a header and an implementation file for each class. Replace the create() and destroy() functions by a constructor and a destructor, respectively; the set value(), get value() and print() methods for the matrix, and the reset(), begin(), end(), next(), previous() and remaining() methods for the iterator remain the same. Following the ”rule of three” (or ”gang of four” as mentioned in the course), it is always best to write a constructor, a copy constructor, a destructor, and an assignment operator, if one of these has already been implemented. Since you have defined a constructor and a destructor on the Matrix- and Iterator-class, overload the assignment operator and write a copy constructor that performs a deep copy, at least up to the level of matrix elements, for these two classes as well. 2 Furthermore, overload the following operators for the Iterator-class: • >> for the next-operation (line 39 in the handout-file), • << for the previous-operation (line 54 in the handout-file). If your implementation is correct, the main-function in the handout file iterator handout e3.cpp should compile and run without errors. 3
© Copyright 2024