📄 datem.cpp
字号:
// Chapter 6 of C++ How to Program
// datem.cpp
// member function definitions for date.cpp
#include <iostream>
using std::cout;
#include "date.h"
// constructor
Date::Date( int m, int d, int y )
{
setDate( m, d, y );
} // end class Date constructor
// return day
int Date::getDay()
{
return day;
} // end function getDay
// return month
int Date::getMonth()
{
return month;
} // end function getMonth
// return year
int Date::getYear()
{
return year;
} // end function getYear
// set day
void Date::setDay( int d )
{
if ( month == 2 && isLeapYear() )
day = ( d <= 29 && d >= 1 ) ? d : 1;
else
day = ( d <= monthDays() && d >= 1 ) ? d : 1;
} // end function setDay
// set month
void Date::setMonth( int m )
{
month = m <= 12 && m >= 1 ? m : 1;
} // end function setMonth
// set year
void Date::setYear( int y )
{
year = y <= 2010 && y >= 1900 ? y : 1900;
} // end function setYear
// set date
void Date::setDate( int mo, int dy, int yr )
{
setMonth( mo );
setDay( dy );
setYear( yr );
} // end function setDate
// output Date
void Date::print()
{
cout << month << '-' << day << '-' << year << '\n';
} // end function print
/* Write function nextDay here */
// test whether is a leap year
bool Date::isLeapYear()
{
if ( year % 400 == 0 || ( year % 4 == 0 && year % 100 != 0 ) )
return true;
else
return false; // not a leap year
} // end function isLeapYear
// return days in month
int Date::monthDays()
{
const int days[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30,
31, 30, 31 };
return month == 2 && leapYear() ? 29 : days[ month - 1 ];
} // end function monthDays
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -