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 --- .../Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp | 42 ++++++++ Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp | 107 +++++++++++++++++++ Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h | 49 +++++++++ Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp | 75 ++++++++++++++ Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp | 74 +++++++++++++ Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp | 85 +++++++++++++++ Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h | 39 +++++++ .../Prog1/examples/ch07/Fig07_06_10/Employee1.cpp | 72 +++++++++++++ .../Prog1/examples/ch07/Fig07_06_10/Employee1.h | 42 ++++++++ .../Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp | 40 ++++++++ Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp | 72 +++++++++++++ Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp | 65 ++++++++++++ Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp | 66 ++++++++++++ .../Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp | 48 +++++++++ Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp | 114 +++++++++++++++++++++ Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h | 51 +++++++++ .../Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp | 55 ++++++++++ .../Prog1/examples/ch07/Fig07_17_19/employee2.cpp | 88 ++++++++++++++++ .../Prog1/examples/ch07/Fig07_17_19/employee2.h | 41 ++++++++ .../Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp | 39 +++++++ .../examples/ch07/Fig07_20_23/implementation.h | 48 +++++++++ .../Prog1/examples/ch07/Fig07_20_23/interface.cpp | 48 +++++++++ .../Prog1/examples/ch07/Fig07_20_23/interface.h | 34 ++++++ 23 files changed, 1394 insertions(+) create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp create mode 100644 Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h (limited to 'Bachelor/Prog1/examples/ch07') diff --git a/Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp new file mode 100644 index 0000000..e2f9ee2 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Fig07_03.cpp @@ -0,0 +1,42 @@ +// Fig. 7.3: fig07_03.cpp +// Attempting to access a const object with +// non-const member functions. + +// include Time class definition from time5.h +#include "time5.h" + +int main() +{ + Time wakeUp( 6, 45, 0 ); // non-constant object + const Time noon( 12, 0, 0 ); // constant object + + // OBJECT MEMBER FUNCTION + wakeUp.setHour( 18 ); // non-const non-const + + noon.setHour( 12 ); // const non-const + + wakeUp.getHour(); // non-const const + + noon.getMinute(); // const const + noon.printUniversal(); // const const + + noon.printStandard(); // const non-const + + 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp new file mode 100644 index 0000000..a8e8a6b --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.cpp @@ -0,0 +1,107 @@ +// Fig. 7.2: time5.cpp +// Member-function definitions for class Time. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +// include definition of class Time from time5.h +#include "time5.h" + +// constructor function to initialize private data; +// calls member function setTime to set variables; +// default values are 0 (see class definition) +Time::Time( int hour, int minute, int second ) +{ + setTime( hour, minute, second ); + +} // end Time constructor + +// set hour, minute and second values +void Time::setTime( int hour, int minute, int second ) +{ + setHour( hour ); + setMinute( minute ); + setSecond( second ); + +} // 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() const +{ + return hour; + +} // end function getHour + +// return minute value +int Time::getMinute() const +{ + return minute; + +} // end function getMinute + +// return second value +int Time::getSecond() const +{ + return second; + +} // end function getSecond + +// print Time in universal format +void Time::printUniversal() const +{ + 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-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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h new file mode 100644 index 0000000..25b583e --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_01_03/Time5.h @@ -0,0 +1,49 @@ +// Fig. 7.1: time5.h +// Definition of class Time. +// Member functions defined in time5.cpp. +#ifndef TIME5_H +#define TIME5_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); // default constructor + + // set functions + void setTime( int, int, int ); // set time + void setHour( int ); // set hour + void setMinute( int ); // set minute + void setSecond( int ); // set second + + // get functions (normally declared const) + int getHour() const; // return hour + int getMinute() const; // return minute + int getSecond() const; // return second + + // print functions (normally declared const) + void printUniversal() const; // print universal time + void printStandard(); // print standard time + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +#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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp b/Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp new file mode 100644 index 0000000..62b4464 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_04/Fig07_04.cpp @@ -0,0 +1,75 @@ +// Fig. 7.4: fig07_04.cpp +// Using a member initializer to initialize a +// constant of a built-in data type. +#include + +using std::cout; +using std::endl; + +class Increment { + +public: + Increment( int c = 0, int i = 1 ); // default constructor + + void addIncrement() + { + count += increment; + + } // end function addIncrement + + void print() const; // prints count and increment + +private: + int count; + const int increment; // const data member + +}; // end class Increment + +// constructor +Increment::Increment( int c, int i ) + : count( c ), // initializer for non-const member + increment( i ) // required initializer for const member +{ + // empty body + +} // end constructor Increment + +// print count and increment values +void Increment::print() const +{ + cout << "count = " << count + << ", increment = " << increment << endl; + +} // end function print + +int main() +{ + Increment value( 10, 5 ); + + cout << "Before incrementing: "; + value.print(); + + for ( int j = 0; j < 3; j++ ) { + value.addIncrement(); + cout << "After increment " << j + 1 << ": "; + value.print(); + } + + 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp b/Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp new file mode 100644 index 0000000..f7945b4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_05/Fig07_05.cpp @@ -0,0 +1,74 @@ +// Fig. 7.5: fig07_05.cpp +// Attempting to initialize a constant of +// a built-in data type with an assignment. +#include + +using std::cout; +using std::endl; + +class Increment { + +public: + Increment( int c = 0, int i = 1 ); // default constructor + + void addIncrement() + { + count += increment; + + } // end function addIncrement + + void print() const; // prints count and increment + +private: + int count; + const int increment; // const data member + +}; // end class Increment + +// constructor +Increment::Increment( int c, int i ) +{ // Constant member 'increment' is not initialized + count = c; // allowed because count is not constant + increment = i; // ERROR: Cannot modify a const object + +} // end constructor Increment + +// print count and increment values +void Increment::print() const +{ + cout << "count = " << count + << ", increment = " << increment << endl; + +} // end function print + +int main() +{ + Increment value( 10, 5 ); + + cout << "Before incrementing: "; + value.print(); + + for ( int j = 0; j < 3; j++ ) { + value.addIncrement(); + cout << "After increment " << j + 1 << ": "; + value.print(); + } + + 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. * + *************************************************************************/ 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h new file mode 100644 index 0000000..5829c71 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Date1.h @@ -0,0 +1,39 @@ +// Fig. 7.6: date1.h +// Date class definition. +// Member functions defined in date1.cpp +#ifndef DATE1_H +#define DATE1_H + +class Date { + +public: + Date( int = 1, int = 1, int = 1900 ); // default constructor + void print() const; // print date in month/day/year format + ~Date(); // provided to confirm destruction order + +private: + int month; // 1-12 (January-December) + int day; // 1-31 based on month + int year; // any year + + // utility function to test proper day for month and year + int checkDay( int ) const; + +}; // 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp new file mode 100644 index 0000000..6cc742a --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.cpp @@ -0,0 +1,72 @@ +// Fig. 7.9: employee1.cpp +// Member-function definitions for class Employee. +#include + +using std::cout; +using std::endl; + +#include // strcpy and strlen prototypes + +#include "employee1.h" // Employee class definition +#include "date1.h" // Date class definition + +// constructor uses member initializer list to pass initializer +// values to constructors of member objects birthDate and +// hireDate [Note: This invokes the so-called "default copy +// constructor" which the C++ compiler provides implicitly.] +Employee::Employee( const char *first, const char *last, + const Date &dateOfBirth, const Date &dateOfHire ) + : birthDate( dateOfBirth ), // initialize birthDate + hireDate( dateOfHire ) // initialize hireDateba +{ + // copy first into firstName and be sure that it fits + int length = strlen( first ); + length = ( length < 25 ? length : 24 ); + strncpy( firstName, first, length ); + firstName[ length ] = '\0'; + + // copy last into lastName and be sure that it fits + length = strlen( last ); + length = ( length < 25 ? length : 24 ); + strncpy( lastName, last, length ); + lastName[ length ] = '\0'; + + // output Employee object to show when constructor is called + cout << "Employee object constructor: " + << firstName << ' ' << lastName << endl; + +} // end Employee constructor + +// print Employee object +void Employee::print() const +{ + cout << lastName << ", " << firstName << "\nHired: "; + hireDate.print(); + cout << " Birth date: "; + birthDate.print(); + cout << endl; + +} // end function print + +// output Employee object to show when its destructor is called +Employee::~Employee() +{ + cout << "Employee object destructor: " + << lastName << ", " << firstName << endl; + +} // end ~Employee destructor + +/************************************************************************** + * (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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h new file mode 100644 index 0000000..cfc294a --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Employee1.h @@ -0,0 +1,42 @@ +// Fig. 7.8: employee1.h +// Employee class definition. +// Member functions defined in employee1.cpp. +#ifndef EMPLY1_H +#define EMPLY1_H + +// include Date class definition from date1.h +#include "date1.h" + +class Employee { + +public: + Employee( + const char *, const char *, const Date &, const Date & ); + + void print() const; + ~Employee(); // provided to confirm destruction order + +private: + char firstName[ 25 ]; + char lastName[ 25 ]; + const Date birthDate; // composition: member object + const Date hireDate; // composition: member object + +}; // end class Employee + +#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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp new file mode 100644 index 0000000..90dfc9b --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_06_10/Fig07_10.cpp @@ -0,0 +1,40 @@ +// Fig. 7.10: fig07_10.cpp +// Demonstrating composition--an object with member objects. +#include + +using std::cout; +using std::endl; + +#include "employee1.h" // Employee class definition + +int main() +{ + Date birth( 7, 24, 1949 ); + Date hire( 3, 12, 1988 ); + Employee manager( "Bob", "Jones", birth, hire ); + + cout << '\n'; + manager.print(); + + cout << "\nTest Date constructor with invalid values:\n"; + Date lastDayOff( 14, 35, 1994 ); // invalid month and day + cout << 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp b/Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp new file mode 100644 index 0000000..995bc86 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_11/Fig07_11.cpp @@ -0,0 +1,72 @@ +// Fig. 7.11: fig07_11.cpp +// Friends can access private members of a class. +#include + +using std::cout; +using std::endl; + +// Count class definition +// (note that there is no friendship declaration) +class Count { + friend void setX( Count &, int ); // friend declaration + +public: + + // constructor + Count() + : x( 0 ) // initialize x to 0 + { + // empty body + + } // end constructor Count + + // output x + void print() const + { + cout << x << endl; + + } // end function print + +private: + int x; // data member + +}; // end class Count + +// function setX can modify private data of Count +// because setX is declared as a friend of Count +void setX( Count &c, int val ) +{ + c.x = val; // legal: setX is a friend of Count + +} // end function setX + +int main() +{ + Count counter; // create Count object + + cout << "counter.x after instantiation: "; + counter.print(); + + setX( counter, 8 ); // set x with a friend + + cout << "counter.x after call to setX friend function: "; + counter.print(); + + 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp b/Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp new file mode 100644 index 0000000..dbab9fc --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_12/Fig07_12.cpp @@ -0,0 +1,65 @@ +// Fig. 7.12: fig07_12.cpp +// Non-friend/non-member functions cannot access +// private data of a class. +#include + +using std::cout; +using std::endl; + +// Count class definition +class Count { + +public: + + // constructor + Count() + : x( 0 ) // initialize x to 0 + { + // empty body + + } // end constructor Count + + // output x + void print() const + { + cout << x << endl; + + } // end function print + +private: + int x; // data member + +}; // end class Count + +// function tries to modify private data of Count, +// but cannot because function is not a friend of Count +void cannotSetX( Count &c, int val ) +{ + c.x = val; // ERROR: cannot access private member in Count + +} // end function cannotSetX + +int main() +{ + Count counter; // create Count object + + cannotSetX( counter, 3 ); // cannotSetX is not a friend + + 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp b/Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp new file mode 100644 index 0000000..2e385a4 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_13/Fig07_13.cpp @@ -0,0 +1,66 @@ +// Fig. 7.13: fig07_13.cpp +// Using the this pointer to refer to object members. +#include + +using std::cout; +using std::endl; + +class Test { + +public: + Test( int = 0 ); // default constructor + void print() const; + +private: + int x; + +}; // end class Test + +// constructor +Test::Test( int value ) + : x( value ) // initialize x to value +{ + // empty body + +} // end constructor Test + +// print x using implicit and explicit this pointers; +// parentheses around *this required +void Test::print() const +{ + // implicitly use this pointer to access member x + cout << " x = " << x; + + // explicitly use this pointer to access member x + cout << "\n this->x = " << this->x; + + // explicitly use dereferenced this pointer and + // the dot operator to access member x + cout << "\n(*this).x = " << ( *this ).x << endl; + +} // end function print + +int main() +{ + Test testObject( 12 ); + + testObject.print(); + + 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp new file mode 100644 index 0000000..70e7a64 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Fig07_16.cpp @@ -0,0 +1,48 @@ +// Fig. 7.16: fig07_16.cpp +// Cascading member function calls with the this pointer. +#include + +using std::cout; +using std::endl; + +#include "time6.h" // Time class definition + +int main() +{ + Time t; + + // cascaded function calls + t.setHour( 18 ).setMinute( 30 ).setSecond( 22 ); + + // output time in universal and standard formats + cout << "Universal time: "; + t.printUniversal(); + + cout << "\nStandard time: "; + t.printStandard(); + + cout << "\n\nNew standard time: "; + + // cascaded function calls + t.setTime( 20, 20, 20 ).printStandard(); + + cout << 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp new file mode 100644 index 0000000..97205c0 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.cpp @@ -0,0 +1,114 @@ +// Fig. 7.15: time6.cpp +// Member-function definitions for Time class. +#include + +using std::cout; + +#include + +using std::setfill; +using std::setw; + +#include "time6.h" // Time class definition + +// 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 +Time &Time::setTime( int h, int m, int s ) +{ + setHour( h ); + setMinute( m ); + setSecond( s ); + + return *this; // enables cascading + +} // end function setTime + +// set hour value +Time &Time::setHour( int h ) +{ + hour = ( h >= 0 && h < 24 ) ? h : 0; + + return *this; // enables cascading + +} // end function setHour + +// set minute value +Time &Time::setMinute( int m ) +{ + minute = ( m >= 0 && m < 60 ) ? m : 0; + + return *this; // enables cascading + +} // end function setMinute + +// set second value +Time &Time::setSecond( int s ) +{ + second = ( s >= 0 && s < 60 ) ? s : 0; + + return *this; // enables cascading + +} // end function setSecond + +// get hour value +int Time::getHour() const +{ + return hour; + +} // end function getHour + +// get minute value +int Time::getMinute() const +{ + return minute; + +} // end function getMinute + +// get second value +int Time::getSecond() const +{ + return second; + +} // end function getSecond + +// print Time in universal format +void Time::printUniversal() const +{ + cout << setfill( '0' ) << setw( 2 ) << hour << ":" + << setw( 2 ) << minute << ":" + << setw( 2 ) << second; + +} // end function printUniversal + +// print Time in standard format +void Time::printStandard() const +{ + 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-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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h new file mode 100644 index 0000000..d9413ca --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_14_16/Time6.h @@ -0,0 +1,51 @@ +// Fig. 7.14: time6.h +// Cascading member function calls. + +// Time class definition. +// Member functions defined in time6.cpp. +#ifndef TIME6_H +#define TIME6_H + +class Time { + +public: + Time( int = 0, int = 0, int = 0 ); // default constructor + + // set functions + Time &setTime( int, int, int ); // set hour, minute, second + Time &setHour( int ); // set hour + Time &setMinute( int ); // set minute + Time &setSecond( int ); // set second + + // get functions (normally declared const) + int getHour() const; // return hour + int getMinute() const; // return minute + int getSecond() const; // return second + + // print functions (normally declared const) + void printUniversal() const; // print universal time + void printStandard() const; // print standard time + +private: + int hour; // 0 - 23 (24-hour clock format) + int minute; // 0 - 59 + int second; // 0 - 59 + +}; // end class Time + +#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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp b/Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp new file mode 100644 index 0000000..27ba9f7 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_17_19/Fig07_19.cpp @@ -0,0 +1,55 @@ +// Fig. 7.19: fig07_19.cpp +// Driver to test class Employee. +#include + +using std::cout; +using std::endl; + +#include // C++ standard new operator + +#include "employee2.h" // Employee class definition + +int main() +{ + cout << "Number of employees before instantiation is " + << Employee::getCount() << endl; // use class name + + Employee *e1Ptr = new Employee( "Susan", "Baker" ); + Employee *e2Ptr = new Employee( "Robert", "Jones" ); + + cout << "Number of employees after instantiation is " + << e1Ptr->getCount(); + + cout << "\n\nEmployee 1: " + << e1Ptr->getFirstName() + << " " << e1Ptr->getLastName() + << "\nEmployee 2: " + << e2Ptr->getFirstName() + << " " << e2Ptr->getLastName() << "\n\n"; + + delete e1Ptr; // recapture memory + e1Ptr = 0; // disconnect pointer from free-store space + delete e2Ptr; // recapture memory + e2Ptr = 0; // disconnect pointer from free-store space + + cout << "Number of employees after deletion is " + << Employee::getCount() << 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp new file mode 100644 index 0000000..540f367 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.cpp @@ -0,0 +1,88 @@ +// Fig. 7.18: employee2.cpp +// Member-function definitions for class Employee. +#include + +using std::cout; +using std::endl; + +#include // C++ standard new operator +#include // strcpy and strlen prototypes + +#include "employee2.h" // Employee class definition + +// define and initialize static data member +int Employee::count = 0; + +// define static member function that returns number of +// Employee objects instantiated +int Employee::getCount() +{ + return count; + +} // end static function getCount + +// constructor dynamically allocates space for +// first and last name and uses strcpy to copy +// first and last names into the object +Employee::Employee( const char *first, const char *last ) +{ + firstName = new char[ strlen( first ) + 1 ]; + strcpy( firstName, first ); + + lastName = new char[ strlen( last ) + 1 ]; + strcpy( lastName, last ); + + ++count; // increment static count of employees + + cout << "Employee constructor for " << firstName + << ' ' << lastName << " called." << endl; + +} // end Employee constructor + +// destructor deallocates dynamically allocated memory +Employee::~Employee() +{ + cout << "~Employee() called for " << firstName + << ' ' << lastName << endl; + + delete [] firstName; // recapture memory + delete [] lastName; // recapture memory + + --count; // decrement static count of employees + +} // end ~Employee destructor + +// return first name of employee +const char *Employee::getFirstName() const +{ + // const before return type prevents client from modifying + // private data; client should copy returned string before + // destructor deletes storage to prevent undefined pointer + return firstName; + +} // end function getFirstName + +// return last name of employee +const char *Employee::getLastName() const +{ + // const before return type prevents client from modifying + // private data; client should copy returned string before + // destructor deletes storage to prevent undefined pointer + return lastName; + +} // end function getLastName + +/************************************************************************** + * (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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h new file mode 100644 index 0000000..3010210 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_17_19/employee2.h @@ -0,0 +1,41 @@ +// Fig. 7.17: employee2.h +// Employee class definition. +#ifndef EMPLOYEE2_H +#define EMPLOYEE2_H + +class Employee { + +public: + Employee( const char *, const char * ); // constructor + ~Employee(); // destructor + const char *getFirstName() const; // return first name + const char *getLastName() const; // return last name + + // static member function + static int getCount(); // return # objects instantiated + +private: + char *firstName; + char *lastName; + + // static data member + static int count; // number of objects instantiated + +}; // end class Employee + +#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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp b/Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp new file mode 100644 index 0000000..a11a543 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/fig07_23.cpp @@ -0,0 +1,39 @@ +// Fig. 7.23: fig07_23.cpp +// Hiding a class’s private data with a proxy class. +#include + +using std::cout; +using std::endl; + +#include "interface.h" // Interface class definition + +int main() +{ + Interface i( 5 ); + + cout << "Interface contains: " << i.getValue() + << " before setValue" << endl; + + i.setValue( 10 ); + + cout << "Interface contains: " << i.getValue() + << " after setValue" << 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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h b/Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h new file mode 100644 index 0000000..1630a50 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/implementation.h @@ -0,0 +1,48 @@ +// Fig. 7.20: implementation.h +// Header file for class Implementation + +class Implementation { + +public: + + // constructor + Implementation( int v ) + : value( v ) // initialize value with v + { + // empty body + + } // end constructor Implementation + + // set value to v + void setValue( int v ) + { + value = v; // should validate v + + } // end function setValue + + // return value + int getValue() const + { + return value; + + } // end function getValue + +private: + int value; + +}; // end class Implementation + +/************************************************************************** + * (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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp new file mode 100644 index 0000000..0f895fd --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.cpp @@ -0,0 +1,48 @@ +// Fig. 7.22: interface.cpp +// Definition of class Interface +#include "interface.h" // Interface class definition +#include "implementation.h" // Implementation class definition + +// constructor +Interface::Interface( int v ) + : ptr ( new Implementation( v ) ) // initialize ptr +{ + // empty body + +} // end Interface constructor + +// call Implementation's setValue function +void Interface::setValue( int v ) +{ + ptr->setValue( v ); + +} // end function setValue + +// call Implementation's getValue function +int Interface::getValue() const +{ + return ptr->getValue(); + +} // end function getValue + +// destructor +Interface::~Interface() +{ + delete ptr; + +} // end ~Interface destructor + +/************************************************************************** + * (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. * + *************************************************************************/ diff --git a/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h new file mode 100644 index 0000000..5d229e1 --- /dev/null +++ b/Bachelor/Prog1/examples/ch07/Fig07_20_23/interface.h @@ -0,0 +1,34 @@ +// Fig. 7.21: interface.h +// Header file for interface.cpp + +class Implementation; // forward class declaration + +class Interface { + +public: + Interface( int ); + void setValue( int ); // same public interface as + int getValue() const; // class Implementation + ~Interface(); + +private: + + // requires previous forward declaration (line 4) + Implementation *ptr; + +}; // end class Interface + +/************************************************************************** + * (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