![]() ![]() ![]() ![]() | Four different policy classes - with type parameterized static methods. | Lecture 5 - slide 9 : 39 Program 7 |
// Four different Point policies - the static methods are now specific to class Point. #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())); } };