templates/functions/point-norms-and-compare-3/norms.cc - Four different policy classes - with type parameterized static methods. | Lecture 5 - slide 37 : 40 Program 8 |
// Four different Point policies - the static methods are now specific to class Point. // This file does not have any template classes nor template functions. #include <cmath> #include "Point.h" struct FixedNorm{ static bool less_than(const Point& a, const Point& b){ return false; } static double distance_to(const Point& a, const Point& b){ return 7.0; } }; struct HorizontalNorm{ static bool less_than(const Point& a, const Point& b){ return a.getx() < b.getx(); } static double distance_to(const Point& a, const Point& b){ return fabs(a.getx() - b.getx()); } }; struct VerticalNorm{ static bool less_than(const Point& a, const Point& b){ return a.gety() < b.gety(); } static double distance_to(const Point& a, const Point& b){ return fabs(a.gety() - b.gety()); } }; template<typename T>double square(T a){ return a*a; } struct Norm{ static bool less_than(const Point& a, const Point& b){ return (a.getx() < b.getx()) && (a.gety() < b.gety()); } static double distance_to(const Point& a, const Point& b){ return sqrt(square(a.getx() - b.getx()) + square(a.gety() - b.gety())); } };