cpp04.cpp

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

CPP
72
字号

// Coded by plusir -- Jan.07.2003.
// Standard C++ Bible -- (P366-12-4)

#include <iostream>
using namespace std ;

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

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

		Date operator + ( int ) const ;
		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 += ( int n )
{
	*this = *this + n ;
	return *this ;
}

int main()
{
	Date oldDate( 2, 20, 1997 ) ;
	Date newDate( 2, 30, 1998 ) ;

	( oldDate += 1 ) = newDate ;
	oldDate.display() ;
    
	return 0 ;
}

⌨️ 快捷键说明

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