Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip> //for setw()
- using namespace std;
- ////////////////////////////////////////////////////////////////
- class Time
- {
- private:
- int hrs, mins, secs;
- public:
- Time() //no-arg constructor
- {
- hrs = mins = secs = 0;
- }
- //--------------------------------------------------------------
- Time(int h, int m, int s) //3-arg constructor
- {
- hrs = h; mins = m; secs = s;
- }
- //--------------------------------------------------------------
- void display() //format 11:59:59
- {
- cout.fill('0'); //with leading zeros
- cout << hrs
- << ':' << setw(2) << mins
- << ':' << setw(2) << secs;
- }
- Time operator +(const Time &s){
- int newh, newm, news;
- news = secs + s.secs;
- newm = mins + s.mins;
- newh = hrs + s.hrs;
- while (news >= 60){
- newm++;
- news -= 60;
- }
- while (newm >= 60){
- newh++;
- newm -= 60;
- }
- if (newh >= 24){
- newh = newh % 24;
- }
- return Time(newh, newm, news);
- }
- Time& operator --(){
- secs--;
- if (secs < 0){
- secs += 60;
- mins--;
- }
- if (mins < 0){
- mins += 60;
- hrs--;
- }
- if (hrs < 0){
- hrs += 24;
- }
- return *this;
- }
- Time& operator ++(){
- secs++;
- if (secs >= 60){
- secs -= 60;
- mins++;
- }
- if (mins >= 60){
- mins -= 60;
- hrs++;
- }
- if (hrs >= 24){
- hrs -= 24;
- }
- return *this;
- }
- Time operator --(int){
- Time Pom = *this;
- secs--;
- if (secs < 0){
- secs += 60;
- mins--;
- }
- if (mins < 0){
- mins += 60;
- hrs--;
- }
- if (hrs < 0){
- hrs += 24;
- }
- return Pom;
- }
- Time operator ++(int){
- Time Pom = *this;
- secs++;
- if (secs >= 60){
- secs -= 60;
- mins++;
- }
- if (mins >= 60){
- mins -= 60;
- hrs++;
- }
- if (hrs >= 24){
- hrs -= 24;
- }
- return Pom;
- }
- }; //end class time
- ////////////////////////////////////////////////////////////////
- int main()
- {
- Time time1(5, 59, 59); //creates and initialze
- Time time2(4, 0, 1); // two times
- Time time3,time4; //create another time
- time3 = time1 + time2; //add two times
- cout << "\nSum = "; time3.display(); //display result
- time4 = --time3;
- cout << "\nPrefix decr time4 = "; time4.display();
- cout << "\nPrefix decr time3 = "; time3.display();
- time4 = ++time3;
- cout << "\nPrefix incr time4 = "; time4.display();
- cout << "\nPrefix incr time3 = "; time3.display();
- cout << "\nNo change = "; time3.display();
- time4 = time3--;
- cout << "\nPostfix decr time4 = "; time4.display();
- cout << "\nPostfix decr time3 = "; time3.display();
- time4 = time3++;
- cout << "\nPostfix incr time4 = "; time4.display();
- cout << "\nPostfix incr time3 = "; time3.display();
- cout << "\nNo change = "; time3.display();
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement