a. The code above is in simplesort.h (there is no simplesort.cpp!)
b. If we tried to compile this code by itself, it is incomplete. What
type will be subtituted for the parameter T?
c. We can use almost any type for T. ALMOST.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include "simplesort.h"
using namespace std;
int main()
{
int a[] = {5,2,3,10,7,1,4,8,9,6};
int n = sizeof(a)/sizeof(int);
simplesort(a, n); // compiler replaces T by int
printArray(a, n);
double b[] = {0.5,0.2,0.3,0.7,0.1,0.4,0.8,0.9,0.6};
int nd = sizeof(b)/sizeof(double);
simplesort(b, nd); // Compiler replaces T by double
printArray(b, nd);
string c[] = {"bb", "aa", "cb", "ca", "ba", "cd", "ab", "ac"};
int ns = sizeof(c)/sizeof(string);
simplesort(c, ns); // Compiler replaces T by string
printArray(c, ns);
return 0;
}
sizeof(a) is 40 (10 int's, each of size 4)
sizeof(int) is 4
sizeof(a)/sizeof(int) is 10 (the number of int's in a)
int main()
{
// declare an empty intArray (of integers)
intArray a;
// Insert numbers at the end
for(int i = 0; i < 10000; i++) {
a.add(i);
}
// Print the items in the Array a
for(int i = 0; i < a.size(); i++) {
cout << a.at(i) << endl;
}
return 0;
}
Our intArray class has int as the type of array elements.
If we wanted a intArray with a different type, we would have to
change the int type to the new different type, but
otherwise there would be no change to the code.
For example we might want a array of LineItem's. So the
substitute for the int data type would be: LineItem.
int main()
{
// declare an empty Array (of integers)
Array<int> a;
// Insert numbers at the end
for(int i = 0; i < 10000; i++) {
a.add(i);
}
// Print the items in the Array a
for(int i = 0; i < a.size(); i++) {
cout << a.at(i) << endl;
}
return 0;
}
int main()
{
// declare an empty Array (of Point's)
Array<Point> a;
int x, y;
srand((unsigned int) time(0)); // Initialize random number generator
// Insert random Point's at the end
for(int i = 0; i < 10000; i++) {
x = rand();
y = rand();
a.add( Point(x,y) );
}
// Print the Points in the Array a
// Assumes operator<< has been overloaded for Poitn
for(int i = 0; i < a.size(); i++) {
cout << a.at(i) << endl;
}
return 0;
}
A template function should be declared and implemented
in a header file, not in a .cpp file.
A template class should be declared and implemented
in a header file, not in a .cpp file. That is, member
functions and constructors for a template class should be
implemented after the class declaration, but in the header
file!
Outside the class declaration, the name of a template class
includes the <T>; e.g., Array<T>
A declaration of a template function should be preceded by the
declaration of the template parameter name; e.g.,
template <class T>
A declaration of a template class should be preceded by the
declaration of the template parameter name; e.g.,
template <class T>
A template function implementation should be preceded by the declaration
of the template parameter name; e.g:
template <class T>
Note this also applies to implementations of member
functions of template classes as well as implementations of
non-member template functions.
So the implementation of a member function outside the class
declaration should use the class scope operator ::
with this class name; e.g., the implementation of add in
Array<T> should begin: