Module 10: Templates in C++
- First read this page then start the module with the GitHub classroom link below.
- Github Classroom Link: https://classroom.github.com/a/GJ8yGeu0
Exercise 1: Template Function
In this exercise, you will create a function template in C++ that calculates the sum of elements in an array. The function should be able to handle arrays of any data type that supports addition and initialization to zero.
- Setup the environment: Review the code in the exercise1 directory in the module10 GitHub repository.
- Create a function template: In the file
exercise1.cpp
modify thearraySum
function to be a template function that can sum generic types instead of onlyint
. - Update the main function: Modify the main function to have multiple calls to the
arraySum
function with different types, such asdouble
,float
,long
. - Compile and test: Compile and run
exercise1.cpp
to test your new template function.
Exercise 2: Template Class
In this exercise, you will create a generic stack data structure using C++ templates. The stack should be able to store elements of any data type and provide basic stack operations.
Steps:
- Setting Up the Environment: Add your code to the exercise2 directory in the module10 GitHub repository.
- Creating the Stack Header File: Create a new file named
StackTemplate.h.
Define a class template namedStackTemplate
. Inside the class, declare aprivate
member variable that will hold the stack elements. Usestd::vector<T>
as the underlying container. Use the following template declaration:template <typename T> class StackTemplate { public: // Class definition goes here private: std::vector<T> elements; };
- Adding Basic Methods: Add the following public methods to the
StackTemplate
class:bool isEmpty();
- To check if the stack is empty.void push(T element);
- To add an element to the top of the stack.T pop();
- To remove and return the top element of the stack. Throwout_of_range
exception if stack is empty.T top();
- To get the top element without removing it. Throwout_of_range
exception if stack is empty.
- Implementing the Stack Methods: Create a new file named StackTemplate.cpp. Implement each method you declared in the StackTemplate class. For example:
template <typename T> bool StackTemplate<T>::isEmpty() { return elements.empty(); } template <typename T> void StackTemplate<T>::push(T element) { elements.push_back(element); } // Continue implementing the rest of the methods...
- Include the Implementation: At the end of the
StackTemplate.h
file, include the implementation fileStackTemplate.cpp
. - Testing Your Stack: Create a
main.cpp
file. Include theStackTemplate.h
file. In the main function, create aStackTemplate<int>
instance and test the stack operations. For example:int main() { StackTemplate<int> intStack; intStack.push(10); intStack.push(20); // Test other methods and print results return 0; }
- Add try/catch block: Implement exception handling for situations like popping from an empty stack.
- Other data types: Create stacks with other data types (e.g.,
StackTemplate<std::string>
). - Add a method: to return the size of the stack.
- Create a Makefile: Define your targets and dependencies:
- all: This target will build your entire project.
- clean: This target will clean up the object files and the executable.
- main.o: Target for compiling the
main.cpp
file. - main: Target for compiling the
main.cpp
andStackTemplate.cpp
.
- Test your program: Use your make file to compile. Then run your program to test.