12 C Programming Standard Template Library Homework 5
12. C++ Programming • Standard Template Library • Homework 5, 6, 7
Sequence Container Overview Library Name Description Example <vector> A dynamic array STL-vector. cpp <list> randomly changing sequence of items STL-list. cpp <stack> A sequence of items with pop and push at one end only (LIFO) - <queue> A Sequence of items with pop and push at opposite ends (FIFO) - <deque> Double Ended Queue with pop and push at both ends STL-deque. cpp 2
[ practice 1 Standard Template Library, List ] 3
[ explain 1 Standard Template Library, List ] 4
[ practice 2 Standard Template Library, Deque ] 5
[ explain 2 Standard Template Library, Deque ] 6
[ practice 3 Standard Template Library, Iterator ] 7
[ explain 3 Standard Template Library, Iterator ] 8
Associative Container Overview Library Name Description Example <set> An unordered collection of items - <map> An collection of pairs of items indexed by the first one STL-map. cpp <bitset> A subset of a fixed and small set of items (The class provides something like an array of bits / optimizing for space allocation ) - 9
[ practice 4 Standard Template Library, Map ] 10
[ explain 4 Standard Template Library, Map ] 11
Handout 5 Exercise 1: Class and object definition a) Define a class Student with the following attributes: Name : char * Number : integer Courses: Course * 12
Handout 5 b) Make each of the above attributes private and define for each attribute a pair of public get-set-methods 13
Handout 5 c) Create a constructor-method for your Student-class for initializing objects of the Student-Class. 14
Handout 5 d) Create a class Course with following attributes: Id: integer Instructor: char * Room. Nr: integer 15
Handout 5 e) Using the above two classes create a simple application that allows the assignment of students to courses (using the Courses attribute of the class Student). Your application should internally store a sequence of Student objects either by using an array of by using a linked list. 16
Handout 5 f) Extend your application by a query function that prints for a given course-Id the name and number of all students who attend this course. 17
Handout 6 Exercise 1: Given the following C++ code: class A { public : int x; A *obj. ARef; private : int y; protected : int z; }; class B : public A { public : A obj. A; }; class C { public : A obj. A; A *obj. ARef; B obj. B; }; Determine for each of the following attribute-access-expressions whether it results in an Error (Wrong) or not (OK). 18
Handout 6 in class A in class B in class C x OK ■ Wrong OK Wrong ■ y OK ■ Wrong OK Wrong ■ z OK ■ Wrong OK Wrong ■ obj. A. x OK Wrong ■ OK ■ Wrong obj. A. y OK Wrong ■ obj. A. z OK Wrong ■ obj. ARef->x OK ■ Wrong obj. ARef->y OK ■ Wrong OK Wrong ■ obj. ARef->z OK ■ Wrong OK Wrong ■ obj. B. x OK Wrong ■ OK ■ Wrong obj. B. y OK Wrong ■ obj. B. z OK Wrong ■ 19
Handout 6 Exercise 2: Given the following class hierarchy: 1. Create C++ code without attributes and methods for all 6 classes. • class Object { }; class Character : public Object{ }; class Digit : public Character{ }; class Letter : public Character{ }; class Vowel : public Letter{ }; class Consonant : public Letter{ }; 20
Handout 6 2. Extend the class character by a public attribute ch, so that it can store a single character. class Character : public Object{ public: char ch; }; 3. Overload the operator + for the class Character, so that it can add two objects of type Character operator + (const Character &r. Character) { Character Char_Ret; Char_Ret. ch = (ch + r. Character. ch) % 128; // ASCII return Char_Ret; } 21
Handout 6 4. Override the operator + in the Digit class, so that it adds the numeric value of two digits and delivers the digit that we get if we finally apply “modulo 10”. (Example ‘ 5’ + ‘ 6’ = ‘ 1’ // 5 + 6 = 11 % 10 = 1) class Digit : public Character{ public: Digit operator + (const Digit &r. Digit) { Digit Dig_Ret; Dig_Ret. ch = (((ch - '0') + (r. Digit. ch - '0')) % 10) + '0'; return Dig_Ret; } 5. Extend the Object class by an object counter that counts the number of created objects for all objects of the above class hierarchy. (Tip: Lecture 9 slide 5) The counter should be embedded into the Object-class default constructor. class Object { public: static int Int_Count ; Object() { ++Int_Count; cout << "Current Object created : " << Int_Count << endl; } }; int Object: : Int_Count = 0; 22
Handout 6 • 6. Change the visibility of the attribute ch, so that it is visible in all subclasses, but inaccessible from outside. Create a get-set method pair for the attribute ch. class Character : public Object{ public: Character operator + (const Character &r. Character) { Character Char_Ret; Char_Ret. ch = (ch + r. Character. ch) % 128; return Char_Ret; } char Get. Ch() { return ch; } void Set. Ch(char a_Ch) { ch = a_Ch; } protected: char ch; }; 7. Create a main-method, where you create 2 objects of each class in the above class hierarchy and that prints finally the value of your object counter (this should be 10). void main() { Character ch[2]; Digit dig[2]; Letter let[2]; Vowel vow[2]; Consonant con[2]; 23 }
Handout 7 Exercise 1: Given the following C++ code: #include <iostream> void fun() { try { std: : cout << "FAn"; throw 3; // line 5 std: : cout << "BAn"; } catch (int i) { std: : cout << "FCA " << i << "n"; } catch (char c) { std: : cout << "FCB " << c << "n"; throw; } std: : cout << "BCn"; } void main() { try { std: : cout << "An"; fun(); std: : cout << "Bn"; } catch (int i) { std: : cout << "C " << i << "n"; } catch (double d) { std: : cout << "D " << d << "n"; } catch (. . . ) { std: : cout << "En"; } std: : cout << "Fn"; } 24
Handout 7 1 a. What is the output of the above code? 25
Handout 7 1 b) What is the output of the above code, if the throw-statement in line 5 is replaced by: i) throw (double)5. 0; ii) throw 'c'; iii) throw true; First try to develop the answers by simply analysing the code. Afterwards take a compiler for verifying whether your answers were right or not. i) throw (double)5. 0; ii) throw 'c'; iii) throw true; 26
Handout 7 2. a) Extend the above code by adding an exception class for exception-object creation. Your exception class should comprise an error indicating attribute (e. g. by using an integer. ) and an appropriate constructor. class Exception { public: Exception(int Int_Code) { Int_Error. Code = Int_Code; } int Int_Error. Code; }; b) Change throw statement in line 5 so that it throws objects of your exception class. void fun() { try { std: : cout << "FAn"; throw Exception(3); // line 5 std: : cout << "BAn"; } catch (int i) { std: : cout << "FCA " << i << "n"; } catch (char c) { std: : cout << "FCB " << c << "n"; throw; } std: : cout << "BCn"; } 27
Handout 7 c) Add a catch block in main for catching exception-objects of your exception class. void main() { try { std: : cout << "An"; fun(); std: : cout << "Bn"; } catch (int i) { std: : cout << "C " << i << "n"; } catch (double d) { std: : cout << "D " << d << "n"; } catch (Exception ex) { std: : cout << "Exception " << ex. Int_Error. Code << "n"; } catch (. . . ) { std: : cout << "En"; } std: : cout << "Fn"; 28 }
Handout 7 d) Change your code so that it works with objects created on the heap by using the new operator. Discuss the positive and negative aspects of such an object-reference based approach for exception handling. #include <iostream> class Exception { public: Exception(int Int_Code) { Int_Error. Code = Int_Code; } int Int_Error. Code; }; void fun() { try { std: : cout << "FAn"; throw new Exception(3); std: : cout << "BAn"; } catch (int i) { std: : cout << "FCA " << i << "n"; } catch (char c) { std: : cout << "FCB " << c << "n"; throw; } std: : cout << "BCn"; } 29
Handout 7 d) Change your code so that it works with objects created on the heap by using the new operator. Discuss the positive and negative aspects of such an object-reference based approach for exception handling. void main() { try { std: : cout << "An"; fun(); std: : cout << "Bn"; } catch (int i) { std: : cout << "C " << i << "n"; } catch (double d) { std: : cout << "D " << d << "n"; } catch (Exception *ex) { std: : cout << "Exception " << ex->Int_Error. Code << "n"; delete ex; } catch (. . . ) { std: : cout << "En"; } std: : cout << "Fn"; } 30
Thank you
- Slides: 31