Homework 3C, Templates and Exceptions.
Short Description:
Do Something
Goals
When you finish this homework, you should have:- Demonstrated that you can implement a templated class.
- Demonstrated that you can use exceptions to handle errors in a class.
- Created a reusable 2D array class that can store different types of data.
Formal Description
Update your dynamic 2D array class from Homework 3B to use templates and exception handling.
Templates
Convert your Array2T class into a templated class. The class should be able to store any appropriate data type.- Remove the DataT type alias from your previous implementation.
- Make the data type a template parameter.
- All methods should work with the templated data type.
- Your class should continue to support all functionality from Homework 3B, including resizing and the required methods for a class that uses dynamic memory.
Array2T<int> a(10, 10);
Array2T<double> b(5, 5);
Array2T<char> c(20, 10);
Exception Handling
Update your class to use exceptions for error handling.- The at function should throw an appropriate exception if the requested row or column is outside the bounds of the array.
- Use the standard
out_of_rangeexception. - The exception should be thrown for both the right-hand and left-hand versions of at.
- Use the standard
- The constructor and Resize function should throw an appropriate exception if the requested dimensions are invalid.
- For example, you may use
invalid_argumentif either dimension is zero.
- For example, you may use
- You should test to see if the return value from new is nullptr, if so, throw a
bad_allocexception.
Your test driver should demonstrate that you can catch and handle exceptions generated by your class. For example:
Array2T a(5,5);
try {
cout << a.at(10,20);
}
catch (const out_of_range &e) {
cout << "Array index out of bounds: "
<< e.what() << endl;
}
Notes
- Since this is a templated class, you will need to place the implementation of your methods where the compiler can see them. The easiest approach is to put the entire implementation in Array2T.h.
- You should continue to use the 2D array implementation from Homework 3B.
- Your [] operators should continue to work without bounds checking.
- Your at functions should perform bounds checking and throw exceptions when necessary.
- You must continue to implement all required methods for a class that uses dynamic memory.
- Remember that your class should work with different data types, not just integers.
- Your code must compile without warnings or errors on prog1 with the following flags: -Wall -Wextra -Wpedantic -Wshadow -Wconversion -std=c++23 -g -O2
- It is highly likely we will use this array in one final graded activity.
Required Files
Turn in a single zip or tar file containing all files required to build your project, including:- Array2T.h
- testDrive.cpp
- Makefile