1 / 5

Overloading Unary Operators

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( )

Download Presentation

Overloading Unary Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

More Related