<<<<<<< HEAD Unit 10: Templates (CS 235/CS 250) - R.W.'s Courses

Unit 10: Templates

  1. asdfasdf
  2. asdfasdf
  3. asdfasdf
  4. asdfasdf
  5. asdfasdf
  6. asdfasdf
  7. asdfasdf
  8. asdfasdf
  9. asdfasdf
  10. asdfasdf

  11. Example code and additional resources

This week's stuff:

asdfasdf

asdfasdf

asdfasdf

asdfasdf

asdfasdf

asdfasdf

asdfasdf

asdfasdf

asdfasdf

asdfasdf

Example code and additional resources

Example code: (repository)

  1. Templates:
  2. Templates: Templated array
  3. Templates: Multiple template types

Archived videos and class lectures:

======= Unit 10: Templates (CS 235/CS 250) - R.W.'s Courses

Unit 10: Templates

  1. What are templates?
  2. Templated functions 1
  3. Templated functions 2
  4. Templated classes 1
  5. Templated classes 2

  6. Example code and additional resources

This week's stuff:

What are templates?

With C++ and other languages like C# and Java, we can now use Templates with our functions and classes. A Template allows us to specify a placeholder for a data type which will be filled in later.

In the C++ Standard Template Library, there are objects like the vector that is essentially a dynamic array, but it can store any data type - we just have to tell it what it's storing when we declare a vector object:


vector<int> listOfQuantities;
vector<float> listOfPrices;
vector<string> listOfNames;
	    

We can also define our own functions and even classes with templated functions and member variables ourselves, leading to much more reusable code.

Templated functions

We can write a standalone function with templated parameters or a templated return type or both. For example, here's a simple function to add two items together:

template <typename T>
T Sum( T numA, T numB )
{
    return numA + numB;
}
	

This function can be called with any data type, so long as the data type has the + operator defined for it - so, if it were a custom class you wrote, you would have to overload the operator+ function.

What this means is that we can call Sum with integers and floats, but also with someting like a string, since strings use the + operator to combine two strings together.

Templated functions

Function definition:

template <typename T>
T Sum( T numA, T numB )
{
    return numA + numB;
}
	

Calling the templated function:

template <typename T>
int main()
{
    int intA = 4, intB = 6;
    float floatA = 3.9, floatB = 2.5;
    string strA = "alpha", strB = "bet";

    cout << intA << " + " << intB
        << " = " << Sum( intA, intB ) << endl;

    cout << floatA << " + " << floatB
        << " = " << Sum( floatA, floatB ) << endl;

    cout << strA << " + " << strB
        << " = " << Sum( strA, strB ) << endl;
}
	

Templated classes

More frequently, you will be using templates to create classes for data structures that can store any kind of data. The C++ Standard Template Library has data structures like vector, list, and map, but we can also write our own.
When creating our templated class, there are a few things to keep in mind:

  1. We need to use template <typename T> at the beginning of the class declaration.
  2. Method definitions MUST be in the header file - in this case, we won't be putting the method definitions in a separate .cpp file. You can either define the functions inside the class declaration, or immediately after it.
  3. Method definitions also need to be prefixed with template <typename T>.

If you try to create a "TemplatedArray.hpp" file and a "TemplatedArray.cpp" file and put your method definitions in the .cpp file, then you're going to get compile errors.

More info: https://isocpp.org/wiki/faq/templates%5C#templates-defn-vs-decl

Templated classes

Example templated class declaration:

template <typename T>
class TemplatedThing
{
  public:
  void SetData( T new_data );
  T GetData() const;

  private:
  T data;
};

Defining templated class' functions:

template <typename T>
void TemplatedThing<T>::SetData( T new_data )
{
  this->data = new_data;
}

template <typename T>
T TemplatedThing<T>::GetData() const;
{
  return this->data;
}

Declaring a templated class object:

TemplatedThing<int> int_thing;
TemplatedThing<float> float_thing;

Example code and additional resources

Spring 2024 lecture:

Example code: https://youtu.be/x7WYZX3cxIM


Example code: (repository)

  1. Templates: Templated array
  2. Templates: Multiple template types

Archived videos and class lectures:

>>>>>>> 1b3da755f69f89a8787c7d747b10904acabef938