100% Guaranteed Results


Programming in C and C++
$ 24.99
Category:

Description

5/5 – (1 vote)

Assignment 13 – Streams, Multiple Inheritance and Exceptions
• The problems of this assignment must be solved in C or C++ (instruction in each problem).
• Your programs should have the input and output formatting according to the testcaseslisted after the problems.
Problem 13.1 Copy a file (1 point)
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 (2 points)
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 (1 point)
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 ’ ’ 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.4 Multiple inheritance I (1 point)
Problem 13.5 Multiple inheritance II (1 point)
Problem 13.6 Out of range exception (1 point)
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 (1 point)
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 ”. 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 (1 point)
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.
How to submit your solutions
• Your source code should be properly indented and compile with gcc or g++ depending on the problem without any errors or warnings (You can use gcc -Wall -o program program.c or g++ -Wall -o program program.cpp). Insert suitable comments (not on every line …) to explain what your program does.
• Please name the programs according to the suggested filenames (they should match the descriptionof the problem) in Grader. Otherwise you might have problems with the inclusion of header files. Each program must include a comment on the top like the following:
/* CH-230-A
*/
If there are problems (but only then) you can submit the programs by sending mail to

Reviews

There are no reviews yet.

Be the first to review “Programming in C and C++”

Your email address will not be published. Required fields are marked *

Related products