#include /* quantity in SI unit */ template class Quantity { public: double magnitude; explicit Quantity(double magnitude): magnitude(magnitude) {} }; /* short names for quantities */ typedef Quantity<1, 0, 0> Length; /* hossz, m */ typedef Quantity<2, 0, 0> Area; /* terület, m^2 */ typedef Quantity<0, 1, 0> Mass; /* tömeg, kg */ // or like this using Time = Quantity<0, 0, 1>; /* idő, s */ using Speed = Quantity<1, 0, -1>; /* sebesség, m/s */ using Acceleration = Quantity<1, 0, -2>; /* gyorsulás, m/s^2 */ using Force = Quantity<1, 1, -2>; /* erő, N=m*kg/s^2 */ using Energy = Quantity<2, 1, -2>; /* energia, J=m^2*kg/s^2 */ using Power = Quantity<2, 1, -3>; /* teljesítmény, Watt = Joule/s = m^2*kg/s^3 */ /* operators for quantities */ template Quantity operator+(Quantity a, Quantity b) { return Quantity(a.magnitude + b.magnitude); } template Quantity operator-(Quantity a, Quantity b) { return Quantity(a.magnitude - b.magnitude); } template Quantity operator*(Quantity a, Quantity b) { return Quantity(a.magnitude * b.magnitude); } template Quantity operator/(Quantity a, Quantity b) { return Quantity(a.magnitude / b.magnitude); } template bool operator<(Quantity a, Quantity b) { return a.magnitude < b.magnitude; } /* generic stream inserter operator for quantities */ template std::ostream & operator<<(std::ostream & os, Quantity m) { os << m.magnitude << ' '; bool elso = true; if (M != 0) { elso = false; os << "m^" << M; } if (KG != 0) { if (!elso) os << '*'; elso = false; os << "kg^" << KG; } if (S != 0) { if (!elso) os << '*'; elso = false; os << "s^" << S; } return os; } /* user-defined literals for SI quantities */ Length operator "" _m (long double magnitude) { return Length(magnitude); } Length operator "" _km (long double magnitude) { return Length(magnitude * 1000.0); } Time operator "" _s (long double magnitude) { return Time(magnitude); } Time operator "" _h (long double magnitude) { return Time(magnitude * 3600); } Mass operator "" _kg (long double magnitude) { return Mass(magnitude); } int main() { /*Egy 1450kg össztömegű verseny-személygépjármű 4s alatt gyorsul 100km/h-ra. Mennyi ez a sebesség SI-ben? Állandó gyorsulást feltételezve mekkora a gyorsító erő? */ std::cout << "Ferrari ===" << std::endl; Speed v = 100.0_km / 1.0_h; std::cout << "100 km/h = " << v << std::endl; Acceleration a = v / 4.0_s; Force f = 1450.0_kg * a; /* Newton */ std::cout << "F = " << f << std::endl; }