Problem: Add a one-arg constructor to the code for converting a “time12” object
ID: 3755589 • Letter: P
Question
Problem:
Add a one-arg constructor to the code for converting a “time12” object to a “time24” object and test it in the main() function.
* Notes for this problem *
• To use class “b” in class “a” but class “b” is defined after class “a”, you need to have the declaration of class “b” ( i.e. “class b;”) before class “a”.
• Since there are no seconds in the “time12” class, the seconds will be treated as zero in the conversion.
• Conversion operator should be added to the source class.
• One-arg constructor should be added to the destination class.
Code:
/*
* Two kinds of time case: conversion between objects of different classes
* one-arg constructor in destination object
*/
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
/** military time */
class time24
{
private:
int hours; // 0 - 23
int minutes; // 0 - 59
int seconds; // 0 - 59
public:
time24() : hours(0), minutes(0), seconds(0) {} // no-arg constructor
time24(int h, int m, int s) : hours(h), minutes(m), seconds(s) {} // three-arg constructor
void display()
{
cout << setw(2) << setfill('0') << hours << ':';
cout << setw(2) << setfill('0') << minutes << ':';
cout << setw(2) << setfill('0') << seconds;
}
//operator time12() const; // conversion operator
int gethrs() const {return hours;};
int getmins() const {return minutes;};
int getsecs() const {return seconds;};
};
/** civilian time */
class time12
{
private:
bool pm; // true: pm; false: am
int hrs; // 1 - 12
int mins; // 0 - 59
public:
time12() : pm(true), hrs(0), mins(0) {} // no-arg constructor
time12(bool ap, int h, int m) : pm(ap), hrs(h), mins(m) {} // three-arg constructor
time12(time24); // one-arg constructor
void display()
{
cout << hrs << ':';
cout << setw(2) << setfill('0') << mins << ' ' ;;
string am_pm = pm ? "p.m. " : "a.m. ";
cout << am_pm;
}
};
time12::time12(time24 t24)
{
int hrs24 = t24.gethrs();
pm = (hrs24<12) ? false : true;
mins = t24.getsecs() < 30 ? t24.getmins() : t24.getmins()+1; // round seconds
if (mins == 60) // carry mins
{
mins = 0;
++hrs24;
if (hrs24 == 12 || hrs24 == 24) pm = (pm==true) ? false : true; // carry hrs? yes toggle pm/am
}
hrs = (hrs24 > 13) ? hrs24-12 : hrs24;
if (hrs == 0) {hrs = 12; pm = false;}
}
int main()
{
int h, m, s;
cout << "enter hour (0-23): "; cin >> h;
cout << "enter minutes (0-59): "; cin >> m;
cout << "enter seconds (0-59): "; cin >> s;
time24 t24(h, m, s);
cout << "24-hour time: "; t24.display(); cout << endl;
time12 t12 = t24;
cout << "12-hour time: "; t12.display(); cout << endl;
return 0;
}
Explanation / Answer
include<iostream>
#include<string>
#include<iomanip>
using namespace std;
/** military time */
class time24
{
private:
int hours; // 0 - 23
int minutes; // 0 - 59
int seconds; // 0 - 59
public:
time24() : hours(0), minutes(0), seconds(0) {} // no-arg constructor
time24(int h, int m, int s) : hours(h), minutes(m), seconds(s) {} // three-arg constructor
void display()
{
cout << setw(2) << setfill('0') << hours << ':';
cout << setw(2) << setfill('0') << minutes << ':';
cout << setw(2) << setfill('0') << seconds;
}
int gethrs() const {return hours;};
int getmins() const {return minutes;};
int getsecs() const {return seconds;};
};
/** civilian time */
class time12
{
private:
bool pm; // true: pm; false: am
int hrs; // 1 - 12
int mins; // 0 - 59
public:
time12() : pm(true), hrs(0), mins(0) {} // no-arg constructor
time12(bool ap, int h, int m) : pm(ap), hrs(h), mins(m) {} // three-arg constructor
time12(time24); // one-arg constructor
void display()
{
cout << hrs << ':';
cout << setw(2) << setfill('0') << mins << ' ' ;;
string am_pm = pm ? "p.m. " : "a.m. ";
cout << am_pm;
}
operator time24() //
{
int hour;
if(pm){
hour=hrs+12;
if(hour==24)
hour=12;
}
else{
hour=hrs;
if(hour==12)
hour=0;
}
int minute = mins;
return time24(hour, minute, 0);
}
};
time12::time12(time24 t24)
{
int hrs24 = t24.gethrs();
pm = (hrs24<12) ? false : true;
mins = t24.getsecs() < 30 ? t24.getmins() : t24.getmins()+1; // round seconds
if (mins == 60) // carry mins
{
mins = 0;
++hrs24;
if (hrs24 == 12 || hrs24 == 24) pm = (pm==true) ? false : true; // carry hrs? yes toggle pm/am
}
hrs = (hrs24 > 13) ? hrs24-12 : hrs24;
if (hrs == 0) {hrs = 12; pm = false;}
}
int main()
{
time24 t24(15, 25, 47);
time12 t12;
t12 = t24;
cout << "Military time at: ";
t24.display();
cout << " Equivalent civilian time: ";
t12.display();
time12 tn12(1, 5,15);
time24 tn24;
tn24 = tn12;
cout << " Civilian time at: ";
tn12.display();
cout << " ";
cout << "Equivalent military time: ";
tn24.display();
cout << " ";
return 0;
}
//conversion from 24 to 12 is done using one-arg constructor
//and vice-versa is done by operator defined in source class 12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.