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. *

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

#include

#include

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;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!