Προαπαιτούµενη Κάθε οµάδα θα πρέπει να εµπλουτίσει το ίδιο πρόγραµµα, που έκανε την προηγούµενη φορά, προσθέτοντας στην κλάση του έναν ή περισσότερους υπερφορτωµένους τελεστές (όπως <<, >>, ++, +,-,+=..) Παραδείγµατα 19,20. 17TimeThis.h function returns reference pointer to same object return *this; //BK 28/04/10-17TimeThis.cpp #include <iostream> #include <iomanip> using namespace std; class Time private: int hour; // 0-23 int minute; // 0-59 int second; // 0-59 public: Time(); Time( int hr, int min, int sec ); Time &sethour( int ); // set hour Time &setminute( int ); // set minute Time &setsecond( int ); // set second Time &settime( int, int, int ); //set all void printstandard()const; ; Time::Time() hour = minute = second = 0; Time::Time( int hr, int min, int sec ) settime( hr, min, sec ); Time &Time::setHour( int h ) hour = ( h >= 0 && h < 24 )? h : 0; Time &Time::setMinute( int m ) minute = ( m >= 0 && m < 60 )? m : 0; Time::Time &Time::setSecond( int s ) second = ( s >= 0 && s < 60 )? s : 0; Time &Time::setTime( int h, int m, int s ) sethour( h ); setminute( m ); setsecond( s ); return *this; // enables cascading void Time::printStandard()const cout << ( ( hour == 0 hour == 12 )? 12 : hour % 12 ) << ":" << setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 ) << second << ( hour < 12? " AM" : " PM" ); BK -Προαπαιτούμενη 7 ου Εργαστηρίου - σελίs 1
int main() Time t; // instantiate object t of class Time // cascaded function calls t.sethour( 18 ).setminute( 10 ).setsecond( 22 ); cout << "\nthe initial standard time is "; t.printstandard(); // 6:10:22 PM cout << "\n\nnew standard time: "; // cascaded function calls t.settime( 20, 20, 20 ).printstandard(); cout << endl << endl ; return 0; // Με δυναµική διαχείρηση µνήµης. Operators new and delete int main() Time* timeptra; timeptra = new Time; Time *timeptrb = new Time( 6, 10, 22 ); cout << "\nthe initial standard time is "; timeptrb->printstandard(); // 6:10:22 PM cout << "\n\nnew standard time: "; timeptra->settime( 20, 20, 20 ).printstandard(); cout << endl << endl ; delete timeptra; delete timeptrb; return 0; BK -Προαπαιτούμενη 7 ου Εργαστηρίου - σελίs 2
19Phone.cpp Υπερφόρτωση τελεστών >>, << //BK 28/04/10-19PhoneOverIoStream.cpp #include <iostream> #include <iomanip> using namespace std; class PhoneNumber private: //2310 335 235 char areacode[ 5 ]; // 4-digit area code and null char line1[ 4 ]; // 3-digit line and null char line2[ 4 ]; // 3-digit line and null friend ostream &operator<<( ostream&, const PhoneNumber & ); friend istream &operator>>( istream&, PhoneNumber & ); ; // end class PhoneNumber // overloaded stream-insertion operator; ME ΦΙΛΕΣ ΣΥΝΑΡΤΗΣΕΙΣ ostream &operator<<( ostream &output, const PhoneNumber &num ) output << "(" << num.areacode << ") " << num.line1 << "-" << num.line2; return output; // enables cout << phone1 << phone2 << phone3; //first calls operator<<(cout, phone1), and returns cout. //Next, cout << phone2 executes. // overloaded stream-extraction operator; istream &operator>>( istream &input, PhoneNumber &num ) //input data in form: (2310) 335-235 input.ignore(); // skip ( input >> setw( 5 ) >> num.areacode; // input area code input.ignore( 2 ); // skip ), space input >> setw( 4 ) >> num.line1; // input line input.ignore(); // skip dash input >> setw( 4 ) >> num.line2; // input line return input; // enables cin >> a >> b >> c; int main() PhoneNumber phone; // create object phone cout << "Enter phone number in the form (2310) 335-235:\n"; //the object of class Phonenumber appears on the right of the operator. cin >> phone; // operator>>(cin,phone); cout << "The phone number entered was: " ; cout << phone << endl; return 0; BK -Προαπαιτούμενη 7 ου Εργαστηρίου - σελίs 3
20DateΟverload.cpp Υπερφόρτωση τελεστών ++, +=,!=, ==, << //BK 28/04/10-20DateOverload.cpp #include <iostream> #include <iomanip> using namespace std; class Date private: int month; int day; int year; void checkdays(); static const int days[]; // array of days per month friend ostream &operator<<( ostream &, const Date & ); public: Date( int m = 1, int d = 1, int y = 2010 ); // constructor void setdate( int, int, int ); // set the date ; Date &operator++ (); // preincrement operator Date operator++ ( int ); // postincrement operator Date &operator+=(const int& ); // add days, modify object bool operator!=(const Date & date1 ) const; bool operator==(const Date & date1 ) const; const int Date::days[] = 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ; Date::Date( int m, int d, int y ) setdate( m, d, y ); void Date::checkdays() if ( month == 2) //δεν ελέγχει leapyear ( day >= 1 && day <= 29 )? day : 1; else ( day >= 1 && day <= days[ month ] )? day : 1; void Date::setDate( int mm, int dd, int yy ) month = ( mm >= 1 && mm <= 12 )? mm : 1; year = ( yy >= 1900 && yy <= 2100 )? yy : 2010; day=dd; checkdays(); // overloaded preincrement operator Date &Date::operator++() day++; checkdays(); return *this; // reference return to create an lvalue // end function operator++ BK -Προαπαιτούμενη 7 ου Εργαστηρίου - σελίs 4
// overloaded postincrement operator; note that the dummy // integer parameter does not have a parameter name Date Date::operator++( int ) Date temp = *this; // hold current state of object checkdays(); // return unincremented, saved, temporary object return temp; // value return; not a reference return // end function operator++ ostream &operator <<( ostream &out, const Date &d ) out << d.day << "/"<< d.month << "/" << d.year; return out; //overload operator += //add Days Date &Date::operator+=(const int& Days ) day+=days; checkdays(); //overload operator!= bool Date::operator!=(const Date& date1 ) const return (( day!= date1.day ) ( month!= date1.month) ( year!= date1.year)) ; //overload operator == bool Date::operator==(const Date& date1 ) const return (( day == date1.day ) & ( month == date1.month)& ( year==date1.year)) ; int main() Date d1; // defaults to Avril 28, 2010 Date d2( 1, 9, 2010 ); cout << "d1 is " << d1 << "\nd2 is " << d2 ; cout << "\n\ntesting the preincrement operator:\n" << " d1 is " << d1 << '\n'; cout << "++d1 is " << ++d1 << '\n'; cout << " d1 is " << d1; cout << "\n\ntesting the postincrement operator:\n" << " d1 is " << d1 << '\n'; BK -Προαπαιτούμενη 7 ου Εργαστηρίου - σελίs 5
cout << "d1++ is " << d1++ << '\n'; cout << " d1 is " << d1 << endl; cout << "\n\ntesting the operator += \n"; cout << "d1 += 7 is " << ( d1 += 7 ); cout << "\n\nd1 is \"" << d1 << "\"; \nd2 is \"" << d2 << "\ntesting operators == and!= with d1 and d2:" << "\nd2 == d1 yields " << ( d2 == d1? "true" : "false" ) << "\nd2!= d1 yields " << ( d2!= d1? "true" : "false" ); if (d1==d2) cout << endl; return 0; Operators in C and C++ http://en.wikipedia.org/wiki/operators_in_c_and_c%2b%2b (λίστα τελεστών) BK -Προαπαιτούμενη 7 ου Εργαστηρίου - σελίs 6