📄 cpp04.cpp
字号:
// 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -