// class to represent and manipulate a velocity, // which can be accessed in kilometres per hour // or miles per hour class Velocity { public: // the conversion factor from miles to km static const float KMperMile = 1.60934; // constructor with optional initial value // and flag indicating if it is being provided // in mph (default) or kmh Velocity(float v = 0, bool inMph = true) { setVel(v, inMph); } // lookup function allowing user to retrieve velocity in either // mph (the default) or kmh float getVel(bool inMph = true) { if (inMph) return velocity; else return KMperMile * velocity; } // set function allowing user to set velocity in either // mph (the default) or kmh void setVel(float v, bool inMph = true) { if (inMph) velocity = v; else velocity = v / KMperMile; } private: float velocity; // velocity, stored in miles per hour }; |