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/ch06/Fig06_01/Fig06_01.cpp | 82 +++++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp | 114 +++++++++++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp | 58 +++++++++++ .../Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp | 57 +++++++++++ Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp | 65 ++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h | 40 ++++++++ Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp | 38 +++++++ Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp | 65 ++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h | 40 ++++++++ .../Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp | 32 ++++++ .../Prog1/examples/ch06/Fig06_09_11/Salesp.cpp | 86 ++++++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h | 36 +++++++ .../Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp | 63 ++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp | 65 ++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h | 40 ++++++++ .../Prog1/examples/ch06/Fig06_15_17/Create.cpp | 47 +++++++++ Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h | 34 ++++++ .../Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp | 68 ++++++++++++ .../Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp | 82 +++++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp | 107 +++++++++++++++++++ Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h | 50 +++++++++ .../Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp | 53 ++++++++++ Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp | 55 ++++++++++ Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h | 40 ++++++++ Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp | 72 +++++++++++++ 25 files changed, 1489 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_12_14/Fig06_14.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_12_14/Time2.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h create mode 100644 Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp (limited to 'Bachelor/Prog1/examples/ch06') diff --git a/Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp b/Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp new file mode 100644 index 0000000..0126a02 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_01/Fig06_01.cpp @@ -0,0 +1,82 @@ +// Fig. 6.1: fig06_01.cpp +// Create a structure, set its members, and print it. +#include + +using std::cout; +using std::endl; + +#include + +using std::setfill; +using std::setw; + +// structure definition +struct Time { + int hour; // 0-23 (24-hour clock format) + int minute; // 0-59 + int second; // 0-59 + +}; // end struct Time + +void printUniversal( const Time & ); // prototype +void printStandard( const Time & ); // prototype + +int main() +{ + Time dinnerTime; // variable of new type Time + + dinnerTime.hour = 18; // set hour member of dinnerTime + dinnerTime.minute = 30; // set minute member of dinnerTime + dinnerTime.second = 0; // set second member of dinnerTime + + cout << "Dinner will be held at "; + printUniversal( dinnerTime ); + cout << " universal time,\nwhich is "; + printStandard( dinnerTime ); + cout << " standard time.\n"; + + dinnerTime.hour = 29; // set hour to invalid value + dinnerTime.minute = 73; // set minute to invalid value + + cout << "\nTime with invalid values: "; + printUniversal( dinnerTime ); + cout << endl; + + return 0; + +} // end main + +// print time in universal-time format +void printUniversal( const Time &t ) +{ + cout << setfill( '0' ) << setw( 2 ) << t.hour << ":" + << setw( 2 ) << t.minute << ":" + << setw( 2 ) << t.second; + +} // end function printUniversal + +// print time in standard-time format +void printStandard( const Time &t ) +{ + cout << ( ( t.hour == 0 || t.hour == 12 ) ? + 12 : t.hour % 12 ) << ":" << setfill( '0' ) + << setw( 2 ) << t.minute << ":" + << setw( 2 ) << t.second + << ( t.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_03/Fig06_03.cpp b/Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp new file mode 100644 index 0000000..3bb7a28 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_03/Fig06_03.cpp @@ -0,0 +1,114 @@ +// Fig. 6.3: fig06_03.cpp +// Time class. +#include + +using std::cout; +using std::endl; + +#include + +using std::setfill; +using std::setw; + +// Time abstract data type (ADT) definition +class Time { + +public: + Time(); // 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 + +// Time constructor initializes each data member to zero and +// ensures all Time objects start in a consistent state +Time::Time() +{ + hour = minute = second = 0; + +} // end constructor Time + +// 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 + +int main() +{ + Time t; // instantiate object t of class Time + + // output Time object t's initial values + cout << "The initial universal time is "; + t.printUniversal(); // 00:00:00 + + cout << "\nThe initial standard time is "; + t.printStandard(); // 12:00:00 AM + + t.setTime( 13, 27, 6 ); // change time + + // output Time object t's new values + cout << "\n\nUniversal time after setTime is "; + t.printUniversal(); // 13:27:06 + + cout << "\nStandard time after setTime is "; + t.printStandard(); // 1:27:06 PM + + t.setTime( 99, 99, 99 ); // attempt invalid settings + + // output t's values after specifying invalid values + cout << "\n\nAfter attempting invalid settings:" + << "\nUniversal time: "; + t.printUniversal(); // 00:00:00 + + cout << "\nStandard time: "; + t.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_04/Fig06_04.cpp b/Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp new file mode 100644 index 0000000..68c1be3 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_04/Fig06_04.cpp @@ -0,0 +1,58 @@ +// Fig. 6.4: fig06_04.cpp +// Demonstrating the class member access operators . and -> +// +// CAUTION: IN FUTURE EXAMPLES WE AVOID PUBLIC DATA! +#include + +using std::cout; +using std::endl; + +// class Count definition +class Count { + +public: + int x; + + void print() + { + cout << x << endl; + } + +}; // end class Count + +int main() +{ + Count counter; // create counter object + Count *counterPtr = &counter; // create pointer to counter + Count &counterRef = counter; // create reference to counter + + cout << "Assign 1 to x and print using the object's name: "; + counter.x = 1; // assign 1 to data member x + counter.print(); // call member function print + + cout << "Assign 2 to x and print using a reference: "; + counterRef.x = 2; // assign 2 to data member x + counterRef.print(); // call member function print + + cout << "Assign 3 to x and print using a pointer: "; + counterPtr->x = 3; // assign 3 to data member x + counterPtr->print(); // call member function print + + 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_05_07/Fig06_05.cpp b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp new file mode 100644 index 0000000..85cca47 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Fig06_05.cpp @@ -0,0 +1,57 @@ +// Fig. 6.7: fig06_07.cpp +// Program to test class Time. +// NOTE: This file must be compiled with time1.cpp. +#include + +using std::cout; +using std::endl; + +// include definition of class Time from time1.h +#include "time1.h" + +int main() +{ + Time t; // instantiate object t of class Time + + // output Time object t's initial values + cout << "The initial universal time is "; + t.printUniversal(); // 00:00:00 + cout << "\nThe initial standard time is "; + t.printStandard(); // 12:00:00 AM + + t.setTime( 13, 27, 6 ); // change time + + // output Time object t's new values + cout << "\n\nUniversal time after setTime is "; + t.printUniversal(); // 13:27:06 + cout << "\nStandard time after setTime is "; + t.printStandard(); // 1:27:06 PM + + t.setTime( 99, 99, 99 ); // attempt invalid settings + + // output t's values after specifying invalid values + cout << "\n\nAfter attempting invalid settings:" + << "\nUniversal time: "; + t.printUniversal(); // 00:00:00 + cout << "\nStandard time: "; + t.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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp new file mode 100644 index 0000000..6ffea1c --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.cpp @@ -0,0 +1,65 @@ +// Fig. 6.6: time1.cpp +// Member-function definitions for class Time. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time1.h +#include "time1.h" + +// Time constructor initializes each data member to zero. +// Ensures all Time objects start in a consistent state. +Time::Time() +{ + hour = minute = second = 0; + +} // end Time constructor + +// Set new Time value using universal time. Perform validity +// checks on the data values. 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_05_07/Time1.h b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h new file mode 100644 index 0000000..9adaba7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_05_07/Time1.h @@ -0,0 +1,40 @@ +// Fig. 6.5: time1.h +// Declaration of class Time. +// Member functions are defined in time1.cpp + +// prevent multiple inclusions of header file +#ifndef TIME1_H +#define TIME1_H + +// Time abstract data type definition +class Time { + +public: + Time(); // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp b/Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp new file mode 100644 index 0000000..93cacc4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_08/Fig06_08.cpp @@ -0,0 +1,38 @@ +// Fig. 6.8: fig06_08.cpp +// Demonstrate errors resulting from attempts +// to access private class members. +#include + +using std::cout; + +// include definition of class Time from time1.h +#include "time1.h" + +int main() +{ + Time t; // create Time object + + + t.hour = 7; // error: 'Time::hour' is not accessible + + // error: 'Time::minute' is not accessible + cout << "minute = " << t.minute; + + 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_08/Time1.cpp b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp new file mode 100644 index 0000000..0f53ccf --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.cpp @@ -0,0 +1,65 @@ +// Fig. 6.6: time1.cpp +// Member-function definitions for class Time. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time1.h +#include "time1.h" + +// Time constructor initializes each data member to zero. +// Ensures all Time objects start in a consistent state. +Time::Time() +{ + hour = minute = second = 0; + +} // end Time constructor + +// Set new Time value using universal time. Perform validity +// checks on the data values. 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h new file mode 100644 index 0000000..9adaba7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_08/Time1.h @@ -0,0 +1,40 @@ +// Fig. 6.5: time1.h +// Declaration of class Time. +// Member functions are defined in time1.cpp + +// prevent multiple inclusions of header file +#ifndef TIME1_H +#define TIME1_H + +// Time abstract data type definition +class Time { + +public: + Time(); // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp new file mode 100644 index 0000000..82787f6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Fig06_11.cpp @@ -0,0 +1,32 @@ +// Fig. 6.11: fig06_11.cpp +// Demonstrating a utility function. +// Compile this program with salesp.cpp + +// include SalesPerson class definition from salesp.h +#include "salesp.h" + +int main() +{ + SalesPerson s; // create SalesPerson object s + + s.getSalesFromUser(); // note simple sequential code; no + s.printAnnualSales(); // control structures in main + + 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp new file mode 100644 index 0000000..4c05a6d --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.cpp @@ -0,0 +1,86 @@ +// Fig. 6.10: salesp.cpp +// Member functions for class SalesPerson. +#include + +using std::cout; +using std::cin; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +// include SalesPerson class definition from salesp.h +#include "salesp.h" + +// initialize elements of array sales to 0.0 +SalesPerson::SalesPerson() +{ + for ( int i = 0; i < 12; i++ ) + sales[ i ] = 0.0; + +} // end SalesPerson constructor + +// get 12 sales figures from the user at the keyboard +void SalesPerson::getSalesFromUser() +{ + double salesFigure; + + for ( int i = 1; i <= 12; i++ ) { + cout << "Enter sales amount for month " << i << ": "; + cin >> salesFigure; + setSales( i, salesFigure ); + + } // end for + +} // end function getSalesFromUser + +// set one of the 12 monthly sales figures; function subtracts +// one from month value for proper subscript in sales array +void SalesPerson::setSales( int month, double amount ) +{ + // test for valid month and amount values + if ( month >= 1 && month <= 12 && amount > 0 ) + sales[ month - 1 ] = amount; // adjust for subscripts 0-11 + + else // invalid month or amount value + cout << "Invalid month or sales figure" << endl; + +} // end function setSales + +// print total annual sales (with the help of utility function) +void SalesPerson::printAnnualSales() +{ + cout << setprecision( 2 ) << fixed + << "\nThe total annual sales are: $" + << totalAnnualSales() << endl; // call utility function + +} // end function printAnnualSales + +// private utility function to total annual sales +double SalesPerson::totalAnnualSales() +{ + double total = 0.0; // initialize total + + for ( int i = 0; i < 12; i++ ) // summarize sales results + total += sales[ i ]; + + return total; + +} // end function totalAnnualSales + +/************************************************************************** + * (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_09_11/Salesp.h b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h new file mode 100644 index 0000000..240e3e7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_09_11/Salesp.h @@ -0,0 +1,36 @@ +// Fig. 6.9: salesp.h +// SalesPerson class definition. +// Member functions defined in salesp.cpp. +#ifndef SALESP_H +#define SALESP_H + +class SalesPerson { + +public: + SalesPerson(); // constructor + void getSalesFromUser(); // input sales from keyboard + void setSales( int, double ); // set sales for a month + void printAnnualSales(); // summarize and print sales + +private: + double totalAnnualSales(); // utility function + double sales[ 12 ]; // 12 monthly sales figures + +}; // end class SalesPerson + +#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. * + *************************************************************************/ \ No newline at end of file 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 + +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 + +using std::cout; + +#include + +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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp new file mode 100644 index 0000000..1f7614f --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.cpp @@ -0,0 +1,47 @@ +// Fig. 6.16: create.cpp +// Member-function definitions for class CreateAndDestroy +#include + +using std::cout; +using std::endl; + +// include CreateAndDestroy class definition from create.h +#include "create.h" + +// constructor +CreateAndDestroy::CreateAndDestroy( + int objectNumber, char *messagePtr ) +{ + objectID = objectNumber; + message = messagePtr; + + cout << "Object " << objectID << " constructor runs " + << message << endl; + +} // end CreateAndDestroy constructor + +// destructor +CreateAndDestroy::~CreateAndDestroy() +{ + // the following line is for pedagogic purposes only + cout << ( objectID == 1 || objectID == 6 ? "\n" : "" ); + + cout << "Object " << objectID << " destructor runs " + << message << endl; + +} // end ~CreateAndDestroy destructor + +/************************************************************************** + * (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_15_17/Create.h b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h new file mode 100644 index 0000000..f0a73cb --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Create.h @@ -0,0 +1,34 @@ +// Fig. 6.15: create.h +// Definition of class CreateAndDestroy. +// Member functions defined in create.cpp. +#ifndef CREATE_H +#define CREATE_H + +class CreateAndDestroy { + +public: + CreateAndDestroy( int, char * ); // constructor + ~CreateAndDestroy(); // destructor + +private: + int objectID; + char *message; + +}; // end class CreateAndDestroy + +#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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp new file mode 100644 index 0000000..3bd2906 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_15_17/Fig06_17.cpp @@ -0,0 +1,68 @@ +// Fig. 6.17: fig06_17.cpp +// Demonstrating the order in which constructors and +// destructors are called. +#include + +using std::cout; +using std::endl; + +// include CreateAndDestroy class definition from create.h +#include "create.h" + +void create( void ); // prototype + +// global object +CreateAndDestroy first( 1, "(global before main)" ); + +int main() +{ + cout << "\nMAIN FUNCTION: EXECUTION BEGINS" << endl; + + CreateAndDestroy second( 2, "(local automatic in main)" ); + + static CreateAndDestroy third( + 3, "(local static in main)" ); + + create(); // call function to create objects + + cout << "\nMAIN FUNCTION: EXECUTION RESUMES" << endl; + + CreateAndDestroy fourth( 4, "(local automatic in main)" ); + + cout << "\nMAIN FUNCTION: EXECUTION ENDS" << endl; + + return 0; + +} // end main + +// function to create objects +void create( void ) +{ + cout << "\nCREATE FUNCTION: EXECUTION BEGINS" << endl; + + CreateAndDestroy fifth( 5, "(local automatic in create)" ); + + static CreateAndDestroy sixth( + 6, "(local static in create)" ); + + CreateAndDestroy seventh( + 7, "(local automatic in create)" ); + + cout << "\nCREATE FUNCTION: EXECUTION ENDS" << endl; + +} // end function create + +/************************************************************************** + * (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_18_20/Fig06_20.cpp b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp new file mode 100644 index 0000000..58f5bb9 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Fig06_20.cpp @@ -0,0 +1,82 @@ +// Fig. 6.20: fig06_20.cpp +// Demonstrating the Time class set and get functions +#include + +using std::cout; +using std::endl; + +// include definition of class Time from time3.h +#include "time3.h" + +void incrementMinutes( Time &, const int ); // prototype + +int main() +{ + Time t; // create Time object + + // set time using individual set functions + t.setHour( 17 ); // set hour to valid value + t.setMinute( 34 ); // set minute to valid value + t.setSecond( 25 ); // set second to valid value + + // use get functions to obtain hour, miunute and second + cout << "Result of setting all valid values:\n" + << " Hour: " << t.getHour() + << " Minute: " << t.getMinute() + << " Second: " << t.getSecond(); + + // set time using individual set functions + t.setHour( 234 ); // invalid hour set to 0 + t.setMinute( 43 ); // set minute to valid value + t.setSecond( 6373 ); // invalid second set to 0 + + // display hour, minute and second after setting + // invalid hour and second values + cout << "\n\nResult of attempting to set invalid hour and" + << " second:\n Hour: " << t.getHour() + << " Minute: " << t.getMinute() + << " Second: " << t.getSecond() << "\n\n"; + + t.setTime( 11, 58, 0 ); // set time + incrementMinutes( t, 3 ); // increment t's minute by 3 + + return 0; + +} // end main + +// add specified number of minutes to a Time object +void incrementMinutes( Time &tt, const int count ) +{ + cout << "Incrementing minute " << count + << " times:\nStart time: "; + tt.printStandard(); + + for ( int i = 0; i < count; i++ ) { + tt.setMinute( ( tt.getMinute() + 1 ) % 60 ); + + if ( tt.getMinute() == 0 ) + tt.setHour( ( tt.getHour() + 1 ) % 24 ); + + cout << "\nminute + 1: "; + tt.printStandard(); + + } // end for + + cout << endl; + +} // end function incrementMinutes + +/************************************************************************** + * (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_18_20/Time3.cpp b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp new file mode 100644 index 0000000..7cf0176 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.cpp @@ -0,0 +1,107 @@ +// Fig. 6.19: time3.cpp +// Member-function definitions for Time class. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time3.h +#include "time3.h" + +// constructor function to initialize private data; +// calls member function setTime to set variables; +// default values are 0 (see class definition) +Time::Time( int hr, int min, int sec ) +{ + setTime( hr, min, sec ); + +} // end Time constructor + +// set hour, minute and second values +void Time::setTime( int h, int m, int s ) +{ + setHour( h ); + setMinute( m ); + setSecond( s ); + +} // end function setTime + +// set hour value +void Time::setHour( int h ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + +} // end function setHour + +// set minute value +void Time::setMinute( int m ) +{ + minute = ( m >= 0 && m < 60 ) ? m : 0; + +} // end function setMinute + +// set second value +void Time::setSecond( int s ) +{ + second = ( s >= 0 && s < 60 ) ? s : 0; + +} // end function setSecond + +// return hour value +int Time::getHour() +{ + return hour; + +} // end function getHour + +// return minute value +int Time::getMinute() +{ + return minute; + +} // end function getMinute + +// return second value +int Time::getSecond() +{ + return second; + +} // end function getSecond + +// 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_18_20/Time3.h b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h new file mode 100644 index 0000000..2f68d31 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_18_20/Time3.h @@ -0,0 +1,50 @@ +// Fig. 6.18: time3.h +// Declaration of class Time. +// Member functions defined in time3.cpp + +// prevent multiple inclusions of header file +#ifndef TIME3_H +#define TIME3_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); // default constructor + + // set functions + void setTime( int, int, int ); // set hour, minute, second + void setHour( int ); // set hour + void setMinute( int ); // set minute + void setSecond( int ); // set second + + // get functions + int getHour(); // return hour + int getMinute(); // return minute + int getSecond(); // return second + + void printUniversal(); // output universal-time format + void printStandard(); // output standard-time format + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end clas 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp new file mode 100644 index 0000000..a5b14b6 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Fig06_23.cpp @@ -0,0 +1,53 @@ +// Fig. 6.23: fig06_23.cpp +// Demonstrating a public member function that +// returns a reference to a private data member. +#include + +using std::cout; +using std::endl; + +// include definition of class Time from time4.h +#include "time4.h" + +int main() +{ + Time t; + + // store in hourRef the reference returned by badSetHour + int &hourRef = t.badSetHour( 20 ); + + cout << "Hour before modification: " << hourRef; + + // use hourRef to set invalid value in Time object t + hourRef = 30; + + cout << "\nHour after modification: " << t.getHour(); + + // Dangerous: Function call that returns + // a reference can be used as an lvalue! + t.badSetHour( 12 ) = 74; + + cout << "\n\n*********************************\n" + << "POOR PROGRAMMING PRACTICE!!!!!!!!\n" + << "badSetHour as an lvalue, Hour: " + << t.getHour() + << "\n*********************************" << 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp new file mode 100644 index 0000000..eef65f2 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.cpp @@ -0,0 +1,55 @@ +// Fig. 6.22: time4.cpp +// Member-function definitions for Time class. + +// include definition of class Time from time4.h +#include "time4.h" + +// constructor function to initialize private data; +// calls member function setTime to set variables; +// default values are 0 (see class definition) +Time::Time( int hr, int min, int sec ) +{ + setTime( hr, min, sec ); + +} // end Time constructor + +// set values of hour, minute and second +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 + +// return hour value +int Time::getHour() +{ + return hour; + +} // end function getHour + +// POOR PROGRAMMING PRACTICE: +// Returning a reference to a private data member. +int &Time::badSetHour( int hh ) +{ + hour = ( hh >= 0 && hh < 24 ) ? hh : 0; + + return hour; // DANGEROUS reference return + +} // end function badSetHour + +/************************************************************************** + * (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_21_23/Time4.h b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h new file mode 100644 index 0000000..92a5d84 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_21_23/Time4.h @@ -0,0 +1,40 @@ +// Fig. 6.21: time4.h +// Declaration of class Time. +// Member functions defined in time4.cpp + +// prevent multiple inclusions of header file +#ifndef TIME4_H +#define TIME4_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); + void setTime( int, int, int ); + int getHour(); + + int &badSetHour( int ); // DANGEROUS reference return + +private: + int hour; + int minute; + int second; + +}; // 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp b/Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp new file mode 100644 index 0000000..9df3198 --- /dev/null +++ b/Bachelor/Prog1/examples/ch06/Fig06_24/Fig06_24.cpp @@ -0,0 +1,72 @@ +// Fig. 6.24: fig06_24.cpp +// Demonstrating that class objects can be assigned +// to each other using default memberwise assignment. +#include + +using std::cout; +using std::endl; + +// class Date definition +class Date { + +public: + Date( int = 1, int = 1, int = 1990 ); // default constructor + void print(); + +private: + int month; + int day; + int year; + +}; // end class Date + +// Date constructor with no range checking +Date::Date( int m, int d, int y ) +{ + month = m; + day = d; + year = y; + +} // end constructor Date + +// print Date in the format mm-dd-yyyy +void Date::print() +{ + cout << month << '-' << day << '-' << year; + +} // end function print + +int main() +{ + Date date1( 7, 4, 2002 ); + Date date2; // date2 defaults to 1/1/1990 + + cout << "date1 = "; + date1.print(); + cout << "\ndate2 = "; + date2.print(); + + date2 = date1; // default memberwise assignment + + cout << "\n\nAfter default memberwise assignment, date2 = "; + date2.print(); + 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. * + *************************************************************************/ -- cgit v1.2.3