Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ exercises/3-containers/part1/test
exercises/3-containers/part2/test
exercises/4-pointers/pointers
exercises/5-templates/part1/sum
exercises/5-templates/part2/test
exercises/5-templates/part2/main
exercises/6.1-my-array/part1
exercises/6.1-my-array/part2
exercises/6.1-my-array/part3
Expand Down
6 changes: 4 additions & 2 deletions exercises/5-templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ List the files in `part2`:
```bash
$ cd archer2-cpp/exercises/5-templates/part2
$ ls
Makefile complex.cpp complex.hpp test.cpp
Makefile array.hpp main.cpp
```

`complex.cpp` contains a working version of the complex number class. Change the class declaration and definitions to use type templating.
`array.hpp` contains the beginnings of a `FixedSizeArray` class. Update the class declaration and definitions to use type and non-type template parameters. Additionally, add new class member functions as directed by the TODO comments. `main.cpp` provides instructions for trying out and testing your implementation.

As before, you can compile with `make`.
10 changes: 5 additions & 5 deletions exercises/5-templates/part2/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CXXFLAGS = --std=c++17 -I../../include
CXXFLAGS = --std=c++20

test : complex.o test.o
main : main.o
$(CXX) $^ -o $@

run : test
./test
run : main
./main

clean :
rm -rf *.o test
rm -rf *.o main
27 changes: 27 additions & 0 deletions exercises/5-templates/part2/array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef CPPEX_ARRAY_HPP
#define CPPEX_ARRAY_HPP

// TODO: Add template parameters to set the data type and array size at compile time
class FixedSizeArray {

private:
int data[10];

public:

FixedSizeArray() {
for (int i = 0; i < 10; ++i) {
data[i] = 0; // Does this work for all data types?
}
}

// TODO: Overload the [] operator to provide access to the array elements

// TODO: Create function size() that returns the size of the array

// TODO: Create function reduce() that sums all elements of the array

// TODO: Create function doubleInPlace() that doubles each element of the array
};

#endif
52 changes: 0 additions & 52 deletions exercises/5-templates/part2/complex.hpp

This file was deleted.

44 changes: 44 additions & 0 deletions exercises/5-templates/part2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "array.hpp"
#include <iostream>

void print(auto&& arr) {
/* Helper function to print the contents of a FixedSizeArray */
std::cout << "[ " << arr[0];
std::cout << "[ " << arr[0];
for (int i = 1; i < arr.size(); ++i) {
std::cout << ", " << arr[i];
}
std::cout << " ]" << std::endl;
}

int main() {
// TODO: Initialise an empty fixed size array of 5 integers
std::cout << "Array size: " << arr.size() << std::endl;
print(arr);

// TODO: Use a for loop to set the array values. E.g. 1-5
print(arr);

// TODO: Call doubleInPlace() and print the new array
arr.doubleInPlace();
print(arr);

// TODO: Initialise an empty fixed size array of 5 integers with the const specifier
print(constArr);

// TODO: Print the output of calling reduce()

// TODO: Check you get a compile time error if you try to set an element value in the
// const array, or if you call doubleInPlace()

// TODO: Initialise a fixed size array of strings with element values "Hello" and "World!"
print(strArr);

// TODO: Print the output of calling reduce() on strArr

// TODO: Call doubleInPlace(). What happens? Can you explain this?
// EXTENSION: Can you change the behaviour of doubleInPlace for non numeric data types?
// E.g. Just print an error and leave the array unchanged.
// Hint: You may want to search for useful type traits, and look up 'if constexpr'.
print(strArr);
}
120 changes: 0 additions & 120 deletions exercises/5-templates/part2/test.cpp

This file was deleted.

5 changes: 1 addition & 4 deletions lectures/5-templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,5 @@ In your clone of this repository, find the `5-templates` exercise. It contains t
4. Change the `sum()` function to use type templating. How does this change the output?


**Part 2**
**Part 2** - `array.hpp` contains the beginnings of a `FixedSizeArray` class. Add template parameters and complete the tasks in the TODO comments.

`complex.cpp` contains a working version of the complex number class. Change the class declaration and definitions to use type templating.

As before, `test.cpp` holds some basic unit tests and you can compile with `make`.