C++ examples for Class:Constructor
Time class containing a constructor with default arguments.
#include <iostream> #include <iomanip> #include <stdexcept> using namespace std; class Time //from ww w . j av a 2s . c o m { public: Time( int = 0, int = 0, int = 0 ); // default constructor // set functions void setTime( int, int, int ); // set hour, minute, second void setHour( int ); // set hour (after validation) void setMinute( int ); // set minute (after validation) void setSecond( int ); // set second (after validation) // get functions int getHour(); // return hour int getMinute(); // return minute int getSecond(); // return second void printUniversal(); // output time in universal-time format void printStandard(); // output time in standard-time format private: int hour; // 0 - 23 (24-hour clock format) int minute; // 0 - 59 int second; // 0 - 59 }; // end class Time // Time constructor initializes each data member to zero Time ::Time( int hour, int minute, int second ) { setTime( hour, minute, second ); // validate and set time } // set new Time value using universal time void Time::setTime( int h, int m, int s ) { setHour( h ); // set private field hour setMinute( m ); // set private field minute setSecond( s ); // set private field second } // set hour value void Time::setHour( int h ) { if ( h >= 0 && h < 24 ) hour = h; else throw invalid_argument( "hour must be 0-23" ); } // set minute value void Time::setMinute( int m ) { if ( m >= 0 && m < 60 ) minute = m; else throw invalid_argument( "minute must be 0-59" ); } // set second value void Time::setSecond( int s ) { if ( s >= 0 && s < 60 ) second = s; else throw invalid_argument( "second must be 0-59" ); } // return hour value int Time ::getHour() { return hour; } // return minute value int Time ::getMinute() { return minute; } // return second value int Time ::getSecond() { return second; } // print Time in universal-time format (HH:MM:SS) void Time::printUniversal() { cout << setfill( '0' ) << setw( 2 ) << getHour() << ":" << setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond(); } // print Time in standard-time format (HH:MM:SS AM or PM) void Time::printStandard() { cout << ( ( getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << getMinute() << ":" << setw( 2 ) << getSecond() << ( hour < 12 ? " AM" : " PM" ); } int main() { Time t1; // all arguments defaulted Time t2( 2 ); // hour specified; minute and second defaulted Time t3( 21, 34 ); // hour and minute specified; second defaulted Time t4( 12, 25, 42 ); // hour, minute and second specified cout << "Constructed with:\n\nt1: all arguments defaulted\n "; t1.printUniversal(); // 00:00:00 cout << "\n "; t1.printStandard(); // 12:00:00 AM cout << "\n\nt2: hour specified; minute and second defaulted\n "; t2.printUniversal(); // 02:00:00 cout << "\n "; t2.printStandard(); // 2:00:00 AM cout << "\n\nt3: hour and minute specified; second defaulted\n "; t3.printUniversal(); // 21:34:00 cout << "\n "; t3.printStandard(); // 9:34:00 PM cout << "\n\nt4: hour, minute and second specified\n "; t4.printUniversal(); // 12:25:42 cout << "\n "; t4.printStandard(); // 12:25:42 PM // attempt to initialize t6 with invalid values try { Time t5( 27, 74, 99 ); // all bad values specified } catch ( invalid_argument &e ) { cout << "\n\nException while initializing t5: " << e.what() << endl; } }