cpp01.cpp

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

CPP
65
字号

// Coded by plusir -- Jan.05.2003.
// Standard C++ Bible -- (P358-12-1)

#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 ;

	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 ;
}

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

	//newDate = oldDate + 21 ;
	newDate = oldDate.operator + ( 21 ) ;

	newDate.display() ;

	return 0 ;
}

⌨️ 快捷键说明

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