summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/examples/ch06/Fig06_12_14
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/ch06/Fig06_12_14
downloadStudium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.gz
Studium-33613a85afc4b1481367fbe92a17ee59c240250b.tar.bz2
add new repoHEADmaster
Diffstat (limited to 'Bachelor/Prog1/examples/ch06/Fig06_12_14')
-rw-r--r--Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp63
-rw-r--r--Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp65
-rw-r--r--Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h40
3 files changed, 168 insertions, 0 deletions
diff --git a/Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp
new file mode 100644
index 0000000..8a7546a
--- /dev/null
+++ b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp
@@ -0,0 +1,63 @@
+// Fig. 6.14: fig06_14.cpp
+// Demonstrating a default constructor for class Time.
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+// include definition of class Time from time2.h
+#include "time2.h"
+
+int main()
+{
+ Time t1; // all arguments defaulted
+ Time t2( 2 ); // minute and second defaulted
+ Time t3( 21, 34 ); // second defaulted
+ Time t4( 12, 25, 42 ); // all values specified
+ Time t5( 27, 74, 99 ); // all bad values specified
+
+ cout << "Constructed with:\n\n"
+ << "all default arguments:\n ";
+ t1.printUniversal(); // 00:00:00
+ cout << "\n ";
+ t1.printStandard(); // 12:00:00 AM
+
+ cout << "\n\nhour specified; default minute and second:\n ";
+ t2.printUniversal(); // 02:00:00
+ cout << "\n ";
+ t2.printStandard(); // 2:00:00 AM
+
+ cout << "\n\nhour and minute specified; default second:\n ";
+ t3.printUniversal(); // 21:34:00
+ cout << "\n ";
+ t3.printStandard(); // 9:34:00 PM
+
+ cout << "\n\nhour, minute, and second specified:\n ";
+ t4.printUniversal(); // 12:25:42
+ cout << "\n ";
+ t4.printStandard(); // 12:25:42 PM
+
+ cout << "\n\nall invalid values specified:\n ";
+ t5.printUniversal(); // 00:00:00
+ cout << "\n ";
+ t5.printStandard(); // 12:00:00 AM
+ cout << endl;
+
+ return 0;
+
+} // end main
+
+/**************************************************************************
+ * (C) Copyright 1992-2002 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/ch06/Fig06_12_14/Time2.cpp b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp
new file mode 100644
index 0000000..5a5d42c
--- /dev/null
+++ b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp
@@ -0,0 +1,65 @@
+// Fig. 6.13: time2.cpp
+// Member-function definitions for class Time.
+#include <iostream>
+
+using std::cout;
+
+#include <iomanip>
+
+using std::setfill;
+using std::setw;
+
+// include definition of class Time from time2.h
+#include "time2.h"
+
+// Time constructor initializes each data member to zero;
+// ensures all Time objects start in a consistent state
+Time::Time( int hr, int min, int sec )
+{
+ setTime( hr, min, sec ); // validate and set time
+
+} // end Time constructor
+
+// set new Time value using universal time, perform validity
+// checks on the data values and set invalid values to zero
+void Time::setTime( int h, int m, int s )
+{
+ hour = ( h >= 0 && h < 24 ) ? h : 0;
+ minute = ( m >= 0 && m < 60 ) ? m : 0;
+ second = ( s >= 0 && s < 60 ) ? s : 0;
+
+} // end function setTime
+
+// print Time in universal format
+void Time::printUniversal()
+{
+ cout << setfill( '0' ) << setw( 2 ) << hour << ":"
+ << setw( 2 ) << minute << ":"
+ << setw( 2 ) << second;
+
+} // end function printUniversal
+
+// print Time in standard format
+void Time::printStandard()
+{
+ cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
+ << ":" << setfill( '0' ) << setw( 2 ) << minute
+ << ":" << setw( 2 ) << second
+ << ( hour < 12 ? " AM" : " PM" );
+
+} // end function printStandard
+
+/**************************************************************************
+ * (C) Copyright 1992-2002 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. *
+ *************************************************************************/
diff --git a/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h
new file mode 100644
index 0000000..3b99b88
--- /dev/null
+++ b/Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h
@@ -0,0 +1,40 @@
+// Fig. 6.12: time2.h
+// Declaration of class Time.
+// Member functions defined in time2.cpp.
+
+// prevent multiple inclusions of header file
+#ifndef TIME2_H
+#define TIME2_H
+
+// Time abstract data type definition
+class Time {
+
+public:
+ Time( int = 0, int = 0, int = 0); // default constructor
+ void setTime( int, int, int ); // set hour, minute, second
+ void printUniversal(); // print universal-time format
+ void printStandard(); // print standard-time format
+
+private:
+ int hour; // 0 - 23 (24-hour clock format)
+ int minute; // 0 - 59
+ int second; // 0 - 59
+
+}; // end class Time
+
+#endif
+
+/**************************************************************************
+ * (C) Copyright 1992-2002 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. *
+ *************************************************************************/