0% found this document useful (0 votes)
64 views

Conversion Constructor

The document defines two classes, time24 and time12, to represent times in 24-hour and 12-hour format respectively. The time24 class stores hours, minutes, and seconds while time12 stores hours, minutes, and an indicator for AM/PM. A time12 object can be constructed from a time24 object by converting the 24-hour time to 12-hour format taking into account AM/PM. The main function demonstrates constructing and displaying objects of both classes and converting between their formats.

Uploaded by

Abdul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Conversion Constructor

The document defines two classes, time24 and time12, to represent times in 24-hour and 12-hour format respectively. The time24 class stores hours, minutes, and seconds while time12 stores hours, minutes, and an indicator for AM/PM. A time12 object can be constructed from a time24 object by converting the 24-hour time to 12-hour format taking into account AM/PM. The main function demonstrates constructing and displaying objects of both classes and converting between their formats.

Uploaded by

Abdul Haq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

//military format Civilian Format

//9:12:33 9:12 AM
//21:12:33 9:12 PM
#include <iostream>
#include <string>
using namespace std;
//////////////////////
class time24
{
private:
int hours;
int minutes;
int seconds;
public:
time24():hours(0), minutes(0), seconds(0){} //no-arg constructor
time24(int h, int m, int s):hours(h), minutes(m), seconds(s){}
void display()const
{
if (hours < 10)cout<<'0'; cout<<hours<<':';
if (minutes < 10)cout<<'0'; cout<<minutes<<':';
if (seconds < 10)cout<<'0'; cout<<seconds<<endl;
}
int getHours(){return hours;}
int getMinutes(){return minutes;}
int getSeconds(){return seconds;}
}; //end of time24 class
//////////////////////////////////////////////////
class time12
{
private:
int hours;
int minutes;
bool pm;
public:
time12():hours(0), minutes(0), pm(true){} // no -arg constructor
time12(int h, int m, bool ap):hours(h), minutes(m), pm(ap){} //3-arg
constructor
time12(time24 t24)
{
int hrs24 = t24.getHours();
if (hrs24 < 12)
pm = false;
else
pm = true;
if (t24.getSeconds() < 30)
minutes = t24.getMinutes();
else
minutes = t24.getMinutes() + 1;
if (minutes == 60)
{
minutes = 0;
hrs24++;
}
if (hrs24 == 12 || hrs24 == 24) //toggle am, pm
{
if (pm == true)
pm = false;
else
pm = true;
}
if (hrs24 < 13)
hours = hrs24;
else
hours = hrs24 - 12;
if (hours == 0)
{
hours = 12;
pm = false;
}
}// end of 1-argconstructor
void display() const
{
cout<<hours<<":";
if (minutes < 10)cout<<'0';cout<<minutes<<' ';
string am_pm;
if (pm == true)
am_pm = "P.M";
else
am_pm = "A.M";
cout<<am_pm<<endl;
}
};
//////////////////////////////////////////////////////////
int main()
{
time12 t12, t11;
time24 t24(9, 12, false), t224(23, 40, 50);
cout<<"t12 = "; t12.display();cout<<endl;
cout<<"t11 = "; t11.display();cout<<endl;
cout<<"t24 = "; t24.display();cout<<endl;
cout<<"t224 = "; t224.display();cout<<endl;
cout<<"*****After Conversion ****** \n";
t12 = t24;
t11 = t224;
cout<<"t12 = "; t12.display();cout<<endl;
cout<<"t11 = "; t11.display();cout<<endl;
return 0;
}

You might also like