Created
January 8, 2023 21:19
-
-
Save EteimZ/2be0f1fa7a86b1d04111c3c691d35b57 to your computer and use it in GitHub Desktop.
Example using default constructors and getters and setters in cpp. Example is gotten from the book C How to Program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using std::cout; | |
| using std::endl; | |
| #include "Time.h" | |
| int main(){ | |
| Time t1; // uses default arguments | |
| Time t2( 2 ); // specified hour | |
| Time t3( 21, 34 ); // specified hour and minute | |
| Time t4( 12, 23, 43); // all arguments specified | |
| Time t5( 27, 87, 98); // invalid arguments | |
| cout << "Constructed with \n\nt1: using default arguments\n "; | |
| t1.printUniversal(); // 00:00:00 | |
| cout << "\n "; | |
| t1.printStandard(); // 12:00:00 AM | |
| cout << "\n\nt2: specified hour\n "; | |
| t2.printUniversal(); // 02:00:00 | |
| cout << "\n "; | |
| t2.printStandard(); // 2:00:00 AM | |
| cout << "\n\nt3: specified hour and minute\n "; | |
| t3.printUniversal(); // 21:34:00 | |
| cout << "\n "; | |
| t3.printStandard(); // 9:30:00 PM | |
| cout << "\n\nt4: specified hour, minute and second\n "; | |
| t4.printUniversal(); // 12:23:43 | |
| cout << "\n "; | |
| t4.printStandard(); // 12:23:43 PM | |
| cout << "\n\nt5: Invalid arguments\n "; | |
| t5.printUniversal(); // 00:00:00 | |
| cout << "\n "; | |
| t5.printStandard(); // 12:00:00 AM | |
| cout << endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Member-function definition for class time | |
| #include <iostream> | |
| using std::cout; | |
| #include <iomanip> | |
| using std::setfill; | |
| using std::setw; | |
| #include "Time.h" | |
| // constructor initializes each data member to zero | |
| Time::Time(int hr, int min, int sec ){ | |
| setTime( hr, min, sec ); | |
| } | |
| void Time::setTime( int hr, int min, int sec ){ | |
| setHour( hr ); | |
| setMinute( min ); | |
| setSecond( sec ); | |
| } | |
| // set hour | |
| void Time::setHour( int h ){ | |
| hour = ( h >= 0 && h < 24 ) ? h : 0; // validate hour | |
| } | |
| // set minute | |
| void Time::setMinute( int m ){ | |
| minute = ( m >= 0 && m < 60 ) ? m : 0; // validate minute | |
| } | |
| // set second | |
| void Time::setSecond( int s ){ | |
| second = ( s >= 0 && s < 60 ) ? s : 0; // validate hour | |
| } | |
| // return hour | |
| int Time::getHour(){ | |
| return hour; | |
| } | |
| // return minute | |
| int Time::getMinute(){ | |
| return minute; | |
| } | |
| // return minute | |
| 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"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Declaration of class Time | |
| #ifndef TIME_H | |
| #define TIME_H | |
| // prevent multiple inclusion | |
| class Time{ | |
| 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 | |
| }; | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment