1) [30 points] In this problem, you will implement the design of a C template class Triplethat can be used to represent triples of a specified type, e.g. a triple of integers like (10,-40, 200) where...

1) [30 points] In this problem, you will implement the design of a C template class Triple






that can be used to represent triples of a specified type, e.g. a triple of integers like (10,






-40, 200) where 10, -40, and 200 are the first, second and third elements in the triple,






respectively.






The interface (header) file defining Triple has been provided. Write the implementation






in a file called triple.cpp and make sure it works with the driver client1.cpp.






At the end of the implementation file, you will may need to do an ‘explicit instantiation’






of the template for types that are used in the driver as shown in Listing 1.










Listing 1: Explicit Instantiation






// explicit instantiation






template class Triple ;










Write your code so as to the same output on running the executable generated by client1.cpp






as shown in Listing 2.










Note: A makefile is provided just to help you while developing/testing/debugging your






code for this problem.










Listing 2: Sample interaction of the driver in client1.cpp with a correct implementation






of Triple in triple.cpp.










$ ./ client1 . out






var1 : (11 , 1500 , 780000)






var1 's third element : 780000






var2 : (t , o , n )






var2 's first element : t






var3 : (0.44 , 1.567 , -89.4325)






var3 's second element : 1.567






var4 : ( go , away , corona )






unnamed : ( -10 , 20 , 30)










In readme.txt clearly describe how to compile and run your code on Polaris.










2. [30 points] You have been provided an interface file containing the declaration of a function safeadd and a client file that invokes safeadd. As shown in the interface file, safeadd has the following signature:










// precondition : n1 >= 0, n2 >= 0






int safeadd (int n1 , int n2 ) ;










In safesum.cpp, write the definition of function safeadd. The implementation should






protect against overflow by raising an std::overflow_error exception if the sum is going






to be more than INT_MAX, otherwise it returns the sum of its arguments.






Note: INT_MAX is defined in the C header file .










Study the sample client driver5.cpp and observe that it can handle the exception generated by safeadd. The result of running the client code in driver5.cpp using a correct






implementation of safeadd is shown in Figure 1.






Note: Note that std::runtime_error is a superclass of overflow_error.






In readme.txt, briefly describe how to compile and run your code on Polaris. In the same






file, briefly explain the difference in behavior for the two exceptional situations shown in






Figure 1 with respect to the control flow and the output obtained.










Figure 1: Expected output when using the driver in driver5.cpp.










$ g -c safesum.cpp






$ g -c driver5.cpp






$ g -o driver5.out driver5.o safesum.o






$ ./driver5.out






INT_MAX (on this machine): 2147483647






safeadd(7, 500): 507






safeadd(2147483547, 200):






main: Caught exception at runtime: safeadd: overflow during integer addition.






safeadd(300, 11): 311






calling safeadd(2147483547, 200): terminate called after






throwing an instance of ’std::overflow_error’






what(): safeadd: overflow during integer addition.






Aborted










In readme.txt clearly describe how to compile and run your code on Polaris.
Dec 07, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here