/////////////////////////////////////////////////////////////////////// // NamedType.cpp - Defines Strong Types // // // // Jim Fawcett, CSE687 - Object Oriented Design, Spring 2018 // /////////////////////////////////////////////////////////////////////// #include "NamedType.h" #include using Seconds = NamedType; using MilliSeconds = NamedType; Seconds operator+(const Seconds& lhs, const Seconds& rhs) { return Seconds(lhs.get() + rhs.get()); } Seconds operator-(const Seconds& lhs, const Seconds& rhs) { return Seconds(lhs.get() - rhs.get()); } Seconds toSeconds(const MilliSeconds& ms) { return Seconds(ms.get()/1000.0); } MilliSeconds toMilliSeconds(const Seconds& s) { return MilliSeconds(1000.0*s.get()); } void acceptsOnlyDoubles(double d) { std::cout << "\n acceptOnlyDoubles accepted " << d; } int main() { std::cout << "\n Demonstrating Strong Types"; std::cout << "\n ============================\n"; std::cout << "\n demonstrating NamedType"; std::cout << "\n --------------------------------------"; Seconds longTime(3.5); std::cout << "\n longTime = " << longTime << " Seconds"; Seconds longerTime(10.5); std::cout << "\n longerTime = " << longerTime << " Seconds"; MilliSeconds shortTime(2.8); std::cout << "\n shortTime = " << shortTime << " MilliSeconds"; Seconds shortTimeInSecs = toSeconds(shortTime); std::cout << "\n toSeconds(shortTime) = " << shortTimeInSecs << " Seconds"; //std::cout << "\n shortTime = " << toSeconds(shortTime) << " Seconds"; // compile failure ?? //std::cout << "\n " << typeid(toSeconds(shortTime)).name(); //std::cout << "\n " << typeid(shortTimeInSecs).name(); Seconds sumSec = longTime + longerTime; std::cout << "\n sumSec = longTime + longerTime = " << sumSec << " Seconds"; //sumSec = sumSec + shortTime; sumSec = sumSec + toSeconds(shortTime); std::cout << "\n sumSec = sumSec + toSeconds(shortTime) = " << sumSec << " Seconds"; MilliSeconds sumSecInMilliSec = toMilliSeconds(sumSec); std::cout << "\n toMilliSeconds(sumSec) = " << sumSecInMilliSec << " MilliSeconds"; std::cout << "\n"; std::cout << "\n calling acceptsOnlyDoubles(sumSec.get())"; acceptsOnlyDoubles(sumSec.get()); std::cout << "\n"; std::cout << "\n demonstrating AddableNamedType"; std::cout << "\n ---------------------------------------------"; Meter dist1 = Meter(3.0); std::cout << "\n dist1 = " << dist1 << " Meters"; Meter dist2(4.0); std::cout << "\n dist2 = " << dist2 << " Meters"; Meter sum = dist1 + dist2; std::cout << "\n sum = dist1 + dist2 = " << sum << " Meters"; Meter diff = dist1 - dist2; std::cout << "\n diff = dist1 - dist2 = " << diff << " Meters"; // these work too sum = Meter(dist1.get() + dist2.get()); sum = Meter(dist1 + dist2); Yard dist4 = Yard(5.0); std::cout << "\n dist4 = " << dist4 << " Yards"; std::cout << "\n can't combine Meters and Yards"; ///////////////////////////////////////////////////////////////////////// // statements below fail to compile: // dist1 = dist4; // can't assign yard to meter // dist1 = 3.5; // can't assign double to meter // double d = dist1; // can't asign meter to double double d = sum.get(); std::cout << "\n\n"; return 0; }