Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          templates/vers4-f14/point.cc - The implementation of the template class Point - point.cc.Lecture 5 - slide 34 : 40
Program 2

// The implementation of the Point template class.

#include <cmath>
#include <iostream>
#include "point.h"

// Default contructor
template<typename C, int dim, C def> Point<C, dim, def>::Point() {
  for (int i = 0; i < dim; i++)
    values[i] = def;
}

// The get method access dimension i of the point
template<typename C, int dim, C def> C Point<C, dim, def>::get(int i) const{
  if (i >= 1 && i <= dim)
    return values[i-1];
  else 
    throw PointDimProblem();
}

// Move the point by means of a vector of int.
template<typename C, int dim, C def> Point<C, dim, def>&    // deltas is a vector<int>
                          Point<C, dim, def>::move(const std::vector<int>& deltas){  
  for (int i = 0; i < dim; i++)                                               
    values[i] += deltas[i];
  return *this;
}

// Distance to method - Using some funny norm:
template<typename C, int dim, C def> double Point<C, dim, def>::distance_to(Point p){    
  double temp = 0.0;
  for (int i = 0; i < dim; i++)
    temp += std::abs(values[i] - p.values[i]);

  return temp;
}


// The << operator for points.
template<typename C, int dim, C def> std::ostream& operator<<
                          (std::ostream& s, const Point<C, dim, def>& p){
  s << "(";
  for(int i = 0; i < dim-1; i++)
    s << p.values[i] << ", ";
  s << p.values[dim-1];
  s << ")" << std::endl;

  return s;
}