From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp | 85 ++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp (limited to 'Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp') diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp new file mode 100644 index 0000000..ef3818b --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp @@ -0,0 +1,85 @@ +// Fig. 7.7: date1.cpp +// Member-function definitions for class Date. +#include + +using std::cout; +using std::endl; + +// include Date class definition from date1.h +#include "date1.h" + +// constructor confirms proper value for month; calls +// utility function checkDay to confirm proper value for day +Date::Date( int mn, int dy, int yr ) +{ + if ( mn > 0 && mn <= 12 ) // validate the month + month = mn; + + else { // invalid month set to 1 + month = 1; + cout << "Month " << mn << " invalid. Set to month 1.\n"; + } + + year = yr; // should validate yr + day = checkDay( dy ); // validate the day + + // output Date object to show when its constructor is called + cout << "Date object constructor for date "; + print(); + cout << endl; + +} // end Date constructor + +// print Date object in form month/day/year +void Date::print() const +{ + cout << month << '/' << day << '/' << year; + +} // end function print + +// output Date object to show when its destructor is called +Date::~Date() +{ + cout << "Date object destructor for date "; + print(); + cout << endl; + +} // end ~Date destructor + +// utility function to confirm proper day value based on +// month and year; handles leap years, too +int Date::checkDay( int testDay ) const +{ + static const int daysPerMonth[ 13 ] = + { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + + // determine whether testDay is valid for specified month + if ( testDay > 0 && testDay <= daysPerMonth[ month ] ) + return testDay; + + // February 29 check for leap year + if ( month == 2 && testDay == 29 && + ( year % 400 == 0 || + ( year % 4 == 0 && year % 100 != 0 ) ) ) + return testDay; + + cout << "Day " << testDay << " invalid. Set to day 1.\n"; + + return 1; // leave object in consistent state if bad value + +} // end function checkDay + +/************************************************************************** + * (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. * + *************************************************************************/ -- cgit v1.2.3