templates/vers4-f14/prog.cc - A program that illustrate the template instantiation. | Lecture 5 - slide 34 : 40 Program 3 |
// A program that illustrates how to use the parameterized Point template class #include <iostream> #include <string> #include <vector> #include "point.cc" // Inclusion of the .cc (.cpp) file, not just .h file const int Dim = 10; int main(){ using namespace std; Point<long int> pli1; // Default dimension and default default value. Point<int,Dim,7> pi1, pi2; // double is illegal as type parameter in this context, // because non-integer constants are not allowed! Point<char, 4, 65> pc1; Point<> pi3; // All three parameters are defaulted. // Diplacement int vectors: vector<int> displacement1 {1, 2, 3}; vector<int> displacement2; for(int i = 0; i < Dim; i++) displacement2.push_back(i*2); // Move points pli1.move(displacement1); pi1.move(displacement2); pc1.move(displacement2); pi3.move(displacement1); // Print points: cout << "pli1: " << pli1 << std::endl; // (1, 2, 3) cout << "pi1: " << pi1 << std::endl; // (7, 9, 11, 13, 15, 17, 19, 21, 23, 25) cout << "pc1: " << pc1 << std::endl; // (A, C, E, G) cout << "pi3: " << pi3 << std::endl; // (1, 2, 3) cout << "|pi1 - pi2| = " << pi1.distance_to(pi2) << endl; // 90 }