Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          templates/vers6/prog.cc - A program that illustrate the instantiation of both the general and specialized template classes.Lecture 5 - slide 39 : 40
Program 3

// A program that illustrates the use of both the general Point template class
// and the specialized Point<char> template class.

#include <iostream>
#include <string>
#include "point.cc"           
                              
int main(){
  Point<double> pd1,
                pd2(1,2);

  Point<int>    pi1,
                pi2(3,4);

  Point<char>   pc1,           // Using the template specialization.
                               // The strange default constructor reveals the
                               // use of the template specialization.
                pc2(97, 98);   // Char 97 = 'a'

  pd2.move(1,1);
  pi2.move(1,1);
  pc2.move('r', 's');          // Absolute moving!

  std::cout << "pd2: " << pd2 << std::endl;      // (2,3)
  std::cout << "pi2: " << pi2 << std::endl;      // (4,5)
  std::cout << "pc1: " << pc1 << std::endl;      // (a,z)  !!
  std::cout << "pc2: " << pc2 << std::endl;      // (r,s)

  std::cout << "|pd1 - pd2| = " << pd1.distance_to(pd2) << std::endl;    // 3.60555
  std::cout << "|pi1 - pi2| = " << pi1.distance_to(pi2) << std::endl;    // 6.40312
  std::cout << "|pc1 - pc2| = " << pc1.distance_to(pc2) << std::endl;    // 7

  std::cout << "|pd1 - pi2| = " << pd1.distance_to(pi2) << std::endl;    // Error: No matching function for call 
                                                                         // to 'Point<double>::distance_to(Point<int>&)' 
}