$25
Assignment 13 - Streams, Multiple Inheritance and Exceptions
Presence assignment,
Write a program which reads from the standard input the name of a file (e.g., "input.txt"). Your program should use C++ streams to create a copy of this file with the name as the original file name concatenated with "_copy" and then the original extension (e.g., "input_copy.txt"). You can assume that the input will be valid and the necessary input file has a valid content if existing.
Problem 13.2 Complex class with streams
Adapt and extend your previous source code for working with complex numbers (Complex.h,
Complex.cpp and testComplex.cpp) such that the operators + for adding, - for subtracting, ∗ for multiplying two complex numbers, = for assigning, << and >> for input and output streams are overloaded. Your testing program (testComplex.cpp) should read two complex numbers from the files called in1.txt and in2.txt, then compute their sum, the difference and the product, and print the results on the screen as well as into a file called output.txt. You have the freedom to set the structure of the input files.
You can assume that the input of the testing program will be valid and the necessary input files have a valid content if existing.
Problem 13.3 Concatenate n files into new file
Write a program which reads from the standard input an integer value n, followed by n file names. Your program has to concatenate the content of the n files and write the result into the file called “concatn.txt” using binary read and write. Add a ’\n’ to separate the contents of the different files inside the resulting file. The operation of the concatenation is successful only if all files are existing and their opening was successful.
You can assume that the input will be valid and the necessary input files have a valid content if existing.
Problem 13.6 Out of range exception
Write a program which creates a vector of integers and adds the value 8, 20 times into the vector.
Include the library <vector> and then create a vector object which stores the 20 values from
th above. Then write a try and catch block in which your code should try to access the 21 element from the vector using the at() method. The exception you should catch should be of type out_of_range. In the catch block use cerr to print to the standard error stream the type of the exception by calling the redefined what() method inherited from the exception class.
Problem 13.7 Simple different exceptions
Write a program and a function with an integer parameter which can throw four exception types: a char, an int, a bool, and your own exception class called OwnException derived from exception. If the parameter of the function is 1 then the character ’a’ should be thrown, if it’s 2 then the integer 12 should be thrown, if it’s 3 then the bool value true should be thrown, and in the default case an OwnException with the string ”Default case exception” should be thrown. You should overwrite the what() method for the OwnException class by returning the string ”OwnException\n”. Call the function in the main function in its four versions and catch the corresponding exceptions. In the catch blocks you should print to the standard error stream cerr the string ”Caught in main: ” followed by the value of the thrown exception. In the case of OwnException print the string returned by the what() method.
Problem 13.8 Car exceptions
Write a Garage class that has a Car (i.e., object of a second class) that is having troubles with its Motor (i.e., object of a third class). Use a function-level try block in the Garage class constructor to catch an exception (thrown from the Motor class with the string ”This motor has problems”) when its Car object is initialized. Throw a different exception with the string
”The car in this garage has problems with the motor” from the body of the Garage constructor’s handler and catch it in the main function.