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:

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 types of inputs to military time. Put simply, 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 writing a program to convert a time measurement (can be minutes provided as integers or hours provided as doubles) to the time in the day (24 hours). Return the time in the format "hour":"minute". Note that if your minute and/or hour count is less than 10, you will need to prepend a 0 (i.e., 14:9 should be 14:09) as if you were reading the time on a digital clock displaying military time.

For example, ToMilitaryTime(8.4) = "08:24" OR ToMilitaryTime(389) = "06:29". When a double (representing the number of hours passed), round the time to the nearest minute using round() (#include <cmath>). Note that the round() function returns a double. How can you convert this to a string? Furthermore, 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(389) => "06:29"
  // Example: ToMilitaryTime(844) => "14:04"
  // Example: ToMilitaryTime(1440) => "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"