time24 t( 1, 0, 0, "pm" ) ;
is equivalent to:
time24 t( 13, 0, 0 ) ;
The prototype of the new constructor is:
time24( int h, int m, int s, const string& am_pm ) ;
(Note, the time24 class please see the book of “learn C++ through English and Chinese, you may have it from me)
#include<iostream>
#include<string>
using namespace std;
class time24
{
public:
time24(int h, int m, int s, const std::string& am_pm);
void display();
private:
int hours;
int minutes;
int seconds;
};
time24::time24(int h, int m, int s, const std::string& am_pm)
{
if (am_pm == "am")
{
hours = h;
minutes = m;
seconds = s;
}
else if (am_pm == "pm")
{
hours = h + 12;
minutes = m;
seconds = s;
}
}
void time24::display()
{
cout << "the time is" << hours << "." << minutes << "." << seconds << endl;
}
int main()
{
time24 t1(1, 0, 0, "pm");
t1.display();
}