<<< class Date implementation | Index | Conclusion >>> |
C++ allows operator syntax for user-defined types:
#include <iostream> int serial_julian_date( int Month, int Day, int Year ); int serial_2_month( int nDate ); int serial_2_day( int nDate ); int serial_2_year( int nDate ); class Date { // member variables int serial_date; public: // member functions void set_date( int month, int day, int year ) {} int get_month() { return 2; } int get_day() { return 8; } int get_year() { return 1999; } }; void operator<<( std::ostream&, Date dt ) { std::cout << dt.get_month(); std::cout << '/'; std::cout << dt.get_day(); std::cout << '/'; std::cout << dt.get_year(); } int main() { Date birthday; // instantiate object of type "Date" birthday.set_date( 2, 8, 1999 ); // member function calls std::cout << birthday; return 0; }
<<< class Date implementation | Index | Conclusion >>> |