Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is ugly, verbose, and generally a dumb way to go. But I figured I'd post a complete code example by way of explanation. For extra points it's actually possible to define a compile-time expanded iteration over the solar planets by tweaking the template specializations just a tad.</p> <pre><code>#include &lt;string&gt; #include &lt;sstream&gt; #include &lt;iostream&gt; #include &lt;cstdlib&gt; class Planet { public: static const double G = 6.67300E-11; Planet(const ::std::string &amp;name, double mass, double radius) : name_(name), mass_(mass), radius_(radius) {} const ::std::string &amp;name() const { return name_; } double surfaceGravity() const { return G * mass_ / (radius_ * radius_); } double surfaceWeight(double otherMass) const { return otherMass * surfaceGravity(); } private: const ::std::string name_; const double mass_; const double radius_; }; enum SolarPlanets { MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE }; template &lt;SolarPlanets planet&gt; class SolarPlanet : public Planet { }; template &lt;&gt; class SolarPlanet&lt;MERCURY&gt; : public Planet { public: SolarPlanet() : Planet("MERCURY", 3.303e+23, 2.4397e6) {} }; template &lt;&gt; class SolarPlanet&lt;VENUS&gt; : public Planet { public: SolarPlanet() : Planet("VENUS", 4.869e+24, 6.0518e6) {} }; template &lt;&gt; class SolarPlanet&lt;EARTH&gt; : public Planet { public: SolarPlanet() : Planet("EARTH", 5.976e+24, 6.37814e6) {} }; template &lt;&gt; class SolarPlanet&lt;MARS&gt; : public Planet { public: SolarPlanet() : Planet("MARS", 6.421e+23, 3.3972e6) {} }; template &lt;&gt; class SolarPlanet&lt;JUPITER&gt; : public Planet { public: SolarPlanet() : Planet("JUPITER", 1.9e+27, 7.1492e7 ) {} }; template &lt;&gt; class SolarPlanet&lt;SATURN&gt; : public Planet { public: SolarPlanet() : Planet("SATURN", 5.688e+26, 6.0268e7) {} }; template &lt;&gt; class SolarPlanet&lt;URANUS&gt; : public Planet { public: SolarPlanet() : Planet("URANUS", 8.686e+25, 2.5559e7) {} }; template &lt;&gt; class SolarPlanet&lt;NEPTUNE&gt; : public Planet { public: SolarPlanet() : Planet("NEPTUNE", 1.024e+26, 2.4746e7) {} }; void printTerranWeightOnPlanet( ::std::ostream &amp;os, double terran_mass, const Planet &amp;p ) { const double mass = terran_mass / SolarPlanet&lt;EARTH&gt;().surfaceGravity(); os &lt;&lt; "Your weight on " &lt;&lt; p.name() &lt;&lt; " is " &lt;&lt; p.surfaceWeight(mass) &lt;&lt; '\n'; } int main(int argc, const char *argv[]) { if (argc != 2) { ::std::cerr &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " &lt;earth_weight&gt;\n"; return 1; } const double earthweight = ::std::atof(argv[1]); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;MERCURY&gt;()); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;VENUS&gt;()); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;EARTH&gt;()); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;MARS&gt;()); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;JUPITER&gt;()); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;SATURN&gt;()); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;URANUS&gt;()); printTerranWeightOnPlanet(::std::cout, earthweight, SolarPlanet&lt;NEPTUNE&gt;()); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload