Question
C++ Part 1: 1- Write the following includes: #include #include using namespace std; 2- Create the following 4 exception classes: a. Class IndexOutOfBoundsException which inherits
C++
Part 1:
1- Write the following includes: #include #include using namespace std; 2- Create the following 4 exception classes: a. Class IndexOutOfBoundsException which inherits from class logic_error Note: logic_error is a class that inherits from class exception, you can use logic_error once you #include as we have already done above. b. Class LowerBoundException which inherits from class IndexOutOfBoundsException c. Class UpperBoundException which inherits from class IndexOutOfBoundsException d. Class UninitializedException which inherits from class logic_error 3- Make the constructor of class IndexOutOfBoundsException takes (const char* msg) as a formal parameter and use the initializer list to send the msg parameter to the constructor of the logic_error class. 4- Create a zero-argument constructor for class LowerBoundException and use the initializer list to send this string "index lower bound exception" message to the constructor of the IndexOutOfBoundsException class. 5- Create a zero-argument constructor for class UpperBoundException and use the initializer list to send this string "index upper bound exception" message to the constructor of the IndexOutOfBoundsException class. 6- Create a zero-argument constructor for class UninitializedException and use the initializer list to send this string "uninitialized element exception" message to the constructor of the logic_error class.
Part 2:
1- Create a new class called MyList class that represents a list of integers. 2- At the beginning of the source file and under the includes, add the following line: #define size 10 This is a way to define a constant size equals 10, so whenever you use size in your code, this will be replaced internally by 10. We will use this size as the number of elements, so we will never write 10 directly in the code, instead we will use size. 3- Write the following private data members inside the MyList class: a. Add an integer array called arr of size elements. This array will contain the elements of the list. b. Add a boolean array called initialized of size elements. This array tells whether the element of the corresponding index is initialized or not. For example, if arr[3] is initialized, then the initialized[3] will equal true, if not, then it will equal false.
4- Write the following public functions inside the MyList class: a. A constructor to initialize all the elements of the initialized array to false. (Question: is there a simpler way for this initialization?) b. Write a function called setElement that takes two parameters, index and value, and does the following: i. If the index is less than 0, then throw LowerBoundException ii. If the index is greater than or equal size, then throw UpperBoundException
iii. Otherwise, update the two data members appropriately to initialize the element at the passed index with the passed value.
c. Write a function called getElement that takes one parameter, index, and does the following: i. If the index is less than 0, then throw LowerBoundException ii. If the index is greater than or equal size, then throw UpperBoundException iii. If the array element of the passed index is not initialized, then throw UninitializedException iv. Otherwise, return the array element of the passed index. d. Write a function called print that prints the array elements space-separated such that if an element is initialized, then its value is printed, if not initialized, then the letter U should be printed instead. After printing all elements, print a new line. Example of the print output: U 100 U U 200 U U 500 U 900
Part 3:
Write the main function that does the following: 1- Inside the try block, do the following: a. Create an object of the MyList class called list. b. Set the element at index 5 with value 100 c. Set the element at index 1 with value 200 d. Set the element at index 9 with value 500 e. Print the list f. In a while loop, prompt the user to enter an index, then print the list element of that index using the getElement function. When the user enters -1, break out of the loop. 2- Add the needed catch blocks, such that if any kind of exception occurred, its message is printed on the screen. 3- After all the catch blocks (outside them), add one cout with the following message the last line before return 0; 4- Run the program and try entering different indices and notice how the program reacts
5- Question: what are the needed modifications to make the while loop continues even if an exception is thrown?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started