310 likes | 327 Views
C++ Programming. Standard Template Library Homework 5,6,7. Sequence Container Overview. [ practice 1 Standard Template Library, List ]. [ explain 1 Standard Template Library, List ]. [ practice 2 Standard Template Library, Deque ]. [ explain 2 Standard Template Library, Deque ].
E N D
C++ Programming • Standard Template Library • Homework 5,6,7
Handout 5 • Exercise 1: Class and object definition a) Define a class Student with the following attributes: Name : char * Number : integer Courses: Course *
Handout 5 b) Make each of the above attributes private and define for each attribute a pair of public get-set-methods
Handout 5 • c) Create a constructor-method for your Student-class for initializing objects of the Student-Class.
Handout 5 • d) Create a class Course with following attributes: Id: integer Instructor: char * RoomNr: integer
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.
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.
Handout 6 • 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 { public : A objA; A *objARef; B objB; }; Determine for each of the following attribute-access-expressions whether it results in an Error (Wrong) or not (OK).
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{ • };
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. • Character operator + (const Character &rCharacter) { Character Char_Ret; Char_Ret.ch = (ch + rCharacter.ch) % 128; // ASCII return Char_Ret; }
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 &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: staticint Int_Count ; Object() { ++Int_Count; cout << "Current Object created : " << Int_Count << endl; } }; int Object::Int_Count = 0;
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]; }
Handout 7 • Exercise 1: Given the following C++ code: #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() { 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 (...) { std::cout << "E\n"; } std::cout << "F\n"; }
Handout 7 • 1a. What is the output of the above code?
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;
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"; }
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"; }
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"; thrownew 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"; }
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"; }