Function overloading

This lesson contains approximately 13 minutes of video content.

Defining overloaded functions

Calling an overloaded function

Overloading guidance

Activity: Overloading to military time

Graded Playground Autograder

Activity Prompt:
Background

For some reason, the U.S. military has been getting their time as the number of hours (double) or as the number of minutes (unsigned int) as the time elapsed from 00:00. Your task as their software engineer is to convert these measurements to a more readable format. More specifically, your task is to implement an overloaded function to convert either of these inputs to military time. The military time displays the hour count (0-23 inclusive) followed by the minute count (0-59 inclusive). If either the hour count or minute count is less than 10 (i.e., a single digit), you will need to prepend a 0 to it in your final answer.

Assignment Prompt

You are tasked to write the function ToMilitaryTime, which converts a time measurement to the time in the day (24 hours). The measurements can be made in minutes passed as an unsigned int or in hours passed as a double. You should return the military time in the format hh:mm. If your minute and/or hour count is less than 10, you must prepend a 0 (i.e., 14:9 should be 14:09) as if reading the time on a digital clock displaying military time. For example, ToMilitaryTime(8.4) -> "08:24" and ToMilitaryTime(389u) -> "06:29". When computing a time's minute component, round to the nearest minute since fractional minutes cannot be reported. You must do this using round() function(#include <cmath>). Since round() returns a double and you need an std::string, use std::to_string. Finally, if you are given input that falls out of the range of military time (i.e., an hour or minute count is less than 0 OR hour count is > 24.0 OR minute count > 1440), you will need to throw an exception.

Functions to Implement
std::string ToMilitaryTime(unsigned int num_mins);
  // Example: ToMilitaryTime(389u) => "06:29" // notice that the argument must be an unsigned int.
  // Example: ToMilitaryTime(844u) => "14:04"
  // Example: ToMilitaryTime(1440u) => "00:00"
      
std::string ToMilitaryTime(double num_hours); 
  // Example: ToMilitaryTime(8.4) => "08:24"
  // Example: ToMilitaryTime(14.4432) => "14:27"    
  // Example: ToMilitaryTime(24.0) => "00:00"
#include <iostream> #include "military_time.hpp" int main() { std::cout << ToMilitaryTime(45u) << std::endl; }
#ifndef MILITARY_TIME_HPP #define MILITARY_TIME_HPP #include <string> // std::string ToMilitaryTime(double hours); // std::string ToMilitaryTime(unsigned int minutes); #endif
#include "military_time.hpp"