60 likes | 278 Views
Overloading Unary Operators. // timeinc.cpp // uses overloaded ++ operator to increment objects // of class time #include <iostream.h> // for cout, etc. #include <stdio.h> // for printf( ), scanf( )
E N D
Overloading Unary Operators // timeinc.cpp // uses overloaded ++ operator to increment objects // of class time #include <iostream.h> // for cout, etc. #include <stdio.h> // for printf( ), scanf( ) //////////////////////////////////////////////////////////////////////////////////////////////////// class time { private: int hrs; // hours int mins; // minutes int secs; // seconds public: time( ) // constructor { secs = 0; mins = 0; hrs = 0; } time to 0:00:00 void printTime(void) { cout << hrs << ‘ : ‘ << mins } << ‘ : ‘ << secs; } void readTime(void) //read time from keyboard { cin >> hrs >> mins >> secs; } void operator ++ (void) { secs++; if( secs > 59 ) { secs -= 60; ++mins; } if( mins > 59 ) { mins -= 60; ++hrs; } } }; ///////////////////////////////////////////////////////////////////////////////////////////////////// Object Oriented Programming Using C - Example Class 7 - Overhead 1
Overloading Unary Operators ////////////////////////////////////////////////////////////////////////////////////////////// main( ) { time t1; // declare time variable cout << “\nEnter time (format hh:mm:ss): ”; t1.readTime( ); // read t1 cout << “\nt1 = ”; // print t1 t1.printTime( ); ++t1; // increment t1 (or use t1++) cout << “\nAfter increment, t1 = ”; t1.printTime( ); } Object Oriented Programming Using C - Example Class 7 - Overhead 2
Overloading Binary Operators // timeplus.cpp // demonstrates operator overloading, using ‘+’ to add times #include <iostream.h> // for cout, etc. #include <stdio.h> // for printf( ), scanf( ) //////////////////////////////////////////////////////////////////////////////////////////////////////// class time { private: int hrs; // hours int mins; // minutes int secs; // seconds public: time( ) // constructor { secs = 0; mins = 0; hrs = 0; } // initialize to 0:00:00 void printTime(void) // print time { cout << hrs << ‘ : ‘ << mins } << ‘ : ‘ << secs; void readTime(void) //read time from keyboard { cin >> hrs >> mins >> secs; } Object Oriented Programming Using C - Example Class 7 - Overhead 3a
Overloading Binary Operators time operator + (time t) // add two times { // to tmp time time tmp; tmp.secs = secs + t.secs; if(tmp.secs > 59) { tmp.secs -= 60; ++tmp.mins; } tmp.mins += mins + t.mins; if(tmp.mins > 59) { tmp.mins -= 60; ++tmp.hrs; } tmp.hrs += hrs + t.hrs; return( tmp ); // return tmp time }; Object Oriented Programming Using C - Example Class 7 - Overhead 3b
Overloading Binary Operators main( ) { time t1, t2, tAnswer; // declare time variables cout << “\nEnter first time ( format hh:mm:ss) : ”; t1.readTime( ); // read t1 cout << “\nEnter second time (format hh:mm:ss): ”; t2.readTime( ); // read t2 cout << “\nt1 = ”; // print t1 t1.printTime( ); cout << “\nt2 = ”; // print t2 t2.printTime( ); tAnswer = t1 + t2; // overloaded + operator cout << “\nSum = ”; // print tAnswer tAnswer.printTime( ); } Object Oriented Programming Using C - Example Class 7 - Overhead 4