| templates/functions/point-norms-and-compare/norms.cc - Four different policy classes - with type parameterized static methods. | Lecture 6 - slide 9 : 46 Program 3 |
#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()));
}
};