| templates/functions/point-norms-and-compare/norms.cc - Four different policy classes - with type parameterized static methods. | Lecture 5 - slide 37 : 40 Program 3 |
// Definition of four different template classes.
// Each defining static member template functions less_than and distance_to.
// Notice that we just assume that getx and gety are public members in C!
// Other designs may be possible - and preferable!
#include <cmath>
struct FixedNorm{
template<typename C> static bool less_than(const C& a, const C& b){
return false;
}
template<typename C> static double distance_to(const C& a, const C& b){
return 7.0;
}
};
struct HorizontalNorm{
template<typename C> static bool less_than(const C& a, const C& b){
return a.getx() < b.getx();
}
template<typename C> static double distance_to(const C& a, const C& b){
return fabs(a.getx() - b.getx());
}
};
struct VerticalNorm{
template<typename C> static bool less_than(const C& a, const C& b){
return a.gety() < b.gety();
}
template<typename C> static double distance_to(const C& a, const C& b){
return fabs(a.gety() - b.gety());
}
};
template<typename T>double square(T a){
return a*a;
}
struct Norm{
template<typename C> static bool less_than(const C& a, const C& b){
return (a.getx() < b.getx()) && (a.gety() < b.gety());
}
template<typename C> static double distance_to(const C& a, const C& b){
return sqrt(square(a.getx() - b.getx()) + square(a.gety() - b.gety()));
}
};