summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/examples/ch08/Fig08_10_12
diff options
context:
space:
mode:
authorSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
committerSven Eisenhauer <sven@sven-eisenhauer.net>2023-11-10 15:11:48 +0100
commit33613a85afc4b1481367fbe92a17ee59c240250b (patch)
tree670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog1/examples/ch08/Fig08_10_12
downloadStudium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz
Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Prog1/examples/ch08/Fig08_10_12')
-rw-r--r--Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp137
-rw-r--r--Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h49
-rw-r--r--Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp54
3 files changed, 240 insertions, 0 deletions
diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp
new file mode 100644
index 0000000..0c96a95
--- /dev/null
+++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.cpp
@@ -0,0 +1,137 @@
+// Fig. 8.11: date1.cpp
+// Date class member function definitions.
+#include <iostream>
+#include "date1.h"
+
+// initialize static member at file scope;
+// one class-wide copy
+const int Date::days[] =
+ { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
+
+// Date constructor
+Date::Date( int m, int d, int y )
+{
+ setDate( m, d, y );
+
+} // end Date constructor
+
+// set month, day and year
+void Date::setDate( int mm, int dd, int yy )
+{
+ month = ( mm >= 1 && mm <= 12 ) ? mm : 1;
+ year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;
+
+ // test for a leap year
+ if ( month == 2 && leapYear( year ) )
+ day = ( dd >= 1 && dd <= 29 ) ? dd : 1;
+ else
+ day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;
+
+} // end function setDate
+
+// overloaded preincrement operator
+Date &Date::operator++()
+{
+ helpIncrement();
+
+ return *this; // reference return to create an lvalue
+
+} // end function operator++
+
+// overloaded postincrement operator; note that the dummy
+// integer parameter does not have a parameter name
+Date Date::operator++( int )
+{
+ Date temp = *this;
+ helpIncrement(); // hold current state of object
+
+ // return unincremented, saved, temporary object
+ return temp; // value return; not a reference return
+
+} // end function operator++
+
+// add specified number of days to date
+const Date &Date::operator+=( int additionalDays )
+{
+ for ( int i = 0; i < additionalDays; i++ )
+ helpIncrement();
+
+ return *this; // enables cascading
+
+} // end function operator+=
+
+// if the year is a leap year, return true;
+// otherwise, return false
+bool Date::leapYear( int testYear ) const
+{
+ if ( testYear % 400 == 0 ||
+ ( testYear % 100 != 0 && testYear % 4 == 0 ) )
+ return true; // a leap year
+ else
+ return false; // not a leap year
+
+} // end function leapYear
+
+// determine whether the day is the last day of the month
+bool Date::endOfMonth( int testDay ) const
+{
+ if ( month == 2 && leapYear( year ) )
+ return testDay == 29; // last day of Feb. in leap year
+ else
+ return testDay == days[ month ];
+
+} // end function endOfMonth
+
+// function to help increment the date
+void Date::helpIncrement()
+{
+ // day is not end of month
+ if ( !endOfMonth( day ) )
+ ++day;
+
+ else
+
+ // day is end of month and month < 12
+ if ( month < 12 ) {
+ ++month;
+ day = 1;
+ }
+
+ // last day of year
+ else {
+ ++year;
+ month = 1;
+ day = 1;
+ }
+
+} // end function helpIncrement
+
+// overloaded output operator
+ostream &operator<<( ostream &output, const Date &d )
+{
+ static char *monthName[ 13 ] = { "", "January",
+ "February", "March", "April", "May", "June",
+ "July", "August", "September", "October",
+ "November", "December" };
+
+ output << monthName[ d.month ] << ' '
+ << d.day << ", " << d.year;
+
+ return output; // enables cascading
+
+} // end function operator<<
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/ \ No newline at end of file
diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h
new file mode 100644
index 0000000..d1d6148
--- /dev/null
+++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Date1.h
@@ -0,0 +1,49 @@
+// Fig. 8.10: date1.h
+// Date class definition.
+#ifndef DATE1_H
+#define DATE1_H
+#include <iostream>
+
+using std::ostream;
+
+class Date {
+ friend ostream &operator<<( ostream &, const Date & );
+
+public:
+ Date( int m = 1, int d = 1, int y = 1900 ); // constructor
+ void setDate( int, int, int ); // set the date
+
+ Date &operator++(); // preincrement operator
+ Date operator++( int ); // postincrement operator
+
+ const Date &operator+=( int ); // add days, modify object
+
+ bool leapYear( int ) const; // is this a leap year?
+ bool endOfMonth( int ) const; // is this end of month?
+
+private:
+ int month;
+ int day;
+ int year;
+
+ static const int days[]; // array of days per month
+ void helpIncrement(); // utility function
+
+}; // end class Date
+
+#endif
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/ \ No newline at end of file
diff --git a/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp
new file mode 100644
index 0000000..0d4c996
--- /dev/null
+++ b/Bachelor/Prog1/examples/ch08/Fig08_10_12/Fig08_12.cpp
@@ -0,0 +1,54 @@
+// Fig. 8.12: fig08_12.cpp
+// Date class test program.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+#include "date1.h" // Date class definition
+
+int main()
+{
+ Date d1; // defaults to January 1, 1900
+ Date d2( 12, 27, 1992 );
+ Date d3( 0, 99, 8045 ); // invalid date
+
+ cout << "d1 is " << d1 << "\nd2 is " << d2
+ << "\nd3 is " << d3;
+
+ cout << "\n\nd2 += 7 is " << ( d2 += 7 );
+
+ d3.setDate( 2, 28, 1992 );
+ cout << "\n\n d3 is " << d3;
+ cout << "\n++d3 is " << ++d3;
+
+ Date d4( 7, 13, 2002 );
+
+ cout << "\n\nTesting the preincrement operator:\n"
+ << " d4 is " << d4 << '\n';
+ cout << "++d4 is " << ++d4 << '\n';
+ cout << " d4 is " << d4;
+
+ cout << "\n\nTesting the postincrement operator:\n"
+ << " d4 is " << d4 << '\n';
+ cout << "d4++ is " << d4++ << '\n';
+ cout << " d4 is " << d4 << endl;
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (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. *
+ *************************************************************************/