Define and implement a C++ class named TwelveHourTime that converts 12-hour time (including AM/PM) to 24 hour time. Your class’ constructor must accept a string argument and you must work with methods of the string class to determine if the time provided is valid or not. You must define an exception class called InvalidTime which the class TwelveHourTime will throw if the data entered is not valid.
Make your class support the following code:
TwelveHourTime sevenAM( “7:00 AM” );
cout << sevenAM << endl; // converts to twenty four hour time and prints “07:00”
TwelveHourTime eightPM( “8:30 PM” );
cout << eightPM << endl; // converts to twenty four hour time and prints “20:30”
TwelveHourTime elevenPM( “11:30 PM” );
cout << elevenPM << endl; // converts to twenty four hour time and prints “23:30”
TwelveHourTime elevenAM( “11:00 AM” );
cout << elevenAM << endl; // converst to twenty four hour time and prints “11:00”
try
{
TwelveHourTime bad( “asef32422” );
// the previous line should throw an InvalidTime exception
} catch( InvalidTime ) {
cout << “caught error as expected” << endl;
Sample Solution