Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Programming Standard Template Library Homework 5,6,7.

Similar presentations


Presentation on theme: "C++ Programming Standard Template Library Homework 5,6,7."— Presentation transcript:

1 C++ Programming Standard Template Library Homework 5,6,7

2 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

3 [ practice 1 Standard Template Library, List ]

4 [ explain 1 Standard Template Library, List ]

5 [ practice 2 Standard Template Library, Deque ]

6 [ explain 2 Standard Template Library, Deque ]

7 [ practice 3 Standard Template Library, Iterator ]

8 [ explain 3 Standard Template Library, Iterator ]

9 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 )

10 [ practice 4 Standard Template Library, Map ]

11 [ explain 4 Standard Template Library, Map ]

12 Handout 5 Exercise 1: Class and object definition
a) Define a class Student with the following attributes: Name : char * Number : integer Courses: Course * 11/2014

13 Handout 5 b) Make each of the above attributes private and define for each attribute a pair of public get-set-methods 11/2014

14 Handout 5 c) Create a constructor-method for your Student-class for initializing objects of the Student-Class. 11/2014

15 d) Create a class Course with following attributes: Id: integer
Handout 5 d) Create a class Course with following attributes: Id: integer Instructor: char * RoomNr: integer 11/2014

16 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. 11/2014

17 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. 11/2014

18 Handout 6 class A { public : int x; A *objARef; private : int y;
Exercise 1: Given the following C++ code: class A { public : int x; A *objARef; private : int y; protected : int z; }; class B : public A { public : A objA; class C { B objB; Determine for each of the following attribute-access-expressions whether it results in an Error (Wrong) or not (OK). 11/2014

19 Handout 6 objA.x objA.y objA.z objARef->x objARef->y
in class A in class B in class C x OK ■ Wrong  OK  Wrong ■ y z objA.x objA.y objA.z objARef->x objARef->y objARef->z objB.x objB.y objB.z 11/2014

20 Handout 6 Exercise 2: Given the following class hierarchy:
1. Create C++ code without attributes and methods for all 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{ 11/2014

21 Handout 6 class Character : public Object{ public: char ch; };
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. Character operator + (const Character &rCharacter) { Character Char_Ret; Char_Ret.ch = (ch + rCharacter.ch) % 128; // ASCII return Char_Ret; } 11/2014

22 Handout 6 class Digit : public Character{ public:
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’ // = 11 % 10 = 1) class Digit : public Character{ public: Digit operator + (const Digit &rDigit) { Digit Dig_Ret; Dig_Ret.ch = (((ch - '0') + (rDigit.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; 11/2014

23 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 &rCharacter) { Character Char_Ret; Char_Ret.ch = (ch + rCharacter.ch) % 128; return Char_Ret; } char GetCh() { return ch; void SetCh(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]; } 11/2014

24 Handout 7 Exercise 1: Given the following C++ code: 11/2014
#include <iostream> void fun() { try { std::cout << "FA\n"; throw 3; // line 5 std::cout << "BA\n"; } catch (int i) { std::cout << "FCA " << i << "\n"; catch (char c) { std::cout << "FCB " << c << "\n"; throw; std::cout << "BC\n"; void main() { std::cout << "A\n"; fun(); std::cout << "B\n"; std::cout << "C " << i << "\n"; catch (double d) { std::cout << "D " << d << "\n"; catch (...) { std::cout << "E\n"; std::cout << "F\n"; 11/2014

25 1a. What is the output of the above code?
Handout 7 1a. What is the output of the above code? 11/2014

26 Handout 7 1b) 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; 11/2014

27 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_ErrorCode = Int_Code; } int Int_ErrorCode; }; b) Change the throw statement in line 5 so that it throws objects of your exception class. void fun() { try { std::cout << "FA\n"; throw Exception(3); // line 5 std::cout << "BA\n"; catch (int i) { std::cout << "FCA " << i << "\n"; catch (char c) { std::cout << "FCB " << c << "\n"; throw; std::cout << "BC\n"; 11/2014

28 Handout 7 c) Add a catch block in main for catching exception-objects of your exception class. void main() { try { std::cout << "A\n"; fun(); std::cout << "B\n"; } catch (int i) { std::cout << "C " << i << "\n"; catch (double d) { std::cout << "D " << d << "\n"; catch (Exception ex) { std::cout << "Exception " << ex.Int_ErrorCode << "\n"; catch (...) { std::cout << "E\n"; std::cout << "F\n"; 11/2014

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. #include <iostream> class Exception { public: Exception(int Int_Code) { Int_ErrorCode = Int_Code; } int Int_ErrorCode; }; void fun() { try { std::cout << "FA\n"; throw new Exception(3); std::cout << "BA\n"; catch (int i) { std::cout << "FCA " << i << "\n"; catch (char c) { std::cout << "FCB " << c << "\n"; throw; std::cout << "BC\n"; 11/2014

30 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 << "A\n"; fun(); std::cout << "B\n"; } catch (int i) { std::cout << "C " << i << "\n"; catch (double d) { std::cout << "D " << d << "\n"; catch (Exception *ex) { std::cout << "Exception " << ex->Int_ErrorCode << "\n"; delete ex; catch (...) { std::cout << "E\n"; std::cout << "F\n"; 11/2014

31


Download ppt "C++ Programming Standard Template Library Homework 5,6,7."

Similar presentations


Ads by Google