cpp05.cpp

来自「C++参考书」· C++ 代码 · 共 78 行

CPP
78
字号

// Coded by plusir -- Jan.07.2003.
// Standard C++ Bible -- (P367-12-5)

#include <iostream>
using namespace std ;

class Date
{
	public:
		Date( int m = 0, int d = 0, int y = 0 )
		{
			month = m ;
			day = d ;
			year = y ;
		}

		void display( void ) const
		{
			cout << month << '/' << day << '/' << year << endl ;
		}

		Date operator + ( int ) const ;
		Date& operator ++ ( void ) ;
		Date operator ++ ( int ) ;

	private:
		int month ;
		int day ;
		int year ;

		static int dys[] ;
} ;

int Date::dys[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } ;

Date Date::operator + ( int n ) const
{
	Date dt( *this ) ;
	n += dt.day ;

	while ( n > dys[dt.month - 1] ) {
		n -= dys[dt.month - 1] ;
		if ( ( ++dt.month ) == 13 ) {
			dt.month = 1 ;
			dt.year++ ;
		}
	}

	dt.day = n ;

	return dt ;
}

Date& Date::operator ++ ( void )
{
	*this = *this + 1 ;
	return *this ;
}

Date Date::operator ++ ( int )
{
	Date dt = *this ;
	*this = *this + 1 ;
	return dt ;
}

int main()
{
	Date oldDate( 2, 20, 1997 ) ;
	oldDate++ ;
	oldDate.display() ;

	( ++oldDate ).display() ;

	return 0 ;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?