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 --- .../1_ch09/Fig09_10_11/CircleTest/circle2.cpp | 74 +++++ .../1_ch09/Fig09_10_11/CircleTest/circle2.h | 44 +++ .../1_ch09/Fig09_10_11/CircleTest/point.cpp | 65 ++++ .../1_ch09/Fig09_10_11/CircleTest/point.h | 40 +++ .../1_ch09/Fig09_12_16/CircleTest3/circle3.cpp | 74 +++++ .../1_ch09/Fig09_12_16/CircleTest3/circle3.h | 44 +++ .../1_ch09/Fig09_12_16/CircleTest3/circletest3.cpp | 63 ++++ .../1_ch09/Fig09_12_16/CircleTest3/point2.cpp | 65 ++++ .../1_ch09/Fig09_12_16/CircleTest3/point2.h | 40 +++ .../ConstructorsAndDestructors/circle5.cpp | 88 ++++++ .../ConstructorsAndDestructors/circle5.h | 45 +++ .../ConstructorsAndDestructors/fig09_29.cpp | 44 +++ .../ConstructorsAndDestructors/point4.cpp | 77 +++++ .../ConstructorsAndDestructors/point4.h | 41 +++ .../elevatorinheritance/ElevatorSimulation.cpp | 45 +++ .../1_ch09/elevatorinheritance/bell.cpp | 44 +++ .../1_ch09/elevatorinheritance/bell.h | 30 ++ .../1_ch09/elevatorinheritance/building.cpp | 65 ++++ .../1_ch09/elevatorinheritance/building.h | 42 +++ .../1_ch09/elevatorinheritance/button.cpp | 52 ++++ .../1_ch09/elevatorinheritance/button.h | 41 +++ .../1_ch09/elevatorinheritance/clock.cpp | 52 ++++ .../1_ch09/elevatorinheritance/clock.h | 34 +++ .../1_ch09/elevatorinheritance/door.cpp | 81 +++++ .../1_ch09/elevatorinheritance/door.h | 40 +++ .../1_ch09/elevatorinheritance/elevator.cpp | 231 ++++++++++++++ .../1_ch09/elevatorinheritance/elevator.h | 75 +++++ .../1_ch09/elevatorinheritance/elevatorButton.cpp | 49 +++ .../1_ch09/elevatorinheritance/elevatorButton.h | 32 ++ .../1_ch09/elevatorinheritance/floor.cpp | 98 ++++++ .../1_ch09/elevatorinheritance/floor.h | 63 ++++ .../1_ch09/elevatorinheritance/floorButton.cpp | 54 ++++ .../1_ch09/elevatorinheritance/floorButton.h | 35 +++ .../1_ch09/elevatorinheritance/light.cpp | 64 ++++ .../1_ch09/elevatorinheritance/light.h | 35 +++ .../1_ch09/elevatorinheritance/person.cpp | 91 ++++++ .../1_ch09/elevatorinheritance/person.h | 42 +++ .../1_ch09/elevatorinheritance/scheduler.cpp | 133 +++++++++ .../1_ch09/elevatorinheritance/scheduler.h | 53 ++++ .../1_ch09/fig09_04_06/PointTest/point.cpp | 65 ++++ .../1_ch09/fig09_04_06/PointTest/point.h | 40 +++ .../1_ch09/fig09_04_06/PointTest/pointtest.cpp | 43 +++ .../1_ch09/fig09_07_09/CircleTest/circle.cpp | 102 +++++++ .../1_ch09/fig09_07_09/CircleTest/circle.h | 50 ++++ .../1_ch09/fig09_07_09/CircleTest/circletest.cpp | 63 ++++ .../1_ch09/fig09_17_21/CircleTest4/circle4.cpp | 74 +++++ .../1_ch09/fig09_17_21/CircleTest4/circle4.h | 44 +++ .../1_ch09/fig09_17_21/CircleTest4/circletest4.cpp | 63 ++++ .../1_ch09/fig09_17_21/CircleTest4/point3.cpp | 65 ++++ .../1_ch09/fig09_17_21/CircleTest4/point3.h | 40 +++ .../1_ch09/fig09_22_24/CylinderTest/circle4.cpp | 74 +++++ .../1_ch09/fig09_22_24/CylinderTest/circle4.h | 44 +++ .../1_ch09/fig09_22_24/CylinderTest/cylinder.cpp | 68 +++++ .../1_ch09/fig09_22_24/CylinderTest/cylinder.h | 42 +++ .../fig09_22_24/CylinderTest/cylindertest.cpp | 70 +++++ .../1_ch09/fig09_22_24/CylinderTest/point3.cpp | 65 ++++ .../1_ch09/fig09_22_24/CylinderTest/point3.h | 40 +++ .../Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp | 75 +++++ .../Codebeispiele/2_ch10/fig10_01_05/circle.cpp | 74 +++++ .../Codebeispiele/2_ch10/fig10_01_05/circle.h | 44 +++ .../Codebeispiele/2_ch10/fig10_01_05/point.cpp | 65 ++++ .../Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h | 40 +++ .../Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp | 31 ++ .../Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp | 74 +++++ .../Prog2/Codebeispiele/2_ch10/fig10_06/circle.h | 44 +++ .../Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp | 65 ++++ .../Prog2/Codebeispiele/2_ch10/fig10_06/point.h | 40 +++ .../Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp | 74 +++++ .../Prog2/Codebeispiele/2_ch10/fig10_07/circle.h | 44 +++ .../Codebeispiele/2_ch10/fig10_07/fig10_07.cpp | 48 +++ .../Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp | 65 ++++ .../Prog2/Codebeispiele/2_ch10/fig10_07/point.h | 40 +++ .../Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp | 80 +++++ .../Codebeispiele/2_ch10/fig10_08_12/circle.cpp | 74 +++++ .../Codebeispiele/2_ch10/fig10_08_12/circle.h | 44 +++ .../Codebeispiele/2_ch10/fig10_08_12/point.cpp | 65 ++++ .../Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h | 40 +++ .../Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp | 120 ++++++++ .../Codebeispiele/2_ch10/fig10_13_20/circle.cpp | 81 +++++ .../Codebeispiele/2_ch10/fig10_13_20/circle.h | 47 +++ .../Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp | 75 +++++ .../Codebeispiele/2_ch10/fig10_13_20/cylinder.h | 46 +++ .../Codebeispiele/2_ch10/fig10_13_20/point.cpp | 72 +++++ .../Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h | 45 +++ .../Codebeispiele/2_ch10/fig10_13_20/shape.cpp | 36 +++ .../Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h | 41 +++ .../Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp | 65 ++++ .../Codebeispiele/2_ch10/fig10_23_33/baseplus.h | 40 +++ .../2_ch10/fig10_23_33/commission.cpp | 76 +++++ .../Codebeispiele/2_ch10/fig10_23_33/commission.h | 44 +++ .../Codebeispiele/2_ch10/fig10_23_33/employee.cpp | 86 ++++++ .../Codebeispiele/2_ch10/fig10_23_33/employee.h | 50 ++++ .../Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp | 100 +++++++ .../Codebeispiele/2_ch10/fig10_23_33/hourly.cpp | 80 +++++ .../Codebeispiele/2_ch10/fig10_23_33/hourly.h | 44 +++ .../Codebeispiele/2_ch10/fig10_23_33/salaried.cpp | 61 ++++ .../Codebeispiele/2_ch10/fig10_23_33/salaried.h | 40 +++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP | 68 +++++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP | 97 ++++++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP | 38 +++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP | 43 +++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP | 75 +++++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP | 71 +++++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP | 69 +++++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP | 50 ++++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP | 57 ++++ Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP | 44 +++ Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp | 59 ++++ Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp | 65 ++++ Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp | 61 ++++ Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp | 78 +++++ Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp | 163 ++++++++++ Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp | 53 ++++ Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp | 94 ++++++ Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp | 91 ++++++ Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp | 331 +++++++++++++++++++++ Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp | 105 +++++++ Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h | 56 ++++ Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h | 98 ++++++ Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp | 59 ++++ Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp | 67 +++++ Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp | 63 ++++ .../Codebeispiele/6_ch17/fig17_03_05/fig17_05.cpp | 105 +++++++ .../Prog2/Codebeispiele/6_ch17/fig17_03_05/list.h | 216 ++++++++++++++ .../Codebeispiele/6_ch17/fig17_03_05/listnode.h | 56 ++++ .../Codebeispiele/6_ch17/fig17_10_11/fig17_11.cpp | 72 +++++ .../Prog2/Codebeispiele/6_ch17/fig17_10_11/list.h | 216 ++++++++++++++ .../Codebeispiele/6_ch17/fig17_10_11/listnode.h | 56 ++++ .../Prog2/Codebeispiele/6_ch17/fig17_10_11/stack.h | 57 ++++ .../Codebeispiele/6_ch17/fig17_12/fig17_12test.cpp | 69 +++++ .../Prog2/Codebeispiele/6_ch17/fig17_12/list.h | 216 ++++++++++++++ .../Prog2/Codebeispiele/6_ch17/fig17_12/listnode.h | 56 ++++ .../6_ch17/fig17_12/stackcomposition.h | 62 ++++ .../Codebeispiele/6_ch17/fig17_13_14/fig17_14.cpp | 72 +++++ .../Prog2/Codebeispiele/6_ch17/fig17_13_14/list.h | 216 ++++++++++++++ .../Codebeispiele/6_ch17/fig17_13_14/listnode.h | 56 ++++ .../Prog2/Codebeispiele/6_ch17/fig17_13_14/queue.h | 57 ++++ .../6_ch17/fig17_13_14/queuecomposition.h | 59 ++++ .../Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h | 159 ++++++++++ .../Codebeispiele/6_ch17/fig17_17_19/Treenode.h | 54 ++++ .../Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp | 76 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP | 46 +++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP | 85 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP | 86 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP | 145 +++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP | 57 ++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP | 90 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP | 68 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP | 62 ++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP | 68 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP | 74 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP | 46 +++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP | 46 +++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP | 73 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp | 86 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp | 102 +++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP | 99 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP | 116 ++++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP | 97 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP | 60 ++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP | 79 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP | 69 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP | 94 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP | 96 ++++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP | 79 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp | 38 +++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP | 82 +++++ Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp | 86 ++++++ Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp | 89 ++++++ Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp | 71 +++++ Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp | 63 ++++ Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp | 49 +++ Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp | 54 ++++ Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp | 53 ++++ Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp | 86 ++++++ Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp | 35 +++ Bachelor/Prog2/Codebeispiele/Student/Student.cpp | 49 +++ Bachelor/Prog2/Codebeispiele/Student/Student.h | 26 ++ Bachelor/Prog2/Codebeispiele/ex9_21.zip | Bin 0 -> 183012 bytes 179 files changed, 12501 insertions(+) create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circletest3.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/fig09_29.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/ElevatorSimulation.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/pointtest.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circletest.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circletest4.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.h create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylindertest.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h create mode 100644 Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h create mode 100644 Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/fig17_05.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/list.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/listnode.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/fig17_11.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/list.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/listnode.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/stack.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/fig17_12test.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/list.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/listnode.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/stackcomposition.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/fig17_14.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/list.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/listnode.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queue.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queuecomposition.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h create mode 100644 Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP create mode 100644 Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/Student/Student.cpp create mode 100644 Bachelor/Prog2/Codebeispiele/Student/Student.h create mode 100644 Bachelor/Prog2/Codebeispiele/ex9_21.zip (limited to 'Bachelor/Prog2/Codebeispiele') diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.cpp new file mode 100644 index 0000000..3f7084f --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.cpp @@ -0,0 +1,74 @@ +// Fig. 9.11: circle2.cpp +// Circle2 class member-function definitions. +#include + +using std::cout; + +#include "circle2.h" // Circle2 class definition + +// default constructor +Circle2::Circle2( int xValue, int yValue, double radiusValue ) +{ + x = xValue; + y = yValue; + setRadius( radiusValue ); + +} // end Circle2 constructor + +// set radius +void Circle2::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle2::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle2::getDiameter() const +{ + return 2 * radius; + +} // end function getDiameter + +// calculate and return circumference +double Circle2::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle2::getArea() const +{ + return 3.14159 * radius * radius; + +} // end function getArea + +// output Circle2 object +void Circle2::print() const +{ + cout << "Center = [" << x << ", " << y << ']' + << "; Radius = " << radius; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.h b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.h new file mode 100644 index 0000000..0fd592d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/circle2.h @@ -0,0 +1,44 @@ +// Fig. 9.10: circle2.h +// Circle2 class contains x-y coordinate pair and radius. +#ifndef CIRCLE2_H +#define CIRCLE2_H + +#include "point.h" // Point class definition + +class Circle2 : public Point { + +public: + + // default constructor + Circle2( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle2 object + +private: + double radius; // Circle2's radius + +}; // end class Circle2 + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.cpp new file mode 100644 index 0000000..38261da --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.cpp @@ -0,0 +1,65 @@ +// Fig. 9.5: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) +{ + x = xValue; + y = yValue; + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << x << ", " << y << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.h b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.h new file mode 100644 index 0000000..7593775 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_10_11/CircleTest/point.h @@ -0,0 +1,40 @@ +// Fig. 9.4: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.cpp new file mode 100644 index 0000000..24a1f67 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.cpp @@ -0,0 +1,74 @@ +// Fig. 9.15: circle3.cpp +// Circle3 class member-function definitions. +#include + +using std::cout; + +#include "circle3.h" // Circle3 class definition + +// default constructor +Circle3::Circle3( int xValue, int yValue, double radiusValue ) +{ + x = xValue; + y = yValue; + setRadius( radiusValue ); + +} // end Circle3 constructor + +// set radius +void Circle3::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle3::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle3::getDiameter() const +{ + return 2 * radius; + +} // end function getDiameter + +// calculate and return circumference +double Circle3::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle3::getArea() const +{ + return 3.14159 * radius * radius; + +} // end function getArea + +// output Circle3 object +void Circle3::print() const +{ + cout << "Center = [" << x << ", " << y << ']' + << "; Radius = " << radius; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.h b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.h new file mode 100644 index 0000000..137a2bd --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circle3.h @@ -0,0 +1,44 @@ +// Fig. 9.14: circle3.h +// Circle3 class contains x-y coordinate pair and radius. +#ifndef CIRCLE3_H +#define CIRCLE3_H + +#include "point2.h" // Point2 class definition + +class Circle3 : public Point2 { + +public: + + // default constructor + Circle3( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle3 object + +private: + double radius; // Circle3's radius + +}; // end class Circle3 + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circletest3.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circletest3.cpp new file mode 100644 index 0000000..6f3a598 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/circletest3.cpp @@ -0,0 +1,63 @@ +// Fig. 9.16: circletest3.cpp +// Testing class Circle3. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "circle3.h" // Circle3 class definition + +int main() +{ + Circle3 circle( 37, 43, 2.5 ); // instantiate Circle3 object + + // display point coordinates + cout << "X coordinate is " << circle.getX() + << "\nY coordinate is " << circle.getY() + << "\nRadius is " << circle.getRadius(); + + circle.setX( 2 ); // set new x-coordinate + circle.setY( 2 ); // set new y-coordinate + circle.setRadius( 4.25 ); // set new radius + + // display new point value + cout << "\n\nThe new location and radius of circle are\n"; + circle.print(); + + // display floating-point values with 2 digits of precision + cout << fixed << setprecision( 2 ); + + // display Circle3's diameter + cout << "\nDiameter is " << circle.getDiameter(); + + // display Circle3's circumference + cout << "\nCircumference is " << circle.getCircumference(); + + // display Circle3's area + cout << "\nArea is " << circle.getArea(); + + cout << endl; + + return 0; // indicates successful termination + +} // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.cpp new file mode 100644 index 0000000..27b014a --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.cpp @@ -0,0 +1,65 @@ +// Fig. 9.13: point2.cpp +// Point2 class member-function definitions. +#include + +using std::cout; + +#include "point2.h" // Point2 class definition + +// default constructor +Point2::Point2( int xValue, int yValue ) +{ + x = xValue; + y = yValue; + +} // end Point2 constructor + +// set x in coordinate pair +void Point2::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point2::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point2::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point2::getY() const +{ + return y; + +} // end function getY + +// output Point2 object +void Point2::print() const +{ + cout << '[' << x << ", " << y << ']'; + +} // end function print + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.h b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.h new file mode 100644 index 0000000..5b81781 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_12_16/CircleTest3/point2.h @@ -0,0 +1,40 @@ +// Fig. 9.12: point2.h +// Point2 class definition represents an x-y coordinate pair. +#ifndef POINT2_H +#define POINT2_H + +class Point2 { + +public: + Point2( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point2 object + +protected: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point2 + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.cpp new file mode 100644 index 0000000..d37278f --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.cpp @@ -0,0 +1,88 @@ +// Fig. 9.28: circle5.cpp +// Circle5 class member-function definitions. +#include + +using std::cout; +using std::endl; + +#include "circle5.h" // Circle5 class definition + +// default constructor +Circle5::Circle5( int xValue, int yValue, double radiusValue ) + : Point4( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + + cout << "Circle5 constructor: "; + print(); + cout << endl; + +} // end Circle5 constructor + +// destructor +Circle5::~Circle5() +{ + cout << "Circle5 destructor: "; + print(); + cout << endl; + +} // end Circle5 destructor + +// set radius +void Circle5::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle5::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle5::getDiameter() const +{ + return 2 * getRadius(); + +} // end function getDiameter + +// calculate and return circumference +double Circle5::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle5::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle5 object +void Circle5::print() const +{ + cout << "Center = "; + Point4::print(); // invoke Point4's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.h b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.h new file mode 100644 index 0000000..e2d8e78 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/circle5.h @@ -0,0 +1,45 @@ +// Fig. 9.27: circle5.h +// Circle5 class contains x-y coordinate pair and radius. +#ifndef CIRCLE5_H +#define CIRCLE5_H + +#include "point4.h" // Point4 class definition + +class Circle5 : public Point4 { + +public: + + // default constructor + Circle5( int = 0, int = 0, double = 0.0 ); + + ~Circle5(); // destructor + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle5 object + +private: + double radius; // Circle5's radius + +}; // end class Circle5 + +#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/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/fig09_29.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/fig09_29.cpp new file mode 100644 index 0000000..634348a --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/fig09_29.cpp @@ -0,0 +1,44 @@ +// Fig. 9.29: fig09_29.cpp +// Display order in which base-class and derived-class +// constructors are called. +#include + +using std::cout; +using std::endl; + +#include "circle5.h" // Circle5 class definition + +int main() +{ + { // begin new scope + + Point4 point( 11, 22 ); + + } // end scope + + cout << endl; + Circle5 circle1( 72, 29, 4.5 ); + + cout << endl; + Circle5 circle2( 5, 5, 10 ); + + cout << endl; + + return 0; // indicates successful termination + +} // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.cpp new file mode 100644 index 0000000..8538b6e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.cpp @@ -0,0 +1,77 @@ +// Fig. 9.26: point4.cpp +// Point4 class member-function definitions. +#include + +using std::cout; +using std::endl; + +#include "point4.h" // Point4 class definition + +// default constructor +Point4::Point4( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + cout << "Point4 constructor: "; + print(); + cout << endl; + +} // end Point4 constructor + +// destructor +Point4::~Point4() +{ + cout << "Point4 destructor: "; + print(); + cout << endl; + +} // end Point4 destructor + +// set x in coordinate pair +void Point4::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point4::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point4::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point4::getY() const +{ + return y; + +} // end function getY + +// output Point4 object +void Point4::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.h b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.h new file mode 100644 index 0000000..bcb87c3 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/Fig09_25_29/ConstructorsAndDestructors/point4.h @@ -0,0 +1,41 @@ +// Fig. 9.25: point4.h +// Point4 class definition represents an x-y coordinate pair. +#ifndef POINT4_H +#define POINT4_H + +class Point4 { + +public: + Point4( int = 0, int = 0 ); // default constructor + ~Point4(); // destructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point3 object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point4 + +#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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/ElevatorSimulation.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/ElevatorSimulation.cpp new file mode 100644 index 0000000..e50e422 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/ElevatorSimulation.cpp @@ -0,0 +1,45 @@ +// Fig. 7.24: elevatorSimulation.cpp +// Driver for the simulation. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "building.h" // Building class definition + +int main() +{ + int duration; // length of simulation in seconds + + cout << "Enter run time: "; + cin >> duration; + cin.ignore(); // ignore return char + + Building building; // create the building + + cout << endl << "*** ELEVATOR SIMULATION BEGINS ***" + << endl << endl; + + building.runSimulation( duration ); // start simulation + + cout << "*** ELEVATOR SIMULATION ENDS ***" << 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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.cpp new file mode 100644 index 0000000..2c97771 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.cpp @@ -0,0 +1,44 @@ +// Fig. 7.32: bell.cpp +// Member-function definitions for class Bell. +#include + +using std::cout; +using std::endl; + +#include "bell.h" // Bell class definition + +// constructor +Bell::Bell() +{ + cout << "bell created" << endl; + +} // end Bell constructor + +// destructor +Bell::~Bell() +{ + cout << "bell destroyed" << endl; + +} // end ~Bell destructor + +// ring bell +void Bell::ringBell() const +{ + cout << "elevator rings its bell" << endl; + +} // end function ringBell + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.h new file mode 100644 index 0000000..7b8a9c3 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/bell.h @@ -0,0 +1,30 @@ +// Fig. 7.31: bell.h +// Bell class definition. +#ifndef BELL_H +#define BELL_H + +class Bell { + +public: + Bell(); // constructor + ~Bell(); // destructor + void ringBell() const; // ring the bell + +}; // end class Bell + +#endif // BELL_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.cpp new file mode 100644 index 0000000..630cc91 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.cpp @@ -0,0 +1,65 @@ +// Fig. 7.26: building.cpp +// Member-function definitions for class Building. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "building.h" // Building class definition + +// constructor +Building::Building() + : floor1( Floor::FLOOR1, elevator ), + floor2( Floor::FLOOR2, elevator ), + elevator( floor1, floor2 ), + scheduler( floor1, floor2 ) +{ + cout << "building created" << endl; + +} // end Building constructor + +// destructor +Building::~Building() +{ + cout << "building destroyed" << endl; + +} // end ~Building destructor + +// function to control simulation +void Building::runSimulation( int totalTime ) +{ + int currentTime = 0; + + while ( currentTime < totalTime ) { + clock.tick(); // increment time + currentTime = clock.getTime(); // get new time + cout << "TIME: " << currentTime << endl; + + // process person arrivals for currentTime + scheduler.processTime( currentTime ); + + // process elevator events for currentTime + elevator.processTime( currentTime ); + + // wait for Enter key press, so user can view output + cin.get(); + + } // end while + +} // end function runSimulation + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.h new file mode 100644 index 0000000..5d47f07 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/building.h @@ -0,0 +1,42 @@ +// Fig. 7.25: building.h +// Building class definition. +#ifndef BUILDING_H +#define BUILDING_H + +#include "elevator.h" // Elevator class definition +#include "floor.h" // Floor class definition +#include "clock.h" // Clock class definition +#include "scheduler.h" // Scheduler class definition + +class Building { + +public: + Building(); // constructor + ~Building(); // destructor + void runSimulation( int ); // controls simulation + +private: + Floor floor1; // floor1 object + Floor floor2; // floor2 object + Elevator elevator; // elevator object + Clock clock; // clock object + Scheduler scheduler; // scheduler object + +}; // end class Building + +#endif // BUILDING_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.cpp new file mode 100644 index 0000000..2195a4f --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.cpp @@ -0,0 +1,52 @@ +// Fig. 9.34: button.cpp +// Member function definitions for class Button. +#include + +using std::cout; +using std::endl; + +#include "button.h" // Button class definition + +// constructor +Button::Button( Elevator &elevatorHandle ) + : elevatorRef( elevatorHandle ), pressed( false ) +{ + cout << "button created" << endl; + +} // end Button constructor + +// destructor +Button::~Button() +{ + cout << "button destroyed" << endl; + +} // end Button destructor + +// press button +void Button::pressButton() +{ + pressed = true; + +} // end function pressButton + +// reset button +void Button::resetButton() +{ + pressed = false; + +} // end function resetButton + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.h new file mode 100644 index 0000000..fbc0f94 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/button.h @@ -0,0 +1,41 @@ +// Fig. 9.33: button.h +// Definition for class Button. +#ifndef BUTTON_H +#define BUTTON_H + +class Elevator; // forward declaration + +class Button { + +public: + Button( Elevator & ); // constructor + ~Button(); // destructor + void pressButton(); // sets button on + void resetButton(); // resets button off + +protected: + + // reference to button's elevator + Elevator &elevatorRef; + +private: + bool pressed; // state of button + +}; // end class Button + +#endif // BUTTON_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.cpp new file mode 100644 index 0000000..725a189 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.cpp @@ -0,0 +1,52 @@ +// Fig. 7.28: clock.cpp +// Member-function definitions for class Clock. +#include + +using std::cout; +using std::endl; + +#include "clock.h" // Clock class definition + +// constructor +Clock::Clock() + : time( 0 ) // initialize time to 0 +{ + cout << "clock created" << endl; + +} // end Clock constructor + +// destructor +Clock::~Clock() +{ + cout << "clock destroyed" << endl; + +} // end ~Clock destructor + +// increment time by 1 +void Clock::tick() +{ + time++; + +} // end function tick + +// return current time +int Clock::getTime() const +{ + return time; + +} // end function getTime + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.h new file mode 100644 index 0000000..de97814 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/clock.h @@ -0,0 +1,34 @@ +// Fig. 7.27: clock.h +// Clock class definition. +#ifndef CLOCK_H +#define CLOCK_H + +class Clock { + +public: + Clock(); // constructor + ~Clock(); // destructor + void tick(); // increment clock by one second + int getTime() const; // returns clock's current time + +private: + int time; // clock's time + +}; // end class Clock + +#endif // CLOCK_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.cpp new file mode 100644 index 0000000..7adbe37 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.cpp @@ -0,0 +1,81 @@ +// Fig. 7.36: door.cpp +// Member-function definitions for class Door. +#include + +using std::cout; +using std::endl; + +#include "door.h" // Door class definition +#include "person.h" // Person class definition +#include "floor.h" // Floor class definition +#include "elevator.h" // Elevator class definition + +// constructor +Door::Door() + : open( false ) // initialize open to false +{ + cout << "door created" << endl; + +} // end Door constructor + +// destructor +Door::~Door() +{ + cout << "door destroyed" << endl; + +} // end ~Door destructor + +// open the door +void Door::openDoor( Person * const passengerPtr, + Person * const nextPassengerPtr, Floor ¤tFloor, + Elevator &elevator ) +{ + if ( !open ) { // if door is not open, open door + open = true; + + cout << "elevator opens its door on floor " + << currentFloor.getNumber() << endl; + + // if passenger is in elevator, tell person to leave + if ( passengerPtr != 0 ) { + passengerPtr->exitElevator( currentFloor, elevator ); + delete passengerPtr; // passenger leaves simulation + + } // end if + + // if passenger waiting to enter elevator, + // tell passenger to enter + if ( nextPassengerPtr != 0 ) + nextPassengerPtr->enterElevator( + elevator, currentFloor ); + + } // end outer if + +} // end function openDoor + +// close the door +void Door::closeDoor( const Floor ¤tFloor ) +{ + if ( open ) { // if door is open, close door + open = false; + cout << "elevator closes its door on floor " + << currentFloor.getNumber() << endl; + + } // end if + +} // end function closeDoor + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.h new file mode 100644 index 0000000..05c7b3e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/door.h @@ -0,0 +1,40 @@ +// Fig. 7.35: door.h +// Door class definition. +#ifndef DOOR_H +#define DOOR_H + +class Person; // forward declaration +class Floor; // forward declaration +class Elevator; // forward declaration + +class Door { + +public: + Door(); // constructor + ~Door(); // destructor + + void openDoor( Person * const, // opens door + Person * const, Floor &, Elevator & ); + void closeDoor( const Floor & ); // closes door + +private: + bool open; // open or closed + +}; + +#endif // DOOR_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.cpp new file mode 100644 index 0000000..26e01f4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.cpp @@ -0,0 +1,231 @@ +// Fig. 7.42: elevator.cpp +// Member-function definitions for class Elevator. +#include + +using std::cout; +using std::endl; + +#include "elevator.h" // Elevator class definition +#include "person.h" // Person class definition +#include "floor.h" // Floor class definition + +// static constants that represent time required to travel +// between floors and directions of the elevator +const int Elevator::ELEVATOR_TRAVEL_TIME = 5; +const int Elevator::UP = 0; +const int Elevator::DOWN = 1; + +// constructor +Elevator::Elevator( Floor &firstFloor, Floor &secondFloor ) + : elevatorButton( *this ), + currentBuildingClockTime( 0 ), + moving( false ), + direction( UP ), + currentFloor( Floor::FLOOR1 ), + arrivalTime( 0 ), + floor1NeedsService( false ), + floor2NeedsService( false ), + floor1Ref( firstFloor ), + floor2Ref( secondFloor ), + passengerPtr( 0 ) +{ + cout << "elevator created" << endl; + +} // end Elevator constructor + +// destructor +Elevator::~Elevator() +{ + delete passengerPtr; + cout << "elevator destroyed" << endl; + +} // end ~Elevator destructor + +// give time to elevator +void Elevator::processTime( int time ) +{ + currentBuildingClockTime = time; + + if ( moving ) // elevator is moving + processPossibleArrival(); + + else // elevator is not moving + processPossibleDeparture(); + + if ( !moving ) + cout << "elevator at rest on floor " + << currentFloor << endl; + +} // end function processTime + +// when elevator is moving, determine if it should stop +void Elevator::processPossibleArrival() +{ + // if elevator arrives at destination floor + if ( currentBuildingClockTime == arrivalTime ) { + + currentFloor = // update current floor + ( currentFloor == Floor::FLOOR1 ? + Floor::FLOOR2 : Floor::FLOOR1 ); + + direction = // update direction + ( currentFloor == Floor::FLOOR1 ? UP : DOWN ); + + cout << "elevator arrives on floor " + << currentFloor << endl; + + // process arrival at currentFloor + arriveAtFloor( currentFloor == Floor::FLOOR1 ? + floor1Ref : floor2Ref ); + + return; + + } // end if + + // elevator still moving + cout << "elevator moving " + << ( direction == UP ? "up" : "down" ) << endl; + +} // end function processPossibleArrival + +// determine whether elevator should move +void Elevator::processPossibleDeparture() +{ + // this floor needs service? + bool currentFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor1NeedsService : floor2NeedsService; + + // other floor needs service? + bool otherFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor2NeedsService : floor1NeedsService; + + // service this floor (if needed) + if ( currentFloorNeedsService ) { + arriveAtFloor( currentFloor == Floor::FLOOR1 ? + floor1Ref : floor2Ref ); + + return; + } + + // service other floor (if needed) + if ( otherFloorNeedsService ) + prepareToLeave( true ); + +} // end function processPossibleDeparture + +// arrive at a particular floor +void Elevator::arriveAtFloor( Floor& arrivalFloor ) +{ + moving = false; // reset state + + cout << "elevator resets its button" << endl; + elevatorButton.resetButton(); + + bell.ringBell(); + + // notify floor that elevator has arrived + Person *floorPersonPtr = arrivalFloor.elevatorArrived(); + + door.openDoor( + passengerPtr, floorPersonPtr, arrivalFloor, *this ); + + // this floor needs service? + bool currentFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor1NeedsService : floor2NeedsService; + + // other floor needs service? + bool otherFloorNeedsService = + currentFloor == Floor::FLOOR1 ? + floor2NeedsService : floor1NeedsService; + + // if this floor does not need service + // prepare to leave for the other floor + if ( !currentFloorNeedsService ) + prepareToLeave( otherFloorNeedsService ); + + else // otherwise, reset service flag + currentFloor == Floor::FLOOR1 ? + floor1NeedsService = false: floor2NeedsService = false; + +} // end function arriveAtFloor + +// request service from elevator +void Elevator::summonElevator( int floor ) +{ + // set appropriate servicing flag + floor == Floor::FLOOR1 ? + floor1NeedsService = true : floor2NeedsService = true; + +} // end function summonElevator + +// accept a passenger +void Elevator::passengerEnters( Person * const personPtr ) +{ + // board passenger + passengerPtr = personPtr; + + cout << "person " << passengerPtr->getID() + << " enters elevator from floor " + << currentFloor << endl; + +} // end function passengerEnters + +// notify elevator that passenger is exiting +void Elevator::passengerExits() +{ + passengerPtr = 0; + +} // end function passengerExits + +// prepare to leave a floor +void Elevator::prepareToLeave( bool leaving ) +{ + // get reference to current floor + Floor &thisFloor = + currentFloor == Floor::FLOOR1 ? floor1Ref : floor2Ref; + + // notify floor that elevator may be leaving + thisFloor.elevatorLeaving(); + + door.closeDoor( thisFloor ); + + if ( leaving ) // leave, if necessary + move(); + +} // end function prepareToLeave + +// go to other floor +void Elevator::move() +{ + moving = true; // change state + + // schedule arrival time + arrivalTime = currentBuildingClockTime + + ELEVATOR_TRAVEL_TIME; + + cout << "elevator begins moving " + << ( direction == DOWN ? "down " : "up " ) + << "to floor " + << ( direction == DOWN ? '1' : '2' ) + << " (arrives at time " << arrivalTime << ')' + << endl; + +} // end function move + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.h new file mode 100644 index 0000000..ed69376 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevator.h @@ -0,0 +1,75 @@ +// Fig. 7.41: elevator.h +// Elevator class definition. +#ifndef ELEVATOR_H +#define ELEVATOR_H + +#include "elevatorButton.h" +#include "door.h" +#include "bell.h" + +class Floor; // forward declaration +class Person; // forward declaration + +class Elevator { + +public: + Elevator( Floor &, Floor & ); // constructor + ~Elevator(); // destructor + void summonElevator( int ); // request to service floor + void prepareToLeave( bool ); // prepare to leave + void processTime( int ); // give time to elevator + void passengerEnters( Person * const ); // board a passenger + void passengerExits(); // exit a passenger + + // public object accessible to client code with + // access to Elevator object + ElevatorButton elevatorButton; + +private: + + // utility functions + void processPossibleArrival(); + void processPossibleDeparture(); + void arriveAtFloor( Floor & ); + void move(); + + // static constants that represent time required to travel + // between floors and directions of the elevator + static const int ELEVATOR_TRAVEL_TIME; + static const int UP; + static const int DOWN; + + // data members + int currentBuildingClockTime; // current time + bool moving; // elevator state + int direction; // current direction + int currentFloor; // current location + int arrivalTime; // time to arrive at a floor + bool floor1NeedsService; // floor1 service flag + bool floor2NeedsService; // floor2 service flag + + Floor &floor1Ref; // reference to floor1 + Floor &floor2Ref; // reference to floor2 + Person *passengerPtr; // pointer to passenger + + Door door; // door object + Bell bell; // bell object + +}; // end class Elevator + +#endif // ELEVATOR_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.cpp new file mode 100644 index 0000000..c7b7117 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.cpp @@ -0,0 +1,49 @@ +// Fig. 9.36: elevatorButton.cpp: +// Member-function definitions for class ElevatorButton. +#include + +using std::cout; +using std::endl; + +#include "elevatorButton.h" // ElevatorButton class definition +#include "elevator.h" // Elevator class definition + +// constructor +ElevatorButton::ElevatorButton( Elevator &elevatorHandle ) + : Button( elevatorHandle ) +{ + cout << "elevator button created" << endl; + +} // end ElevatorButton constructor + +// destructor +ElevatorButton::~ElevatorButton() +{ + cout << "elevator button destroyed" << endl; + +} // end ~ElevatorButton destructor + +// press button and signal elevator to prepare to leave floor +void ElevatorButton::pressButton() +{ + Button::pressButton(); + cout << "elevator button tells elevator to prepare to leave" + << endl; + elevatorRef.prepareToLeave( true ); + +} // end function pressButton + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.h new file mode 100644 index 0000000..4567799 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/elevatorButton.h @@ -0,0 +1,32 @@ +// Fig. 9.35: elevatorButton.h +// ElevatorButton class definition. +#ifndef ELEVATORBUTTON_H +#define ELEVATORBUTTON_H + +#include "button.h" // Button class definition + +class ElevatorButton : public Button { + +public: + ElevatorButton( Elevator & ); // constructor + ~ElevatorButton(); // destructor + void pressButton(); // press the button + +}; // end class ElevatorButton + +#endif // ELEVATORBUTTON_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.cpp new file mode 100644 index 0000000..c24564b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.cpp @@ -0,0 +1,98 @@ +// Fig. 7.44: floor.cpp +// Member-function definitions for class Floor. +#include + +using std::cout; +using std::endl; + +#include "floor.h" // Floor class definition +#include "person.h" // Person class definition +#include "elevator.h" // Elevator class definition +#include "door.h" // Door class definition + +// static constants that represent the floor numbers +const int Floor::FLOOR1 = 1; +const int Floor::FLOOR2 = 2; + +// constructor +Floor::Floor(int number, Elevator &elevatorHandle ) + : floorButton( number, elevatorHandle ), + floorNumber( number ), + elevatorRef( elevatorHandle ), + occupantPtr ( 0 ), + light( floorNumber ) +{ + cout << "floor " << floorNumber << " created" << endl; + +} // end Floor constructor + +// destructor +Floor::~Floor() +{ + delete occupantPtr; + cout << "floor " << floorNumber << " destroyed" << endl; + +} // end ~Floor destructor + +// determine whether floor is occupied +bool Floor::isOccupied() const +{ + return ( occupantPtr != 0 ); + +} // end function isOccupied + +// return this floor's number +int Floor::getNumber() const +{ + return floorNumber; + +} // end function getNumber + +// person arrives on floor +void Floor::personArrives( Person * const personPtr ) +{ + occupantPtr = personPtr; + +} // end function personArrives + +// notify floor that elevator has arrived +Person *Floor::elevatorArrived() +{ + cout << "floor " << floorNumber + << " resets its button" << endl; + + floorButton.resetButton(); + light.turnOn(); + + return occupantPtr; + +} // end function elevatorArrived + +// tell floor that elevator is leaving +void Floor::elevatorLeaving() +{ + light.turnOff(); + +} // end function elevatorLeaving + +// notifies floor that person is leaving +void Floor::personBoardingElevator() +{ + occupantPtr = 0; // person no longer on floor + +} // end function personBoardingElevator + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.h new file mode 100644 index 0000000..5ded23e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floor.h @@ -0,0 +1,63 @@ +// Fig. 7.43: floor.h +// Floor class definition. +#ifndef FLOOR_H +#define FLOOR_H + +#include "floorButton.h" +#include "light.h" + +class Elevator; // forward declaration +class Person; // forward declaration + +class Floor { + +public: + Floor( int, Elevator & ); // constructor + ~Floor(); // destructor + bool isOccupied() const; // return true if floor occupied + int getNumber() const; // return floor's number + + // pass a handle to new person coming on floor + void personArrives( Person * const ); + + // notify floor that elevator has arrived + Person *elevatorArrived(); + + // notify floor that elevator is leaving + void elevatorLeaving(); + + // notify floor that person is leaving floor + void personBoardingElevator(); + + // static constants representing floor numbers + static const int FLOOR1; + static const int FLOOR2; + + // public FloorButton object accessible to + // any client code with access to a Floor + FloorButton floorButton; + +private: + const int floorNumber; // the floor's number + Elevator &elevatorRef; // reference to elevator + Person *occupantPtr; // pointer to person on floor + Light light; // light object + +}; // end class Floor + +#endif // FLOOR_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.cpp new file mode 100644 index 0000000..0370b4e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.cpp @@ -0,0 +1,54 @@ +// Fig. 9.38: floorButton.cpp +// Member-function definitions for class FloorButton. +#include + +using std::cout; +using std::endl; + +#include "floorButton.h" +#include "elevator.h" + +// constructor +FloorButton::FloorButton( int floor, Elevator &elevatorHandle ) + : Button( elevatorHandle ), + floorNumber( floor ) +{ + cout << "floor " << floorNumber << " button created" + << endl; + +} // end FloorButton constructor + +// destructor +FloorButton::~FloorButton() +{ + cout << "floor " << floorNumber << " button destroyed" + << endl; + +} // end ~FloorButton destructor + +// press the button +void FloorButton::pressButton() +{ + Button::pressButton(); + cout << "floor " << floorNumber + << " button summons elevator" << endl; + + // call elevator to this floor + elevatorRef.summonElevator( floorNumber ); + +} // end function pressButton + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.h new file mode 100644 index 0000000..a0525f7 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/floorButton.h @@ -0,0 +1,35 @@ +// Fig. 9.37: floorButton.h +// FloorButton class definition. +#ifndef FLOORBUTTON_H +#define FLOORBUTTON_H + +#include "button.h" // Button class definition + +class FloorButton : public Button { + +public: + FloorButton( int, Elevator & ); // constructor + ~FloorButton(); // destructor + void pressButton(); // press the button + +private: + const int floorNumber; // button's floor number + +}; // end class FloorButton + +#endif // FLOORBUTTON_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.cpp new file mode 100644 index 0000000..54f3204 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.cpp @@ -0,0 +1,64 @@ +// Fig. 7.34: light.cpp +// Member-function definitions for class Light. +#include + +using std::cout; +using std::endl; + +#include "light.h" // Light class definition + +// constructor +Light::Light( int number ) + : on( false ), + floorNumber( number ) +{ + cout << "floor " << floorNumber << " light created" << endl; + +} // end Light constructor + +// destuctor +Light::~Light() +{ + cout << "floor " << floorNumber + << " light destroyed" << endl; + +} // end ~Light destructor + +// turn light on +void Light::turnOn() +{ + if ( !on ) { // if light not on, turn it on + on = true; + cout << "floor " << floorNumber + << " light turns on" << endl; + + } // end if + +} // end function turnOn + +// turn light off +void Light::turnOff() +{ + if ( on ) { // if light is on, turn it off + on = false; + cout << "floor " << floorNumber + << " light turns off" << endl; + + } // end if + +} // end function turnOff + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.h new file mode 100644 index 0000000..69c559c --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/light.h @@ -0,0 +1,35 @@ +// Fig. 7.33: light.h +// Light class definition. +#ifndef LIGHT_H +#define LIGHT_H + +class Light { + +public: + Light( int ); // constructor + ~Light(); // destructor + void turnOn(); // turns light on + void turnOff(); // turns light off + +private: + bool on; // true if on; false if off + const int floorNumber; // floor number that contains light + +}; // end class Light + +#endif // LIGHT_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.cpp new file mode 100644 index 0000000..a3cfeb7 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.cpp @@ -0,0 +1,91 @@ +// Fig. 7.46: person.cpp +// Member-function definitions for class Person. +#include + +using std::cout; +using std::endl; + +#include "person.h" // Person class definition +#include "floor.h" // Floor class definition +#include "elevator.h" // Elevator class definition + +// initialize static member personCount +int Person::personCount = 0; + +// constructor +Person::Person( int destFloor ) + : ID( ++personCount ), + destinationFloor( destFloor ) +{ + // empty body + +} // end Person constructor + +// destructor +Person::~Person() +{ + cout << "(person " << ID << " destructor invoked)" << endl; + +} // end ~Person destructor + +// return person's ID number +int Person::getID() const +{ + return ID; + +} // end function getID + +// person walks onto a floor +void Person::stepOntoFloor( Floor& floor ) +{ + // notify floor person is coming + cout << "person " << ID << " steps onto floor " + << floor.getNumber() << endl; + floor.personArrives( this ); + + // press button on floor + cout << "person " << ID + << " presses floor button on floor " + << floor.getNumber() << endl; + floor.floorButton.pressButton(); + +} // end function stepOntoFloor + +// person enters elevator +void Person::enterElevator( Elevator &elevator, Floor &floor ) +{ + floor.personBoardingElevator(); // person leaves floor + + elevator.passengerEnters( this ); // person enters elevator + + // press button on elevator + cout << "person " << ID + << " presses elevator button" << endl; + elevator.elevatorButton.pressButton(); + +} // end function enterElevator + +// person exits elevator +void Person::exitElevator( + const Floor &floor, Elevator &elevator ) const +{ + cout << "person " << ID << " exits elevator on floor " + << floor.getNumber() << endl; + elevator.passengerExits(); + +} // end function exitElevator + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.h new file mode 100644 index 0000000..64906a5 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/person.h @@ -0,0 +1,42 @@ +// Fig. 7.45: person.h +// Person class definition. +#ifndef PERSON_H +#define PERSON_H + +class Floor; // forward declaration +class Elevator; // forward declaration + +class Person { + +public: + Person( int ); // constructor + ~Person(); // destructor + int getID() const; // returns person's ID + + void stepOntoFloor( Floor & ); + void enterElevator( Elevator &, Floor & ); + void exitElevator( const Floor &, Elevator & ) const; + +private: + static int personCount; // total number of people + const int ID; // person's unique ID # + const int destinationFloor; // destination floor # + +}; // end class Person + +#endif // PERSON_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.cpp new file mode 100644 index 0000000..ac9110c --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.cpp @@ -0,0 +1,133 @@ +// Fig. 7.30: scheduler.cpp +// Member-function definitions for class Scheduler. +#include + +using std::cout; +using std::endl; + +#include +#include + +#include "scheduler.h" // Scheduler class definition +#include "floor.h" // Floor class definition +#include "person.h" // Person class definition + +// constructor +Scheduler::Scheduler( Floor &firstFloor, Floor &secondFloor ) + : currentClockTime( 0 ), + floor1Ref( firstFloor ), + floor2Ref( secondFloor ) +{ + srand( time( 0 ) ); // seed random number generator + cout << "scheduler created" << endl; + + // schedule first arrivals for floor 1 and floor 2 + scheduleTime( floor1Ref ); + scheduleTime( floor2Ref ); + +} // end Scheduler constructor + +// destructor +Scheduler::~Scheduler() +{ + cout << "scheduler destroyed" << endl; + +} // end Scheduler destructor + +// schedule arrival on a floor +void Scheduler::scheduleTime( const Floor &floor ) +{ + int floorNumber = floor.getNumber(); + int arrivalTime = currentClockTime + ( 5 + rand() % 16 ); + + floorNumber == Floor::FLOOR1 ? + floor1ArrivalTime = arrivalTime : + floor2ArrivalTime = arrivalTime; + + cout << "(scheduler schedules next person for floor " + << floorNumber << " at time " << arrivalTime << ')' + << endl; + +} // end function scheduleTime + +// reschedule arrival on a floor +void Scheduler::delayTime( const Floor &floor ) +{ + int floorNumber = floor.getNumber(); + + int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? + ++floor1ArrivalTime : ++floor2ArrivalTime; + + cout << "(scheduler delays next person for floor " + << floorNumber << " until time " << arrivalTime << ')' + << endl; + +} // end function delayTime + +// give time to scheduler +void Scheduler::processTime( int time ) +{ + currentClockTime = time; // record time + + // handle arrivals on floor 1 + handleArrivals( floor1Ref, currentClockTime ); + + // handle arrivals on floor 2 + handleArrivals( floor2Ref, currentClockTime ); + +} // end function processTime + +// create new person and place it on specified floor +void Scheduler::createNewPerson( Floor &floor ) +{ + int destinationFloor = + floor.getNumber() == Floor::FLOOR1 ? + Floor::FLOOR2 : Floor::FLOOR1; + + // create new person + Person *newPersonPtr = new Person( destinationFloor ); + + cout << "scheduler creates person " + << newPersonPtr->getID() << endl; + + // place person on proper floor + newPersonPtr->stepOntoFloor( floor ); + + scheduleTime( floor ); // schedule next arrival + +} // end function createNewPerson + +// handle arrivals for a specified floor +void Scheduler::handleArrivals( Floor &floor, int time ) +{ + int floorNumber = floor.getNumber(); + + int arrivalTime = ( floorNumber == Floor::FLOOR1 ) ? + floor1ArrivalTime : floor2ArrivalTime; + + if ( arrivalTime == time ) { + + if ( floor.isOccupied() ) // if floor occupied, + delayTime( floor ); // delay arrival + + else // otherwise, + createNewPerson( floor ); // create new person + + } // end outer if + +} // end function handleArrivals + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.h b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.h new file mode 100644 index 0000000..6bb084c --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/elevatorinheritance/scheduler.h @@ -0,0 +1,53 @@ +// Fig. 7.29: scheduler.h +// Scheduler class definition. +#ifndef SCHEDULER_H +#define SCHEDULER_H + +class Floor; // forward declaration + +class Scheduler { + +public: + Scheduler( Floor &, Floor & ); // constructor + ~Scheduler(); // destructor + void processTime( int ); // set scheduler's time + +private: + // schedule arrival to a floor + void scheduleTime( const Floor & ); + + // delay arrival to a floor + void delayTime( const Floor & ); + + // create new person; place on floor + void createNewPerson( Floor & ); + + // handle person arrival on a floor + void handleArrivals( Floor &, int ); + + int currentClockTime; + + Floor &floor1Ref; + Floor &floor2Ref; + + int floor1ArrivalTime; + int floor2ArrivalTime; + +}; // end class Scheduler + +#endif // SCHEDULER_H + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.cpp new file mode 100644 index 0000000..38261da --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.cpp @@ -0,0 +1,65 @@ +// Fig. 9.5: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) +{ + x = xValue; + y = yValue; + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << x << ", " << y << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.h b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.h new file mode 100644 index 0000000..7593775 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/point.h @@ -0,0 +1,40 @@ +// Fig. 9.4: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/pointtest.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/pointtest.cpp new file mode 100644 index 0000000..3ca57bf --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_04_06/PointTest/pointtest.cpp @@ -0,0 +1,43 @@ +// Fig. 9.6: pointtest.cpp +// Testing class Point. +#include + +using std::cout; +using std::endl; + +#include "point.h" // Point class definition + +int main() +{ + Point point( 72, 115 ); // instantiate Point object + + // display point coordinates + cout << "X coordinate is " << point.getX() + << "\nY coordinate is " << point.getY(); + + point.setX( 10 ); // set x-coordinate + point.setY( 10 ); // set y-coordinate + + // display new point value + cout << "\n\nThe new location of point is "; + point.print(); + cout << endl; + + return 0; // indicates successful termination + +} // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.cpp new file mode 100644 index 0000000..0a32f7d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.cpp @@ -0,0 +1,102 @@ +// Fig. 9.8: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) +{ + x = xValue; + y = yValue; + setRadius( radiusValue ); + +} // end Circle constructor + +// set x in coordinate pair +void Circle::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Circle::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Circle::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Circle::getY() const +{ + return y; + +} // end function getY + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return 2 * radius; + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * radius * radius; + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "Center = [" << x << ", " << y << ']' + << "; Radius = " << radius; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.h b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.h new file mode 100644 index 0000000..4ee5c3e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circle.h @@ -0,0 +1,50 @@ +// Fig. 9.7: circle.h +// Circle class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +class Circle { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle object + +private: + int x; // x-coordinate of Circle's center + int y; // y-coordinate of Circle's center + double radius; // Circle's radius + +}; // end class Circle + +#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/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circletest.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circletest.cpp new file mode 100644 index 0000000..f71c455 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_07_09/CircleTest/circletest.cpp @@ -0,0 +1,63 @@ +// Fig. 9.9: circletest.cpp +// Testing class Circle. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "circle.h" // Circle class definition + +int main() +{ + Circle circle( 37, 43, 2.5 ); // instantiate Circle object + + // display point coordinates + cout << "X coordinate is " << circle.getX() + << "\nY coordinate is " << circle.getY() + << "\nRadius is " << circle.getRadius(); + + circle.setX( 2 ); // set new x-coordinate + circle.setY( 2 ); // set new y-coordinate + circle.setRadius( 4.25 ); // set new radius + + // display new point value + cout << "\n\nThe new location and radius of circle are\n"; + circle.print(); + + // display floating-point values with 2 digits of precision + cout << fixed << setprecision( 2 ); + + // display Circle's diameter + cout << "\nDiameter is " << circle.getDiameter(); + + // display Circle's circumference + cout << "\nCircumference is " << circle.getCircumference(); + + // display Circle's area + cout << "\nArea is " << circle.getArea(); + + cout << endl; + + return 0; // indicates successful termination + +} // 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/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.cpp new file mode 100644 index 0000000..3e043c3 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.cpp @@ -0,0 +1,74 @@ +// Fig. 9.20: circle4.cpp +// Circle4 class member-function definitions. +#include + +using std::cout; + +#include "circle4.h" // Circle4 class definition + +// default constructor +Circle4::Circle4( int xValue, int yValue, double radiusValue ) + : Point3( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle4 constructor + +// set radius +void Circle4::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle4::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle4::getDiameter() const +{ + return 2 * getRadius(); + +} // end function getDiameter + +// calculate and return circumference +double Circle4::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle4::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle4 object +void Circle4::print() const +{ + cout << "Center = "; + Point3::print(); // invoke Point3's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.h b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.h new file mode 100644 index 0000000..46f0f58 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circle4.h @@ -0,0 +1,44 @@ +// Fig. 9.19: circle4.h +// Circle4 class contains x-y coordinate pair and radius. +#ifndef CIRCLE4_H +#define CIRCLE4_H + +#include "point3.h" // Point3 class definition + +class Circle4 : public Point3 { + +public: + + // default constructor + Circle4( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle4 object + +private: + double radius; // Circle4's radius + +}; // end class Circle4 + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circletest4.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circletest4.cpp new file mode 100644 index 0000000..c82f30f --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/circletest4.cpp @@ -0,0 +1,63 @@ +// Fig. 9.21: circletest4.cpp +// Testing class Circle4. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "circle4.h" // Circle4 class definition + +int main() +{ + Circle4 circle( 37, 43, 2.5 ); // instantiate Circle4 object + + // display point coordinates + cout << "X coordinate is " << circle.getX() + << "\nY coordinate is " << circle.getY() + << "\nRadius is " << circle.getRadius(); + + circle.setX( 2 ); // set new x-coordinate + circle.setY( 2 ); // set new y-coordinate + circle.setRadius( 4.25 ); // set new radius + + // display new circle value + cout << "\n\nThe new location and radius of circle are\n"; + circle.print(); + + // display floating-point values with 2 digits of precision + cout << fixed << setprecision( 2 ); + + // display Circle4's diameter + cout << "\nDiameter is " << circle.getDiameter(); + + // display Circle4's circumference + cout << "\nCircumference is " << circle.getCircumference(); + + // display Circle4's area + cout << "\nArea is " << circle.getArea(); + + cout << endl; + + return 0; // indicates successful termination + +} // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.cpp new file mode 100644 index 0000000..ac4f642 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.cpp @@ -0,0 +1,65 @@ +// Fig. 9.18: point3.cpp +// Point3 class member-function definitions. +#include + +using std::cout; + +#include "point3.h" // Point3 class definition + +// default constructor +Point3::Point3( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point3 constructor + +// set x in coordinate pair +void Point3::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point3::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point3::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point3::getY() const +{ + return y; + +} // end function getY + +// output Point3 object +void Point3::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.h b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.h new file mode 100644 index 0000000..028f105 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_17_21/CircleTest4/point3.h @@ -0,0 +1,40 @@ +// Fig. 9.17: point3.h +// Point3 class definition represents an x-y coordinate pair. +#ifndef POINT3_H +#define POINT3_H + +class Point3 { + +public: + Point3( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point3 object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point3 + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.cpp new file mode 100644 index 0000000..3e043c3 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.cpp @@ -0,0 +1,74 @@ +// Fig. 9.20: circle4.cpp +// Circle4 class member-function definitions. +#include + +using std::cout; + +#include "circle4.h" // Circle4 class definition + +// default constructor +Circle4::Circle4( int xValue, int yValue, double radiusValue ) + : Point3( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle4 constructor + +// set radius +void Circle4::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle4::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle4::getDiameter() const +{ + return 2 * getRadius(); + +} // end function getDiameter + +// calculate and return circumference +double Circle4::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle4::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle4 object +void Circle4::print() const +{ + cout << "Center = "; + Point3::print(); // invoke Point3's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.h b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.h new file mode 100644 index 0000000..46f0f58 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/circle4.h @@ -0,0 +1,44 @@ +// Fig. 9.19: circle4.h +// Circle4 class contains x-y coordinate pair and radius. +#ifndef CIRCLE4_H +#define CIRCLE4_H + +#include "point3.h" // Point3 class definition + +class Circle4 : public Point3 { + +public: + + // default constructor + Circle4( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle4 object + +private: + double radius; // Circle4's radius + +}; // end class Circle4 + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.cpp new file mode 100644 index 0000000..d0047db --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.cpp @@ -0,0 +1,68 @@ +// Fig. 9.23: cylinder.cpp +// Cylinder class inherits from class Circle4. +#include + +using std::cout; + +#include "cylinder.h" // Cylinder class definition + +// default constructor +Cylinder::Cylinder( int xValue, int yValue, double radiusValue, + double heightValue ) + : Circle4( xValue, yValue, radiusValue ) +{ + setHeight( heightValue ); + +} // end Cylinder constructor + +// set Cylinder's height +void Cylinder::setHeight( double heightValue ) +{ + height = ( heightValue < 0.0 ? 0.0 : heightValue ); + +} // end function setHeight + +// get Cylinder's height +double Cylinder::getHeight() const +{ + return height; + +} // end function getHeight + +// redefine Circle4 function getArea to calculate Cylinder area +double Cylinder::getArea() const +{ + return 2 * Circle4::getArea() + + getCircumference() * getHeight(); + +} // end function getArea + +// calculate Cylinder volume +double Cylinder::getVolume() const +{ + return Circle4::getArea() * getHeight(); + +} // end function getVolume + +// output Cylinder object +void Cylinder::print() const +{ + Circle4::print(); + cout << "; Height = " << getHeight(); + +} // end function print + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.h b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.h new file mode 100644 index 0000000..461feb8 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylinder.h @@ -0,0 +1,42 @@ +// Fig. 9.22: cylinder.h +// Cylinder class inherits from class Circle4. +#ifndef CYLINDER_H +#define CYLINDER_H + +#include "circle4.h" // Circle4 class definition + +class Cylinder : public Circle4 { + +public: + + // default constructor + Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); + + void setHeight( double ); // set Cylinder's height + double getHeight() const; // return Cylinder's height + + double getArea() const; // return Cylinder's area + double getVolume() const; // return Cylinder's volume + void print() const; // output Cylinder + +private: + double height; // Cylinder's height + +}; // end class Cylinder + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylindertest.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylindertest.cpp new file mode 100644 index 0000000..8fb49b9 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/cylindertest.cpp @@ -0,0 +1,70 @@ +// Fig. 9.24: cylindertest.cpp +// Testing class Cylinder. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "cylinder.h" // Cylinder class definition + +int main() +{ + // instantiate Cylinder object + Cylinder cylinder( 12, 23, 2.5, 5.7 ); + + // display point coordinates + cout << "X coordinate is " << cylinder.getX() + << "\nY coordinate is " << cylinder.getY() + << "\nRadius is " << cylinder.getRadius() + << "\nHeight is " << cylinder.getHeight(); + + cylinder.setX( 2 ); // set new x-coordinate + cylinder.setY( 2 ); // set new y-coordinate + cylinder.setRadius( 4.25 ); // set new radius + cylinder.setHeight( 10 ); // set new height + + // display new cylinder value + cout << "\n\nThe new location and radius of circle are\n"; + cylinder.print(); + + // display floating-point values with 2 digits of precision + cout << fixed << setprecision( 2 ); + + // display cylinder's diameter + cout << "\n\nDiameter is " << cylinder.getDiameter(); + + // display cylinder's circumference + cout << "\nCircumference is " + << cylinder.getCircumference(); + + // display cylinder's area + cout << "\nArea is " << cylinder.getArea(); + + // display cylinder's volume + cout << "\nVolume is " << cylinder.getVolume(); + + cout << endl; + + return 0; // indicates successful termination + +} // 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/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.cpp b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.cpp new file mode 100644 index 0000000..70af44a --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.cpp @@ -0,0 +1,65 @@ +// Fig. 9.18: point3.cpp +// Point3 class member-function definitions. +#include + +using std::cout; + +#include "point3.h" // Point3 class definition + +// default constructor +Point3::Point3( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point3 constructor + +// set x in coordinate pair +void Point3::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point3::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point3::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point3::getY() const +{ + return y; + +} // end function getY + +// output Point3 object +void Point3::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (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/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.h b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.h new file mode 100644 index 0000000..028f105 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/1_ch09/fig09_22_24/CylinderTest/point3.h @@ -0,0 +1,40 @@ +// Fig. 9.17: point3.h +// Point3 class definition represents an x-y coordinate pair. +#ifndef POINT3_H +#define POINT3_H + +class Point3 { + +public: + Point3( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point3 object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point3 + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp new file mode 100644 index 0000000..c04f750 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/Fig10_05.cpp @@ -0,0 +1,75 @@ +// Fig. 10.5: fig10_05.cpp +// Aiming base-class and derived-class pointers at base-class +// and derived-class objects, respectively. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point point( 30, 50 ); + Point *pointPtr = 0; // base-class pointer + + Circle circle( 120, 89, 2.7 ); + Circle *circlePtr = 0; // derived-class pointer + + // set floating-point numeric formatting + cout << fixed << setprecision( 2 ); + + // output objects point and circle + cout << "Print point and circle objects:" + << "\nPoint: "; + point.print(); // invokes Point's print + cout << "\nCircle: "; + circle.print(); // invokes Circle's print + + // aim base-class pointer at base-class object and print + pointPtr = &point; + cout << "\n\nCalling print with base-class pointer to " + << "\nbase-class object invokes base-class print " + << "function:\n"; + pointPtr->print(); // invokes Point's print + + // aim derived-class pointer at derived-class object + // and print + circlePtr = &circle; + cout << "\n\nCalling print with derived-class pointer to " + << "\nderived-class object invokes derived-class " + << "print function:\n"; + circlePtr->print(); // invokes Circle's print + + // aim base-class pointer at derived-class object and print + pointPtr = &circle; + cout << "\n\nCalling print with base-class pointer to " + << "derived-class object\ninvokes base-class print " + << "function on that derived-class object\n"; + pointPtr->print(); // invokes Point's print + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp new file mode 100644 index 0000000..91b708d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.4: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return 2 * getRadius(); + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "center = "; + Point::print(); // invoke Point's print function + cout << "; radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h new file mode 100644 index 0000000..42ec256 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.3: circle.h +// Circle class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#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/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h new file mode 100644 index 0000000..6a2874e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_01_05/point.h @@ -0,0 +1,40 @@ +// Fig. 10.1: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp new file mode 100644 index 0000000..8c3c122 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/Fig10_06.cpp @@ -0,0 +1,31 @@ +// Fig. 10.6: fig10_06.cpp +// Aiming a derived-class pointer at a base-class object. +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point point( 30, 50 ); + Circle *circlePtr = 0; + + // aim derived-class pointer at base-class object + circlePtr = &point; // Error: a Point is not a Circle + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp new file mode 100644 index 0000000..2f3325e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.4: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return getRadius() * 2; + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "Center = "; + Point::print(); // invoke Point's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (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/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h new file mode 100644 index 0000000..fc22e5b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.3: circle.h +// Circle4 class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h new file mode 100644 index 0000000..6a2874e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_06/point.h @@ -0,0 +1,40 @@ +// Fig. 10.1: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp new file mode 100644 index 0000000..582d5d9 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.4: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return getRadius() * 2; + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "Center = "; + Point::print(); // invoke Point's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h new file mode 100644 index 0000000..fc22e5b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.3: circle.h +// Circle4 class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp new file mode 100644 index 0000000..184ed32 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/fig10_07.cpp @@ -0,0 +1,48 @@ +// Fig. 10.7: fig10_07.cpp +// Attempting to invoke derived-class-only member functions +// through a base-class pointer. +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point *pointPtr = 0; + Circle circle( 120, 89, 2.7 ); + + // aim base-class pointer at derived-class object + pointPtr = &circle; + + // invoke base-class member functions on derived-class + // object through base-class pointer + int x = pointPtr->getX(); + int y = pointPtr->getY(); + pointPtr->setX( 10 ); + pointPtr->setY( 10 ); + pointPtr->print(); + + // attempt to invoke derived-class-only member functions + // on derived-class object through base-class pointer + double radius = pointPtr->getRadius(); + pointPtr->setRadius( 33.33 ); + double diameter = pointPtr->getDiameter(); + double circumference = pointPtr->getCircumference(); + double area = pointPtr->getArea(); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h new file mode 100644 index 0000000..6a2874e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_07/point.h @@ -0,0 +1,40 @@ +// Fig. 10.1: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp new file mode 100644 index 0000000..be43be3 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/Fig10_10.cpp @@ -0,0 +1,80 @@ +// Fig. 10.10: fig10_10.cpp +// Introducing polymorphism, virtual functions and dynamic +// binding. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition + +int main() +{ + Point point( 30, 50 ); + Point *pointPtr = 0; + + Circle circle( 120, 89, 2.7 ); + Circle *circlePtr = 0; + + // set floating-point numeric formatting + cout << fixed << setprecision( 2 ); + + // output objects point and circle using static binding + cout << "Invoking print function on point and circle " + << "\nobjects with static binding " + << "\n\nPoint: "; + point.print(); // static binding + cout << "\nCircle: "; + circle.print(); // static binding + + // output objects point and circle using dynamic binding + cout << "\n\nInvoking print function on point and circle " + << "\nobjects with dynamic binding"; + + // aim base-class pointer at base-class object and print + pointPtr = &point; + cout << "\n\nCalling virtual function print with base-class" + << "\npointer to base-class object" + << "\ninvokes base-class print function:\n"; + pointPtr->print(); + + // aim derived-class pointer at derived-class + // object and print + circlePtr = &circle; + cout << "\n\nCalling virtual function print with " + << "\nderived-class pointer to derived-class object " + << "\ninvokes derived-class print function:\n"; + circlePtr->print(); + + // aim base-class pointer at derived-class object and print + pointPtr = &circle; + cout << "\n\nCalling virtual function print with base-class" + << "\npointer to derived-class object " + << "\ninvokes derived-class print function:\n"; + pointPtr->print(); // polymorphism: invokes circle's print + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp new file mode 100644 index 0000000..550b3e2 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.cpp @@ -0,0 +1,74 @@ +// Fig. 10.11: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return getRadius() * 2; + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// calculate and return area +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// output Circle object +void Circle::print() const +{ + cout << "Center = "; + Point::print(); // invoke Point's print function + cout << "; Radius = " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h new file mode 100644 index 0000000..138a4b8 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/circle.h @@ -0,0 +1,44 @@ +// Fig. 10.10: circle.h +// Circle class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + double getArea() const; // return area + + virtual void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp new file mode 100644 index 0000000..43a6b14 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.cpp @@ -0,0 +1,65 @@ +// Fig. 10.2: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h new file mode 100644 index 0000000..5cd7bd6 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_08_12/point.h @@ -0,0 +1,40 @@ +// Fig. 10.8: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +class Point { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + virtual void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp new file mode 100644 index 0000000..2ce0553 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/Fig10_20.cpp @@ -0,0 +1,120 @@ +// Fig. 10.20: fig10_20.cpp +// Driver for shape, point, circle, cylinder hierarchy. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include + +using std::vector; + +#include "shape.h" // Shape class definition +#include "point.h" // Point class definition +#include "circle.h" // Circle class definition +#include "cylinder.h" // Cylinder class definition + +void virtualViaPointer( const Shape * ); +void virtualViaReference( const Shape & ); + +int main() +{ + // set floating-point number format + cout << fixed << setprecision( 2 ); + + Point point( 7, 11 ); // create a Point + Circle circle( 22, 8, 3.5 ); // create a Circle + Cylinder cylinder( 10, 10, 3.3, 10 ); // create a Cylinder + + cout << point.getName() << ": "; // static binding + point.print(); // static binding + cout << '\n'; + + cout << circle.getName() << ": "; // static binding + circle.print(); // static binding + cout << '\n'; + + cout << cylinder.getName() << ": "; // static binding + cylinder.print(); // static binding + cout << "\n\n"; + + // create vector of three base-class pointers + vector< Shape * > shapeVector( 3 ); + + // aim shapeVector[0] at derived-class Point object + shapeVector[ 0 ] = &point; + + // aim shapeVector[1] at derived-class Circle object + shapeVector[ 1 ] = &circle; + + // aim shapeVector[2] at derived-class Cylinder object + shapeVector[ 2 ] = &cylinder; + + // loop through shapeVector and call virtualViaPointer + // to print the shape name, attributes, area and volume + // of each object using dynamic binding + cout << "\nVirtual function calls made off " + << "base-class pointers:\n\n"; + + for ( int i = 0; i < shapeVector.size(); i++ ) + virtualViaPointer( shapeVector[ i ] ); + + // loop through shapeVector and call virtualViaReference + // to print the shape name, attributes, area and volume + // of each object using dynamic binding + cout << "\nVirtual function calls made off " + << "base-class references:\n\n"; + + for ( int j = 0; j < shapeVector.size(); j++ ) + virtualViaReference( *shapeVector[ j ] ); + + return 0; + +} // end main + +// make virtual function calls off a base-class pointer +// using dynamic binding +void virtualViaPointer( const Shape *baseClassPtr ) +{ + cout << baseClassPtr->getName() << ": "; + + baseClassPtr->print(); + + cout << "\narea is " << baseClassPtr->getArea() + << "\nvolume is " << baseClassPtr->getVolume() + << "\n\n"; + +} // end function virtualViaPointer + +// make virtual function calls off a base-class reference +// using dynamic binding +void virtualViaReference( const Shape &baseClassRef ) +{ + cout << baseClassRef.getName() << ": "; + + baseClassRef.print(); + + cout << "\narea is " << baseClassRef.getArea() + << "\nvolume is " << baseClassRef.getVolume() << "\n\n"; + +} // end function virtualViaReference + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp new file mode 100644 index 0000000..b8f6f59 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.cpp @@ -0,0 +1,81 @@ +// Fig. 10.17: circle.cpp +// Circle class member-function definitions. +#include + +using std::cout; + +#include "circle.h" // Circle class definition + +// default constructor +Circle::Circle( int xValue, int yValue, double radiusValue ) + : Point( xValue, yValue ) // call base-class constructor +{ + setRadius( radiusValue ); + +} // end Circle constructor + +// set radius +void Circle::setRadius( double radiusValue ) +{ + radius = ( radiusValue < 0.0 ? 0.0 : radiusValue ); + +} // end function setRadius + +// return radius +double Circle::getRadius() const +{ + return radius; + +} // end function getRadius + +// calculate and return diameter +double Circle::getDiameter() const +{ + return 2 * getRadius(); + +} // end function getDiameter + +// calculate and return circumference +double Circle::getCircumference() const +{ + return 3.14159 * getDiameter(); + +} // end function getCircumference + +// override virtual function getArea: return area of Circle +double Circle::getArea() const +{ + return 3.14159 * getRadius() * getRadius(); + +} // end function getArea + +// override virutual function getName: return name of Circle +string Circle::getName() const +{ + return "Circle"; + +} // end function getName + +// override virtual function print: output Circle object +void Circle::print() const +{ + cout << "center is "; + Point::print(); // invoke Point's print function + cout << "; radius is " << getRadius(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h new file mode 100644 index 0000000..6d6877c --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/circle.h @@ -0,0 +1,47 @@ +// Fig. 10.16: circle.h +// Circle class contains x-y coordinate pair and radius. +#ifndef CIRCLE_H +#define CIRCLE_H + +#include "point.h" // Point class definition + +class Circle : public Point { + +public: + + // default constructor + Circle( int = 0, int = 0, double = 0.0 ); + + void setRadius( double ); // set radius + double getRadius() const; // return radius + + double getDiameter() const; // return diameter + double getCircumference() const; // return circumference + virtual double getArea() const; // return area + + // return name of shape (i.e., "Circle") + virtual string getName() const; + + virtual void print() const; // output Circle object + +private: + double radius; // Circle's radius + +}; // end class Circle + +#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/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp new file mode 100644 index 0000000..7c51113 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.cpp @@ -0,0 +1,75 @@ +// Fig. 10.19: cylinder.cpp +// Cylinder class inherits from class Circle. +#include + +using std::cout; + +#include "cylinder.h" // Cylinder class definition + +// default constructor +Cylinder::Cylinder( int xValue, int yValue, double radiusValue, + double heightValue ) + : Circle( xValue, yValue, radiusValue ) +{ + setHeight( heightValue ); + +} // end Cylinder constructor + +// set Cylinder's height +void Cylinder::setHeight( double heightValue ) +{ + height = ( heightValue < 0.0 ? 0.0 : heightValue ); + +} // end function setHeight + +// get Cylinder's height +double Cylinder::getHeight() const +{ + return height; + +} // end function getHeight + +// override virtual function getArea: return Cylinder area +double Cylinder::getArea() const +{ + return 2 * Circle::getArea() + // code reuse + getCircumference() * getHeight(); + +} // end function getArea + +// override virtual function getVolume: return Cylinder volume +double Cylinder::getVolume() const +{ + return Circle::getArea() * getHeight(); // code reuse + +} // end function getVolume + +// override virtual function getName: return name of Cylinder +string Cylinder::getName() const +{ + return "Cylinder"; + +} // end function getName + +// override virtual function print: output Cylinder object +void Cylinder::print() const +{ + Circle::print(); // code reuse + cout << "; height is " << getHeight(); + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h new file mode 100644 index 0000000..f20d624 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/cylinder.h @@ -0,0 +1,46 @@ +// Fig. 10.18: cylinder.h +// Cylinder class inherits from class Circle. +#ifndef CYLINDER_H +#define CYLINDER_H + +#include "circle.h" // Circle class definition + +class Cylinder : public Circle { + +public: + + // default constructor + Cylinder( int = 0, int = 0, double = 0.0, double = 0.0 ); + + void setHeight( double ); // set Cylinder's height + double getHeight() const; // return Cylinder's height + + virtual double getArea() const; // return Cylinder's area + virtual double getVolume() const; // return Cylinder's volume + + // return name of shape (i.e., "Cylinder" ) + virtual string getName() const; + + virtual void print() const; // output Cylinder + +private: + double height; // Cylinder's height + +}; // end class Cylinder + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp new file mode 100644 index 0000000..3f63c81 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.cpp @@ -0,0 +1,72 @@ +// Fig. 10.15: point.cpp +// Point class member-function definitions. +#include + +using std::cout; + +#include "point.h" // Point class definition + +// default constructor +Point::Point( int xValue, int yValue ) + : x( xValue ), y( yValue ) +{ + // empty body + +} // end Point constructor + +// set x in coordinate pair +void Point::setX( int xValue ) +{ + x = xValue; // no need for validation + +} // end function setX + +// return x from coordinate pair +int Point::getX() const +{ + return x; + +} // end function getX + +// set y in coordinate pair +void Point::setY( int yValue ) +{ + y = yValue; // no need for validation + +} // end function setY + +// return y from coordinate pair +int Point::getY() const +{ + return y; + +} // end function getY + +// override pure virtual function getName: return name of Point +string Point::getName() const +{ + return "Point"; + +} // end function getName + +// override pure virtual function print: output Point object +void Point::print() const +{ + cout << '[' << getX() << ", " << getY() << ']'; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h new file mode 100644 index 0000000..264b37b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/point.h @@ -0,0 +1,45 @@ +// Fig. 10.14: point.h +// Point class definition represents an x-y coordinate pair. +#ifndef POINT_H +#define POINT_H + +#include "shape.h" // Shape class definition + +class Point : public Shape { + +public: + Point( int = 0, int = 0 ); // default constructor + + void setX( int ); // set x in coordinate pair + int getX() const; // return x from coordinate pair + + void setY( int ); // set y in coordinate pair + int getY() const; // return y from coordinate pair + + // return name of shape (i.e., "Point" ) + virtual string getName() const; + + virtual void print() const; // output Point object + +private: + int x; // x part of coordinate pair + int y; // y part of coordinate pair + +}; // end class Point + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp new file mode 100644 index 0000000..125ea79 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.cpp @@ -0,0 +1,36 @@ +// Fig. 10.13: shape.cpp +// Shape class member-function definitions. +#include + +using std::cout; + +#include "shape.h" // Shape class definition + +// return area of shape; 0.0 by default +double Shape::getArea() const +{ + return 0.0; + +} // end function getArea + +// return volume of shape; 0.0 by default +double Shape::getVolume() const +{ + return 0.0; + +} // end function getVolume + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h new file mode 100644 index 0000000..0c929a5 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_13_20/shape.h @@ -0,0 +1,41 @@ +// Fig. 10.12: shape.h +// Shape abstract-base-class definition. +#ifndef SHAPE_H +#define SHAPE_H + +#include // C++ standard string class + +using std::string; + +class Shape { + +public: + + // virtual function that returns shape area + virtual double getArea() const; + + // virtual function that returns shape volume + virtual double getVolume() const; + + // pure virtual functions; overridden in derived classes + virtual string getName() const = 0; // return shape name + virtual void print() const = 0; // output shape + +}; // end class Shape + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp new file mode 100644 index 0000000..2052d48 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.cpp @@ -0,0 +1,65 @@ +// Fig. 10.32: baseplus.cpp +// BasePlusCommissionEmployee member-function definitions. +#include + +using std::cout; + +#include "baseplus.h" + +// constructor for class BasePlusCommissionEmployee +BasePlusCommissionEmployee::BasePlusCommissionEmployee( + const string &first, const string &last, + const string &socialSecurityNumber, + double baseSalaryAmount, + double grossSalesAmount, + double rate ) + : CommissionEmployee( first, last, socialSecurityNumber, + grossSalesAmount, rate ) +{ + setBaseSalary( baseSalaryAmount ); + +} // end BasePlusCommissionEmployee constructor + +// set base-salaried commission worker's wage +void BasePlusCommissionEmployee::setBaseSalary( double salary ) +{ + baseSalary = salary < 0.0 ? 0.0 : salary; + +} // end function setBaseSalary + +// return base-salaried commission worker's base salary +double BasePlusCommissionEmployee::getBaseSalary() const +{ + return baseSalary; + +} // end function getBaseSalary + +// return base-salaried commission worker's earnings +double BasePlusCommissionEmployee::earnings() const +{ + return getBaseSalary() + CommissionEmployee::earnings(); + +} // end function earnings + +// print base-salaried commission worker's name +void BasePlusCommissionEmployee::print() const +{ + cout << "\nbase-salaried commission worker: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h new file mode 100644 index 0000000..d049914 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/baseplus.h @@ -0,0 +1,40 @@ +// Fig. 10.31: baseplus.h +// BasePlusCommissionEmployee class derived from Employee. +#ifndef BASEPLUS_H +#define BASEPLUS_H + +#include "commission.h" // Employee class definition + +class BasePlusCommissionEmployee : public CommissionEmployee { + +public: + BasePlusCommissionEmployee( const string &, const string &, + const string &, double = 0.0, double = 0.0, double = 0.0 ); + + void setBaseSalary( double ); + double getBaseSalary() const; + + virtual double earnings() const; + virtual void print() const; + +private: + double baseSalary; // base salary per week + +}; // end class BasePlusCommissionEmployee + +#endif // BASEPLUS_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp new file mode 100644 index 0000000..2d05932 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.cpp @@ -0,0 +1,76 @@ +// Fig. 10.30: commission.cpp +// CommissionEmployee class member-function definitions. +#include + +using std::cout; + +#include "commission.h" // Commission class + +// CommissionEmployee constructor +CommissionEmployee::CommissionEmployee( const string &first, + const string &last, const string &socialSecurityNumber, + double grossWeeklySales, double percent ) + : Employee( first, last, socialSecurityNumber ) +{ + setGrossSales( grossWeeklySales ); + setCommissionRate( percent ); + +} // end CommissionEmployee constructor + +// return commission worker's rate +double CommissionEmployee::getCommissionRate() const +{ + return commissionRate; + +} // end function getCommissionRate + +// return commission worker's gross sales amount +double CommissionEmployee::getGrossSales() const +{ + return grossSales; + +} // end function getGrossSales + +// set commission worker's weekly base salary +void CommissionEmployee::setGrossSales( double sales ) +{ + grossSales = sales < 0.0 ? 0.0 : sales; + +} // end function setGrossSales + +// set commission worker's commission +void CommissionEmployee::setCommissionRate( double rate ) +{ + commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; + +} // end function setCommissionRate + +// calculate commission worker's earnings +double CommissionEmployee::earnings() const +{ + return getCommissionRate() * getGrossSales(); + +} // end function earnings + +// print commission worker's name +void CommissionEmployee::print() const +{ + cout << "\ncommission employee: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h new file mode 100644 index 0000000..423bd03 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/commission.h @@ -0,0 +1,44 @@ +// Fig. 10.29: commission.h +// CommissionEmployee class derived from Employee. +#ifndef COMMISSION_H +#define COMMISSION_H + +#include "employee.h" // Employee class definition + +class CommissionEmployee : public Employee { + +public: + CommissionEmployee( const string &, const string &, + const string &, double = 0.0, double = 0.0 ); + + void setCommissionRate( double ); + double getCommissionRate() const; + + void setGrossSales( double ); + double getGrossSales() const; + + virtual double earnings() const; + virtual void print() const; + +private: + double grossSales; // gross weekly sales + double commissionRate; // commission percentage + +}; // end class CommissionEmployee + +#endif // COMMISSION_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp new file mode 100644 index 0000000..151aa58 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.cpp @@ -0,0 +1,86 @@ +// Fig. 10.24: employee.cpp +// Abstract-base-class Employee member-function definitions. +// Note: No definitions are given for pure virtual functions. +#include + +using std::cout; +using std::endl; + +#include "employee.h" // Employee class definition + +// constructor +Employee::Employee( const string &first, const string &last, + const string &SSN ) + : firstName( first ), + lastName( last ), + socialSecurityNumber( SSN ) +{ + // empty body + +} // end Employee constructor + +// return first name +string Employee::getFirstName() const +{ + return firstName; + +} // end function getFirstName + +// return last name +string Employee::getLastName() const +{ + return lastName; + +} // end function getLastName + +// return social security number +string Employee::getSocialSecurityNumber() const +{ + return socialSecurityNumber; + +} // end function getSocialSecurityNumber + +// set first name +void Employee::setFirstName( const string &first ) +{ + firstName = first; + +} // end function setFirstName + +// set last name +void Employee::setLastName( const string &last ) +{ + lastName = last; + +} // end function setLastName + +// set social security number +void Employee::setSocialSecurityNumber( const string &number ) +{ + socialSecurityNumber = number; // should validate + +} // end function setSocialSecurityNumber + +// print Employee's information +void Employee::print() const +{ + cout << getFirstName() << ' ' << getLastName() + << "\nsocial security number: " + << getSocialSecurityNumber() << endl; + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h new file mode 100644 index 0000000..50ab131 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/employee.h @@ -0,0 +1,50 @@ +// Fig. 10.23: employee.h +// Employee abstract base class. +#ifndef EMPLOYEE_H +#define EMPLOYEE_H + +#include // C++ standard string class + +using std::string; + +class Employee { + +public: + Employee( const string &, const string &, const string & ); + + void setFirstName( const string & ); + string getFirstName() const; + + void setLastName( const string & ); + string getLastName() const; + + void setSocialSecurityNumber( const string & ); + string getSocialSecurityNumber() const; + + // pure virtual function makes Employee abstract base class + virtual double earnings() const = 0; // pure virtual + virtual void print() const; // virtual + +private: + string firstName; + string lastName; + string socialSecurityNumber; + +}; // end class Employee + +#endif // EMPLOYEE_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp new file mode 100644 index 0000000..c8e9761 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/fig10_33.cpp @@ -0,0 +1,100 @@ +// Fig. 10.33: fig10_33.cpp +// Driver for Employee hierarchy. +#include + +using std::cout; +using std::endl; +using std::fixed; + +#include + +using std::setprecision; + +#include + +using std::vector; + +#include + +#include "employee.h" // Employee base class +#include "salaried.h" // SalariedEmployee class +#include "commission.h" // CommissionEmployee class +#include "baseplus.h" // BasePlusCommissionEmployee class +#include "hourly.h" // HourlyEmployee class + +int main() +{ + // set floating-point output formatting + cout << fixed << setprecision( 2 ); + + // create vector employees + vector < Employee * > employees( 4 ); + + // initialize vector with Employees + employees[ 0 ] = new SalariedEmployee( "John", "Smith", + "111-11-1111", 800.00 ); + employees[ 1 ] = new CommissionEmployee( "Sue", "Jones", + "222-22-2222", 10000, .06 ); + employees[ 2 ] = new BasePlusCommissionEmployee( "Bob", + "Lewis", "333-33-3333", 300, 5000, .04 ); + employees[ 3 ] = new HourlyEmployee( "Karen", "Price", + "444-44-4444", 16.75, 40 ); + + // generically process each element in vector employees + for ( int i = 0; i < employees.size(); i++ ) { + + // output employee information + employees[ i ]->print(); + + // downcast pointer + BasePlusCommissionEmployee *commissionPtr = + dynamic_cast < BasePlusCommissionEmployee * > + ( employees[ i ] ); + + // determine whether element points to base-salaried + // commission worker + if ( commissionPtr != 0 ) { + cout << "old base salary: $" + << commissionPtr->getBaseSalary() << endl; + commissionPtr->setBaseSalary( + 1.10 * commissionPtr->getBaseSalary() ); + cout << "new base salary with 10% increase is: $" + << commissionPtr->getBaseSalary() << endl; + + } // end if + + cout << "earned $" << employees[ i ]->earnings() << endl; + + } // end for + + // release memory held by vector employees + for ( int j = 0; j < employees.size(); j++ ) { + + // output class name + cout << "\ndeleting object of " + << typeid( *employees[ j ] ).name(); + + delete employees[ j ]; + + } // end for + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp new file mode 100644 index 0000000..1a517e1 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.cpp @@ -0,0 +1,80 @@ +// Fig. 10.28: hourly.cpp +// HourlyEmployee class member-function definitions. +#include + +using std::cout; + +#include "hourly.h" + +// constructor for class HourlyEmployee +HourlyEmployee::HourlyEmployee( const string &first, + const string &last, const string &socialSecurityNumber, + double hourlyWage, double hoursWorked ) + : Employee( first, last, socialSecurityNumber ) +{ + setWage( hourlyWage ); + setHours( hoursWorked ); + +} // end HourlyEmployee constructor + +// set hourly worker's wage +void HourlyEmployee::setWage( double wageAmount ) +{ + wage = wageAmount < 0.0 ? 0.0 : wageAmount; + +} // end function setWage + +// set hourly worker's hours worked +void HourlyEmployee::setHours( double hoursWorked ) +{ + hours = ( hoursWorked >= 0.0 && hoursWorked <= 168.0 ) ? + hoursWorked : 0.0; + +} // end function setHours + +// return hours worked +double HourlyEmployee::getHours() const +{ + return hours; + +} // end function getHours + +// return wage +double HourlyEmployee::getWage() const +{ + return wage; + +} // end function getWage + +// get hourly worker's pay +double HourlyEmployee::earnings() const +{ + if ( hours <= 40 ) // no overtime + return wage * hours; + else // overtime is paid at wage * 1.5 + return 40 * wage + ( hours - 40 ) * wage * 1.5; + +} // end function earnings + +// print hourly worker's information +void HourlyEmployee::print() const +{ + cout << "\nhourly employee: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h new file mode 100644 index 0000000..a74d4ce --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/hourly.h @@ -0,0 +1,44 @@ +// Fig. 10.27: hourly.h +// HourlyEmployee class definition. +#ifndef HOURLY_H +#define HOURLY_H + +#include "employee.h" // Employee class definition + +class HourlyEmployee : public Employee { + +public: + HourlyEmployee( const string &, const string &, + const string &, double = 0.0, double = 0.0); + + void setWage( double ); + double getWage() const; + + void setHours( double ); + double getHours() const; + + virtual double earnings() const; + virtual void print() const; + +private: + double wage; // wage per hour + double hours; // hours worked for week + +}; // end class HourlyEmployee + +#endif // HOURLY_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp new file mode 100644 index 0000000..3b9ed98 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.cpp @@ -0,0 +1,61 @@ +// Fig. 10.26: salaried.cpp +// SalariedEmployee class member-function definitions. +#include + +using std::cout; + +#include "salaried.h" // SalariedEmployee class definition + +// SalariedEmployee constructor +SalariedEmployee::SalariedEmployee( const string &first, + const string &last, const string &socialSecurityNumber, + double salary ) + : Employee( first, last, socialSecurityNumber ) +{ + setWeeklySalary( salary ); + +} // end SalariedEmployee constructor + +// set salaried worker's salary +void SalariedEmployee::setWeeklySalary( double salary ) +{ + weeklySalary = salary < 0.0 ? 0.0 : salary; + +} // end function setWeeklySalary + +// calculate salaried worker's pay +double SalariedEmployee::earnings() const +{ + return getWeeklySalary(); + +} // end function earnings + +// return salaried worker's salary +double SalariedEmployee::getWeeklySalary() const +{ + return weeklySalary; + +} // end function getWeeklySalary + +// print salaried worker's name +void SalariedEmployee::print() const +{ + cout << "\nsalaried employee: "; + Employee::print(); // code reuse + +} // end function print + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h new file mode 100644 index 0000000..80b2595 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/2_ch10/fig10_23_33/salaried.h @@ -0,0 +1,40 @@ +// Fig. 10.25: salaried.h +// SalariedEmployee class derived from Employee. +#ifndef SALARIED_H +#define SALARIED_H + +#include "employee.h" // Employee class definition + +class SalariedEmployee : public Employee { + +public: + SalariedEmployee( const string &, const string &, + const string &, double = 0.0 ); + + void setWeeklySalary( double ); + double getWeeklySalary() const; + + virtual double earnings() const; + virtual void print() const; // "salaried employee: " + +private: + double weeklySalary; + +}; // end class SalariedEmployee + +#endif // SALARIED_H + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP new file mode 100644 index 0000000..2c69128 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_01.CPP @@ -0,0 +1,68 @@ +// Fig. 16.1: fig16_01.cpp +// Demonstrating string assignment and concatenation. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string string1( "cat" ); + string string2; + string string3; + + string2 = string1; // assign string1 to string2 + string3.assign( string1 ); // assign string1 to string3 + cout << "string1: " << string1 << "\nstring2: " << string2 + << "\nstring3: " << string3 << "\n\n"; + + // modify string2 and string3 + string2[ 0 ] = string3[ 2 ] = 'r'; + + cout << "After modification of string2 and string3:\n" + << "string1: " << string1 << "\nstring2: " << string2 + << "\nstring3: "; + + // demonstrating member function at + for ( int i = 0; i < string3.length(); i++ ) + cout << string3.at( i ); + + // declare string4 and string5 + string string4( string1 + "apult" ); + string string5; + + // overloaded += + string3 += "pet"; // create "carpet" + string1.append( "acomb" ); // create "catacomb" + + // append subscript locations 4 through end of string1 to + // create string "comb" (string5 was initially empty) + string5.append( string1, 4, string1.length() ); + + cout << "\n\nAfter concatenation:\nstring1: " << string1 + << "\nstring2: " << string2 << "\nstring3: " + << string3 << "\nstring4: " << string4 + << "\nstring5: " << string5 << 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/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP new file mode 100644 index 0000000..37fa669 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_02.CPP @@ -0,0 +1,97 @@ +// Fig. 16.2: fig16_02.cpp +// Demonstrating string comparison capabilities. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string string1( "Testing the comparison functions." ); + string string2( "Hello" ); + string string3( "stinger" ); + string string4( string2 ); + + cout << "string1: " << string1 << "\nstring2: " << string2 + << "\nstring3: " << string3 << "\nstring4: " << string4 + << "\n\n"; + + // comparing string1 and string4 + if ( string1 == string4 ) + cout << "string1 == string4\n"; + else { // string1 != string4 + if ( string1 > string4 ) + cout << "string1 > string4\n"; + else // string1 < string4 + cout << "string1 < string4\n"; + } + + // comparing string1 and string2 + int result = string1.compare( string2 ); + + if ( result == 0 ) + cout << "string1.compare( string2 ) == 0\n"; + else // result != 0 + if ( result > 0 ) + cout << "string1.compare( string2 ) > 0\n"; + else // result < 0 + cout << "string1.compare( string2 ) < 0\n"; + + // comparing string1 (elements 2-5) and string3 (elements 0-5) + result = string1.compare( 2, 5, string3, 0, 5 ); + + if ( result == 0 ) + cout << "string1.compare( 2, 5, string3, 0, 5 ) == 0\n"; + else // result != 0 + if ( result > 0 ) + cout << "string1.compare( 2, 5, string3, 0, 5 ) > 0\n"; + else // result < 0 + cout << "string1.compare( 2, 5, string3, 0, 5 ) < 0\n"; + + // comparing string2 and string4 + result = string4.compare( 0, string2.length(), string2 ); + + if ( result == 0 ) + cout << "string4.compare( 0, string2.length(), " + << "string2 ) == 0" << endl; + else // result != 0 + if ( result > 0 ) + cout << "string4.compare( 0, string2.length(), " + << "string2 ) > 0" << endl; + else // result < 0 + cout << "string4.compare( 0, string2.length(), " + << "string2 ) < 0" << endl; + + // comparing string2 and string4 + result = string2.compare( 0, 3, string4 ); + + if ( result == 0 ) + cout << "string2.compare( 0, 3, string4 ) == 0" << endl; + else // result != 0 + if ( result > 0 ) + cout << "string2.compare( 0, 3, string4 ) > 0" << endl; + else // result < 0 + cout << "string2.compare( 0, 3, string4 ) < 0" << 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/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP new file mode 100644 index 0000000..40fd024 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_03.CPP @@ -0,0 +1,38 @@ +// Fig. 16.3: fig16_03.cpp +// Demonstrating string member function substr. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string string1( "The airplane landed on time." ); + + // retrieve substring "plane" which + // begins at subscript 7 and consists of 5 elements + cout << string1.substr( 7, 5 ) << 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/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP new file mode 100644 index 0000000..0747743 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_04.CPP @@ -0,0 +1,43 @@ +// Fig. 16.4: fig16_04.cpp +// Using the swap function to swap two strings. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string first( "one" ); + string second( "two" ); + + // output strings + cout << "Before swap:\n first: " << first + << "\nsecond: " << second; + + first.swap( second ); // swap strings + + cout << "\n\nAfter swap:\n first: " << first + << "\nsecond: " << second << 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP new file mode 100644 index 0000000..6fc20c4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_05.CPP @@ -0,0 +1,75 @@ +// Fig. 16.5: fig16_05.cpp +// Demonstrating member functions related to size and capacity. +#include + +using std::cout; +using std::endl; +using std::cin; +using std::boolalpha; + +#include + +using std::string; + +void printStatistics( const string & ); + +int main() +{ + string string1; + + cout << "Statistics before input:\n" << boolalpha; + printStatistics( string1 ); + + // read in "tomato" + cout << "\n\nEnter a string: "; + cin >> string1; // delimited by whitespace + cout << "The string entered was: " << string1; + + cout << "\nStatistics after input:\n"; + printStatistics( string1 ); + + // read in "soup" + cin >> string1; // delimited by whitespace + cout << "\n\nThe remaining string is: " << string1 << endl; + printStatistics( string1 ); + + // append 46 characters to string1 + string1 += "1234567890abcdefghijklmnopqrstuvwxyz1234567890"; + cout << "\n\nstring1 is now: " << string1 << endl; + printStatistics( string1 ); + + // add 10 elements to string1 + string1.resize( string1.length() + 10 ); + cout << "\n\nStats after resizing by (length + 10):\n"; + printStatistics( string1 ); + + cout << endl; + return 0; + +} // end main + +// display string statistics +void printStatistics( const string &stringRef ) +{ + cout << "capacity: " << stringRef.capacity() + << "\nmax size: " << stringRef.max_size() + << "\nsize: " << stringRef.size() + << "\nlength: " << stringRef.length() + << "\nempty: " << stringRef.empty(); + +} // end printStatistics + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP new file mode 100644 index 0000000..10a73aa --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_06.CPP @@ -0,0 +1,71 @@ +// Fig. 16.6: fig16_06.cpp +// Demonstrating the string find member functions. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string string1( "noon is 12 p.m." ); + int location; + + // find "is" at location 5 + cout << "Original string:\n" << string1 + << "\n\n(find) \"is\" was found at: " + << string1.find( "is" ) + << "\n(rfind) \"is\" was found at: " + << string1.rfind( "is" ); + + // find 'o' at location 1 + location = string1.find_first_of( "misop" ); + + cout << "\n\n(find_first_of) found '" << string1[ location ] + << "' from the group \"misop\" at: " + << location; + + // find 'm' at location 13 + location = string1.find_last_of( "misop" ); + cout << "\n\n(find_last_of) found '" << string1[ location ] + << "' from the group \"misop\" at: " + << location; + + // find '1' at location 8 + location = string1.find_first_not_of( "noi spm" ); + cout << "\n\n(find_first_not_of) '" << string1[ location ] + << "' is not contained in \"noi spm\" and was found at:" + << location; + + // find '.' at location 12 + location = string1.find_first_not_of( "12noi spm" ); + cout << "\n\n(find_first_not_of) '" << string1[ location ] + << "' is not contained in \"12noi spm\" and was " + << "found at:" << location << endl; + + // search for characters not in string1 + location = string1.find_first_not_of( "noon is 12 p.m." ); + cout << "\nfind_first_not_of(\"noon is 12 p.m.\")" + << " returned: " << location << 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP new file mode 100644 index 0000000..c09a04f --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_07.CPP @@ -0,0 +1,69 @@ +// Fig. 16.7: fig16_07.cpp +// Demonstrating string member functions erase and replace. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + // compiler concatenates all parts into one string + string string1( "The values in any left subtree" + "\nare less than the value in the" + "\nparent node and the values in" + "\nany right subtree are greater" + "\nthan the value in the parent node" ); + + cout << "Original string:\n" << string1 << endl << endl; + + // remove all characters from (and including) location 62 + // through the end of string1 + string1.erase( 62 ); + + // output new string + cout << "Original string after erase:\n" << string1 + << "\n\nAfter first replacement:\n"; + + // replace all spaces with period + int position = string1.find( " " ); + + while ( position != string::npos ) { + string1.replace( position, 1, "." ); + position = string1.find( " ", position + 1 ); + } // end while + + cout << string1 << "\n\nAfter second replacement:\n"; + + // replace all periods with two semicolons + // NOTE: this will overwrite characters + position = string1.find( "." ); + + while ( position != string::npos ) { + string1.replace( position, 2, "xxxxx;;yyy", 5, 2 ); + position = string1.find( ".", position + 1 ); + } // end while + + cout << string1 << 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/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP new file mode 100644 index 0000000..98bc3d9 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_08.CPP @@ -0,0 +1,50 @@ +// Fig. 16.8: fig16_08.cpp +// Demonstrating class string insert member functions. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string string1( "beginning end" ); + string string2( "middle " ); + string string3( "12345678" ); + string string4( "xx" ); + + cout << "Initial strings:\nstring1: " << string1 + << "\nstring2: " << string2 << "\nstring3: " << string3 + << "\nstring4: " << string4 << "\n\n"; + + // insert "middle" at location 10 in string1 + string1.insert( 10, string2 ); + + // insert "xx" at location 3 in string3 + string3.insert( 3, string4, 0, string::npos ); + + cout << "Strings after insert:\nstring1: " << string1 + << "\nstring2: " << string2 << "\nstring3: " << string3 + << "\nstring4: " << string4 << 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/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP new file mode 100644 index 0000000..d94ff92 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_09.CPP @@ -0,0 +1,57 @@ +// Fig. 16.9: fig16_09.cpp +// Converting to C-style strings. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string string1( "STRINGS" ); + const char *ptr1 = 0; + int length = string1.length(); + char *ptr2 = new char[ length + 1 ]; // including null + + // copy characters from string1 into allocated memory + string1.copy( ptr2, length, 0 ); + ptr2[ length ] = '\0'; // add null terminator + + // output + cout << "string s is " << string1 + << "\nstring1 converted to a C-Style string is " + << string1.c_str() << "\nptr1 is "; + + // Assign to pointer ptr1 the const char * returned by + // function data(). NOTE: this is a potentially dangerous + // assignment. If string1 is modified, pointer ptr1 can + // become invalid. + ptr1 = string1.data(); + + // output each character using pointer + for ( int i = 0; i < length; i++ ) + cout << *( ptr1 + i ); // use pointer arithmetic + + cout << "\nptr2 is " << ptr2 << endl; + delete [] ptr2; + 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/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP new file mode 100644 index 0000000..c65136e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/FIG15_10.CPP @@ -0,0 +1,44 @@ +// Fig. 16.10: fig16_10.cpp +// Using an iterator to output a string. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +int main() +{ + string string1( "Testing iterators" ); + string::const_iterator iterator1 = string1.begin(); + + cout << "string1 = " << string1 + << "\n(Using iterator iterator1) string1 is: "; + + // iterate through string + while ( iterator1 != string1.end() ) { + cout << *iterator1; // dereference iterator to get char + ++iterator1; // advance iterator to next char + } // end while + + 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/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp new file mode 100644 index 0000000..1b55f71 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_11.cpp @@ -0,0 +1,59 @@ +// Fig. 16.11: fig16_11.cpp +// Using a dynamically allocated ostringstream object. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +#include + +using std::ostringstream; + +int main() +{ + ostringstream outputString; // create ostringstream instance + + string string1( "Output of several data types " ); + string string2( "to an ostringstream object:" ); + string string3( "\n double: " ); + string string4( "\n int: " ); + string string5( "\naddress of int: " ); + + double double1 = 123.4567; + int integer = 22; + + // output strings, double and int to outputString + outputString << string1 << string2 << string3 << double1 + << string4 << integer << string5 << &integer; + + // call str to output contents + cout << "outputString contains:\n" << outputString.str(); + + // add additional characters and call str to output string + outputString << "\nmore characters added"; + cout << "\n\nafter additional stream insertions,\n" + << "outputString contains:\n" << outputString.str() + << 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/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp new file mode 100644 index 0000000..93bb65b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/3_ch15/Fig15_12.cpp @@ -0,0 +1,65 @@ +// Fig. 16.12: fig16_12.cpp +// Demonstrating input from an istringstream object. +#include + +using std::cout; +using std::endl; + +#include + +using std::string; + +#include + +using std::istringstream; + +int main() +{ + string input( "Input test 123 4.7 A" ); + istringstream inputString( input ); + string string1; + string string2; + int integer; + double double1; + char character; + + inputString >> string1 >> string2 >> integer >> double1 + >> character; + + cout << "The following items were extracted\n" + << "from the istringstream object:" + << "\nstring: " << string1 + << "\nstring: " << string2 + << "\n int: " << integer + << "\ndouble: " << double1 + << "\n char: " << character; + + // attempt to read from empty stream + long value; + + inputString >> value; + + // test stream results + if ( inputString.good() ) + cout << "\n\nlong value is: " << value << endl; + else + cout << "\n\ninputString is empty" << 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/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp new file mode 100644 index 0000000..f132e11 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_04.cpp @@ -0,0 +1,61 @@ +// Fig. 14.4: fig14_04.cpp +// Create a sequential file. +#include + +using std::cout; +using std::cin; +using std::ios; +using std::cerr; +using std::endl; + +#include + +using std::ofstream; + +#include // exit prototype + +int main() +{ + // ofstream constructor opens file + ofstream outClientFile( "clients.dat", ios::out ); + + // exit program if unable to create file + if ( !outClientFile ) { // overloaded ! operator + cerr << "File could not be opened" << endl; + exit( 1 ); + + } // end if + + cout << "Enter the account, name, and balance." << endl + << "Enter end-of-file to end input.\n? "; + + int account; + char name[ 30 ]; + double balance; + + // read account, name and balance from cin, then place in file + while ( cin >> account >> name >> balance ) { + outClientFile << account << ' ' << name << ' ' << balance + << endl; + cout << "? "; + + } // end while + + return 0; // ofstream destructor closes file + +} // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp new file mode 100644 index 0000000..7067985 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_07.cpp @@ -0,0 +1,78 @@ +// Fig. 14.7: fig14_07.cpp +// Reading and printing a sequential file. +#include + +using std::cout; +using std::cin; +using std::ios; +using std::cerr; +using std::endl; +using std::left; +using std::right; +using std::fixed; +using std::showpoint; + +#include + +using std::ifstream; + +#include + +using std::setw; +using std::setprecision; + +#include // exit prototype + +void outputLine( int, const char * const, double ); + +int main() +{ + // ifstream constructor opens the file + ifstream inClientFile( "clients.dat", ios::in ); + + // exit program if ifstream could not open file + if ( !inClientFile ) { + cerr << "File could not be opened" << endl; + exit( 1 ); + + } // end if + + int account; + char name[ 30 ]; + double balance; + + cout << left << setw( 10 ) << "Account" << setw( 13 ) + << "Name" << "Balance" << endl << fixed << showpoint; + + // display each record in file + while ( inClientFile >> account >> name >> balance ) + outputLine( account, name, balance ); + + return 0; // ifstream destructor closes the file + +} // end main + +// display single record from file +void outputLine( int account, const char * const name, + double balance ) +{ + cout << left << setw( 10 ) << account << setw( 13 ) << name + << setw( 7 ) << setprecision( 2 ) << right << balance + << endl; + +} // end function outputLine + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp new file mode 100644 index 0000000..ec49dee --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_08.cpp @@ -0,0 +1,163 @@ +// Fig. 14.8: fig14_08.cpp +// Credit inquiry program. +#include + +using std::cout; +using std::cin; +using std::ios; +using std::cerr; +using std::endl; +using std::fixed; +using std::showpoint; +using std::left; +using std::right; + +#include + +using std::ifstream; + +#include + +using std::setw; +using std::setprecision; + +#include + +enum RequestType { ZERO_BALANCE = 1, CREDIT_BALANCE, + DEBIT_BALANCE, END }; +int getRequest(); +bool shouldDisplay( int, double ); +void outputLine( int, const char * const, double ); + +int main() +{ + // ifstream constructor opens the file + ifstream inClientFile( "clients.dat", ios::in ); + + // exit program if ifstream could not open file + if ( !inClientFile ) { + cerr << "File could not be opened" << endl; + exit( 1 ); + + } // end if + + int request; + int account; + char name[ 30 ]; + double balance; + + // get user's request (e.g., zero, credit or debit balance) + request = getRequest(); + + // process user's request + while ( request != END ) { + + switch ( request ) { + + case ZERO_BALANCE: + cout << "\nAccounts with zero balances:\n"; + break; + + case CREDIT_BALANCE: + cout << "\nAccounts with credit balances:\n"; + break; + + case DEBIT_BALANCE: + cout << "\nAccounts with debit balances:\n"; + break; + + } // end switch + + // read account, name and balance from file + inClientFile >> account >> name >> balance; + + // display file contents (until eof) + while ( !inClientFile.eof() ) { + + // display record + if ( shouldDisplay( request, balance ) ) + outputLine( account, name, balance ); + + // read account, name and balance from file + inClientFile >> account >> name >> balance; + + } // end inner while + + inClientFile.clear(); // reset eof for next input + inClientFile.seekg( 0 ); // move to beginning of file + request = getRequest(); // get additional request from user + + } // end outer while + + cout << "End of run." << endl; + + return 0; // ifstream destructor closes the file + +} // end main + +// obtain request from user +int getRequest() +{ + int request; + + // display request options + cout << "Enter request" << endl + << " 1 - List accounts with zero balances" << endl + << " 2 - List accounts with credit balances" << endl + << " 3 - List accounts with debit balances" << endl + << " 4 - End of run" << fixed << showpoint; + + // input user request + do { + cout << "\n? "; + cin >> request; + + } while( request < ZERO_BALANCE && request > END ); + + return request; + +} // end function getRequest + +// determine whether to display given record +bool shouldDisplay( int type, double balance ) +{ + // determine whether to display credit balances + if ( type == CREDIT_BALANCE && balance < 0 ) + return true; + + // determine whether to display debit balances + if ( type == DEBIT_BALANCE && balance > 0 ) + return true; + + // determine whether to display zero balances + if ( type == ZERO_BALANCE && balance == 0 ) + return true; + + return false; + +} // end function shouldDisplay + +// display single record from file +void outputLine( int account, const char * const name, + double balance ) +{ + cout << left << setw( 10 ) << account << setw( 13 ) << name + << setw( 7 ) << setprecision( 2 ) << right << balance + << endl; + +} // end function outputLine + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp new file mode 100644 index 0000000..48ba334 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_12.cpp @@ -0,0 +1,53 @@ +// Fig. 14.12: fig14_12.cpp +// Creating a randomly accessed file. +#include + +using std::cerr; +using std::endl; +using std::ios; + +#include + +using std::ofstream; + +#include +#include "clientData.h" // ClientData class definition + +int main() +{ + ofstream outCredit( "credit.dat", ios::binary ); + + // exit program if ofstream could not open file + if ( !outCredit ) { + cerr << "File could not be opened." << endl; + exit( 1 ); + + } // end if + + // create ClientData with no information + ClientData blankClient; + + // output 100 blank records to file + for ( int i = 0; i < 100; i++ ) + outCredit.write( + reinterpret_cast< const char * >( &blankClient ), + sizeof( ClientData ) ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp new file mode 100644 index 0000000..fefa264 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_13.cpp @@ -0,0 +1,94 @@ +// Fig. 14.13: fig14_13.cpp +// Writing to a random access file. +#include + +using std::cerr; +using std::endl; +using std::cout; +using std::cin; +using std::ios; + +#include + +using std::setw; + +#include + +using std::ofstream; + +#include +#include "clientData.h" // ClientData class definition + +int main() +{ + int accountNumber; + char lastName[ 15 ]; + char firstName[ 10 ]; + double balance; + + ofstream outCredit( "credit.dat", ios::binary ); + + // exit program if ofstream cannot open file + if ( !outCredit ) { + cerr << "File could not be opened." << endl; + exit( 1 ); + + } // end if + + cout << "Enter account number " + << "(1 to 100, 0 to end input)\n? "; + + // require user to specify account number + ClientData client; + cin >> accountNumber; + client.setAccountNumber( accountNumber ); + + // user enters information, which is copied into file + while ( client.getAccountNumber() > 0 && + client.getAccountNumber() <= 100 ) { + + // user enters last name, first name and balance + cout << "Enter lastname, firstname, balance\n? "; + cin >> setw( 15 ) >> lastName; + cin >> setw( 10 ) >> firstName; + cin >> balance; + + // set record lastName, firstName and balance values + client.setLastName( lastName ); + client.setFirstName( firstName ); + client.setBalance( balance ); + + // seek position in file of user-specified record + outCredit.seekp( ( client.getAccountNumber() - 1 ) * + sizeof( ClientData ) ); + + // write user-specified information in file + outCredit.write( + reinterpret_cast< const char * >( &client ), + sizeof( ClientData ) ); + + // enable user to specify another account number + cout << "Enter account number\n? "; + cin >> accountNumber; + client.setAccountNumber( accountNumber ); + + } // end while + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp new file mode 100644 index 0000000..6a9728e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_14.cpp @@ -0,0 +1,91 @@ +// Fig. 14.14: fig14_14.cpp +// Reading a random access file. +#include + +using std::cout; +using std::endl; +using std::ios; +using std::cerr; +using std::left; +using std::right; +using std::fixed; +using std::showpoint; + +#include + +using std::setprecision; +using std::setw; + +#include + +using std::ifstream; +using std::ostream; + +#include +#include "clientData.h" // ClientData class definition + +void outputLine( ostream&, const ClientData & ); + +int main() +{ + ifstream inCredit( "credit.dat", ios::in ); + + // exit program if ifstream cannot open file + if ( !inCredit ) { + cerr << "File could not be opened." << endl; + exit( 1 ); + + } // end if + + cout << left << setw( 10 ) << "Account" << setw( 16 ) + << "Last Name" << setw( 11 ) << "First Name" << left + << setw( 10 ) << right << "Balance" << endl; + + ClientData client; // create record + + // read first record from file + inCredit.read( reinterpret_cast< char * >( &client ), + sizeof( ClientData ) ); + + // read all records from file + while ( inCredit && !inCredit.eof() ) { + + // display record + if ( client.getAccountNumber() != 0 ) + outputLine( cout, client ); + + // read next from file + inCredit.read( reinterpret_cast< char * >( &client ), + sizeof( ClientData ) ); + + } // end while + + return 0; + +} // end main + +// display single record +void outputLine( ostream &output, const ClientData &record ) +{ + output << left << setw( 10 ) << record.getAccountNumber() + << setw( 16 ) << record.getLastName().data() + << setw( 11 ) << record.getFirstName().data() + << setw( 10 ) << setprecision( 2 ) << right << fixed + << showpoint << record.getBalance() << endl; + +} // end outputLine + +/************************************************************************** + * (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/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp new file mode 100644 index 0000000..da2ef2e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/Fig14_15.cpp @@ -0,0 +1,331 @@ +// Fig. 14.15: fig14_15.cpp +// This program reads a random access file sequentially, updates +// data previously written to the file, creates data to be placed +// in the file, and deletes data previously in the file. +#include + +using std::cout; +using std::cerr; +using std::cin; +using std::endl; +using std::ios; +using std::left; +using std::right; +using std::fixed; +using std::showpoint; + +#include + +using std::ofstream; +using std::ostream; +using std::fstream; + +#include + +using std::setw; +using std::setprecision; + +#include +#include "clientData.h" // ClientData class definition + +int enterChoice(); +void printRecord( fstream& ); +void updateRecord( fstream& ); +void newRecord( fstream& ); +void deleteRecord( fstream& ); +void outputLine( ostream&, const ClientData & ); +int getAccount( const char * const ); + +enum Choices { PRINT = 1, UPDATE, NEW, DELETE, END }; + +int main() +{ + // open file for reading and writing + fstream inOutCredit( "credit.dat", ios::in | ios::out ); + + // exit program if fstream cannot open file + if ( !inOutCredit ) { + cerr << "File could not be opened." << endl; + exit ( 1 ); + + } // end if + + int choice; + + // enable user to specify action + while ( ( choice = enterChoice() ) != END ) { + + switch ( choice ) { + + // create text file from record file + case PRINT: + printRecord( inOutCredit ); + break; + + // update record + case UPDATE: + updateRecord( inOutCredit ); + break; + + // create record + case NEW: + newRecord( inOutCredit ); + break; + + // delete existing record + case DELETE: + deleteRecord( inOutCredit ); + break; + + // display error if user does not select valid choice + default: + cerr << "Incorrect choice" << endl; + break; + + } // end switch + + inOutCredit.clear(); // reset end-of-file indicator + + } // end while + + return 0; + +} // end main + +// enable user to input menu choice +int enterChoice() +{ + // display available options + cout << "\nEnter your choice" << endl + << "1 - store a formatted text file of accounts" << endl + << " called \"print.txt\" for printing" << endl + << "2 - update an account" << endl + << "3 - add a new account" << endl + << "4 - delete an account" << endl + << "5 - end program\n? "; + + int menuChoice; + cin >> menuChoice; // receive choice from user + + return menuChoice; + +} // end function enterChoice + +// create formatted text file for printing +void printRecord( fstream &readFromFile ) +{ + // create text file + ofstream outPrintFile( "print.txt", ios::out ); + + // exit program if ofstream cannot create file + if ( !outPrintFile ) { + cerr << "File could not be created." << endl; + exit( 1 ); + + } // end if + + outPrintFile << left << setw( 10 ) << "Account" << setw( 16 ) + << "Last Name" << setw( 11 ) << "First Name" << right + << setw( 10 ) << "Balance" << endl; + + // set file-position pointer to beginning of record file + readFromFile.seekg( 0 ); + + // read first record from record file + ClientData client; + readFromFile.read( reinterpret_cast< char * >( &client ), + sizeof( ClientData ) ); + + // copy all records from record file into text file + while ( !readFromFile.eof() ) { + + // write single record to text file + if ( client.getAccountNumber() != 0 ) + outputLine( outPrintFile, client ); + + // read next record from record file + readFromFile.read( reinterpret_cast< char * >( &client ), + sizeof( ClientData ) ); + + } // end while + +} // end function printRecord + +// update balance in record +void updateRecord( fstream &updateFile ) +{ + // obtain number of account to update + int accountNumber = getAccount( "Enter account to update" ); + + // move file-position pointer to correct record in file + updateFile.seekg( + ( accountNumber - 1 ) * sizeof( ClientData ) ); + + // read first record from file + ClientData client; + updateFile.read( reinterpret_cast< char * >( &client ), + sizeof( ClientData ) ); + + // update record + if ( client.getAccountNumber() != 0 ) { + outputLine( cout, client ); + + // request user to specify transaction + cout << "\nEnter charge (+) or payment (-): "; + double transaction; // charge or payment + cin >> transaction; + + // update record balance + double oldBalance = client.getBalance(); + client.setBalance( oldBalance + transaction ); + outputLine( cout, client ); + + // move file-position pointer to correct record in file + updateFile.seekp( + ( accountNumber - 1 ) * sizeof( ClientData ) ); + + // write updated record over old record in file + updateFile.write( + reinterpret_cast< const char * >( &client ), + sizeof( ClientData ) ); + + } // end if + + // display error if account does not exist + else + cerr << "Account #" << accountNumber + << " has no information." << endl; + +} // end function updateRecord + +// create and insert record +void newRecord( fstream &insertInFile ) +{ + // obtain number of account to create + int accountNumber = getAccount( "Enter new account number" ); + + // move file-position pointer to correct record in file + insertInFile.seekg( + ( accountNumber - 1 ) * sizeof( ClientData ) ); + + // read record from file + ClientData client; + insertInFile.read( reinterpret_cast< char * >( &client ), + sizeof( ClientData ) ); + + // create record, if record does not previously exist + if ( client.getAccountNumber() == 0 ) { + + char lastName[ 15 ]; + char firstName[ 10 ]; + double balance; + + // user enters last name, first name and balance + cout << "Enter lastname, firstname, balance\n? "; + cin >> setw( 15 ) >> lastName; + cin >> setw( 10 ) >> firstName; + cin >> balance; + + // use values to populate account values + client.setLastName( lastName ); + client.setFirstName( firstName ); + client.setBalance( balance ); + client.setAccountNumber( accountNumber ); + + // move file-position pointer to correct record in file + insertInFile.seekp( ( accountNumber - 1 ) * + sizeof( ClientData ) ); + + // insert record in file + insertInFile.write( + reinterpret_cast< const char * >( &client ), + sizeof( ClientData ) ); + + } // end if + + // display error if account previously exists + else + cerr << "Account #" << accountNumber + << " already contains information." << endl; + +} // end function newRecord + +// delete an existing record +void deleteRecord( fstream &deleteFromFile ) +{ + // obtain number of account to delete + int accountNumber = getAccount( "Enter account to delete" ); + + // move file-position pointer to correct record in file + deleteFromFile.seekg( + ( accountNumber - 1 ) * sizeof( ClientData ) ); + + // read record from file + ClientData client; + deleteFromFile.read( reinterpret_cast< char * >( &client ), + sizeof( ClientData ) ); + + // delete record, if record exists in file + if ( client.getAccountNumber() != 0 ) { + ClientData blankClient; + + // move file-position pointer to correct record in file + deleteFromFile.seekp( ( accountNumber - 1 ) * + sizeof( ClientData ) ); + + // replace existing record with blank record + deleteFromFile.write( + reinterpret_cast< const char * >( &blankClient ), + sizeof( ClientData ) ); + + cout << "Account #" << accountNumber << " deleted.\n"; + + } // end if + + // display error if record does not exist + else + cerr << "Account #" << accountNumber << " is empty.\n"; + +} // end deleteRecord + +// display single record +void outputLine( ostream &output, const ClientData &record ) +{ + output << left << setw( 10 ) << record.getAccountNumber() + << setw( 16 ) << record.getLastName().data() + << setw( 11 ) << record.getFirstName().data() + << setw( 10 ) << setprecision( 2 ) << right << fixed + << showpoint << record.getBalance() << endl; + +} // end function outputLine + +// obtain account-number value from user +int getAccount( const char * const prompt ) +{ + int accountNumber; + + // obtain account-number value + do { + cout << prompt << " (1 - 100): "; + cin >> accountNumber; + + } while ( accountNumber < 1 || accountNumber > 100 ); + + return accountNumber; + +} // end function getAccount + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp new file mode 100644 index 0000000..3885357 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.cpp @@ -0,0 +1,105 @@ +// Fig. 14.11: ClientData.cpp +// Class ClientData stores customer's credit information. +#include + +using std::string; + +#include +#include "clientData.h" + +// default ClientData constructor +ClientData::ClientData( int accountNumberValue, + string lastNameValue, string firstNameValue, + double balanceValue ) +{ + setAccountNumber( accountNumberValue ); + setLastName( lastNameValue ); + setFirstName( firstNameValue ); + setBalance( balanceValue ); + +} // end ClientData constructor + +// get account-number value +int ClientData::getAccountNumber() const +{ + return accountNumber; + +} // end function getAccountNumber + +// set account-number value +void ClientData::setAccountNumber( int accountNumberValue ) +{ + accountNumber = accountNumberValue; + +} // end function setAccountNumber + +// get last-name value +string ClientData::getLastName() const +{ + return lastName; + +} // end function getLastName + +// set last-name value +void ClientData::setLastName( string lastNameString ) +{ + // copy at most 15 characters from string to lastName + const char *lastNameValue = lastNameString.data(); + int length = strlen( lastNameValue ); + length = ( length < 15 ? length : 14 ); + strncpy( lastName, lastNameValue, length ); + + // append null character to lastName + lastName[ length ] = '\0'; + +} // end function setLastName + +// get first-name value +string ClientData::getFirstName() const +{ + return firstName; + +} // end function getFirstName + +// set first-name value +void ClientData::setFirstName( string firstNameString ) +{ + // copy at most 10 characters from string to firstName + const char *firstNameValue = firstNameString.data(); + int length = strlen( firstNameValue ); + length = ( length < 10 ? length : 9 ); + strncpy( firstName, firstNameValue, length ); + + // append new-line character to firstName + firstName[ length ] = '\0'; + +} // end function setFirstName + +// get balance value +double ClientData::getBalance() const +{ + return balance; + +} // end function getBalance + +// set balance value +void ClientData::setBalance( double balanceValue ) +{ + balance = balanceValue; + +} // end function setBalance + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h new file mode 100644 index 0000000..37afb23 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/4_ch14/clientData.h @@ -0,0 +1,56 @@ +// Fig. 14.10: clientData.h +// Class ClientData definition used in Fig. 14.12–Fig. 14.15. +#ifndef CLIENTDATA_H +#define CLIENTDATA_H + +#include + +using std::string; + +class ClientData { + +public: + + // default ClientData constructor + ClientData( int = 0, string = "", string = "", double = 0.0 ); + + // accessor functions for accountNumber + void setAccountNumber( int ); + int getAccountNumber() const; + + // accessor functions for lastName + void setLastName( string ); + string getLastName() const; + + // accessor functions for firstName + void setFirstName( string ); + string getFirstName() const; + + // accessor functions for balance + void setBalance( double ); + double getBalance() const; + +private: + int accountNumber; + char lastName[ 15 ]; + char firstName[ 10 ]; + double balance; + +}; // end class ClientData + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h b/Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h new file mode 100644 index 0000000..d689030 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/5_ch11/Tstack1.h @@ -0,0 +1,98 @@ +// Fig. 11.2: tstack1.h +// Stack class template. +#ifndef TSTACK1_H +#define TSTACK1_H + +template< class T > +class Stack { + +public: + Stack( int = 10 ); // default constructor (stack size 10) + + // destructor + ~Stack() + { + delete [] stackPtr; + + } // end ~Stack destructor + + bool push( const T& ); // push an element onto the stack + bool pop( T& ); // pop an element off the stack + + // determine whether Stack is empty + bool isEmpty() const + { + return top == -1; + + } // end function isEmpty + + // determine whether Stack is full + bool isFull() const + { + return top == size - 1; + + } // end function isFull + +private: + int size; // # of elements in the stack + int top; // location of the top element + T *stackPtr; // pointer to the stack + +}; // end class Stack + +// constructor +template< class T > +Stack< T >::Stack( int s ) +{ + size = s > 0 ? s : 10; + top = -1; // Stack initially empty + stackPtr = new T[ size ]; // allocate memory for elements + +} // end Stack constructor + +// push element onto stack; +// if successful, return true; otherwise, return false +template< class T > +bool Stack< T >::push( const T &pushValue ) +{ + if ( !isFull() ) { + stackPtr[ ++top ] = pushValue; // place item on Stack + return true; // push successful + + } // end if + + return false; // push unsuccessful + +} // end function push + +// pop element off stack; +// if successful, return true; otherwise, return false +template< class T > +bool Stack< T >::pop( T &popValue ) +{ + if ( !isEmpty() ) { + popValue = stackPtr[ top-- ]; // remove item from Stack + return true; // pop successful + + } // end if + + return false; // pop unsuccessful + +} // end function pop + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp new file mode 100644 index 0000000..549cc6b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_01.cpp @@ -0,0 +1,59 @@ +// Fig 11.1: fig11_01.cpp +// Using template functions. +#include + +using std::cout; +using std::endl; + +// function template printArray definition +template< class T > +void printArray( const T *array, const int count ) +{ + for ( int i = 0; i < count; i++ ) + cout << array[ i ] << " "; + + cout << endl; + +} // end function printArray + +int main() +{ + const int aCount = 5, bCount = 7, cCount = 6; + + int a[ aCount ] = { 1, 2, 3, 4, 5 }; + double b[ bCount ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; + char c[ cCount ] = "HELLO"; // 6th position for null + + cout << "Array a contains:" << endl; + + // call integer function-template specialization + printArray( a, aCount ); + + cout << "Array b contains:" << endl; + + // call double function-template specialization + printArray( b, bCount ); + + cout << "Array c contains:" << endl; + + // call character function-template specialization + printArray( c, cCount ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp new file mode 100644 index 0000000..725cbe1 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_03.cpp @@ -0,0 +1,67 @@ +// Fig. 11.3: fig11_03.cpp +// Stack class template test program. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "tstack1.h" // Stack class template definition + +int main() +{ + Stack< double > doubleStack( 5 ); + double doubleValue = 1.1; + + cout << "Pushing elements onto doubleStack\n"; + + while ( doubleStack.push( doubleValue ) ) { + cout << doubleValue << ' '; + doubleValue += 1.1; + + } // end while + + cout << "\nStack is full. Cannot push " << doubleValue + << "\n\nPopping elements from doubleStack\n"; + + while ( doubleStack.pop( doubleValue ) ) + cout << doubleValue << ' '; + + cout << "\nStack is empty. Cannot pop\n"; + + Stack< int > intStack; + int intValue = 1; + cout << "\nPushing elements onto intStack\n"; + + while ( intStack.push( intValue ) ) { + cout << intValue << ' '; + ++intValue; + + } // end while + + cout << "\nStack is full. Cannot push " << intValue + << "\n\nPopping elements from intStack\n"; + + while ( intStack.pop( intValue ) ) + cout << intValue << ' '; + + cout << "\nStack is empty. Cannot pop\n"; + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp new file mode 100644 index 0000000..852c4c4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/5_ch11/fig11_04.cpp @@ -0,0 +1,63 @@ +// Fig. 11.4: fig11_04.cpp +// Stack class template test program. Function main uses a +// function template to manipulate objects of type Stack< T >. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include "tstack1.h" // Stack class template definition + +// function template to manipulate Stack< T > +template< class T > +void testStack( + Stack< T > &theStack, // reference to Stack< T > + T value, // initial value to push + T increment, // increment for subsequent values + const char *stackName ) // name of the Stack < T > object +{ + cout << "\nPushing elements onto " << stackName << '\n'; + + while ( theStack.push( value ) ) { + cout << value << ' '; + value += increment; + + } // end while + + cout << "\nStack is full. Cannot push " << value + << "\n\nPopping elements from " << stackName << '\n'; + + while ( theStack.pop( value ) ) + cout << value << ' '; + + cout << "\nStack is empty. Cannot pop\n"; + +} // end function testStack + +int main() +{ + Stack< double > doubleStack( 5 ); + Stack< int > intStack; + + testStack( doubleStack, 1.1, 1.1, "doubleStack" ); + testStack( intStack, 1, 1, "intStack" ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/fig17_05.cpp b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/fig17_05.cpp new file mode 100644 index 0000000..4ca0d87 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/fig17_05.cpp @@ -0,0 +1,105 @@ +// Fig. 17.5: fig17_05.cpp +// List class test program. +#include + +using std::cin; +using std::endl; + +#include + +using std::string; + +#include "list.h" // List class definition + +// function to test a List +template< class T > +void testList( List< T > &listObject, const string &typeName ) +{ + cout << "Testing a List of " << typeName << " values\n"; + + instructions(); // display instructions + + int choice; + T value; + + do { + cout << "? "; + cin >> choice; + + switch ( choice ) { + case 1: + cout << "Enter " << typeName << ": "; + cin >> value; + listObject.insertAtFront( value ); + listObject.print(); + break; + + case 2: + cout << "Enter " << typeName << ": "; + cin >> value; + listObject.insertAtBack( value ); + listObject.print(); + break; + + case 3: + if ( listObject.removeFromFront( value ) ) + cout << value << " removed from list\n"; + + listObject.print(); + break; + + case 4: + if ( listObject.removeFromBack( value ) ) + cout << value << " removed from list\n"; + + listObject.print(); + break; + + } // end switch + + } while ( choice != 5 ); // end do/while + + cout << "End list test\n\n"; + +} // end function testList + +// display program instructions to user +void instructions() +{ + cout << "Enter one of the following:\n" + << " 1 to insert at beginning of list\n" + << " 2 to insert at end of list\n" + << " 3 to delete from beginning of list\n" + << " 4 to delete from end of list\n" + << " 5 to end list processing\n"; + +} // end function instructions + +int main() +{ + // test List of int values + List< int > integerList; + testList( integerList, "integer" ); + + // test List of double values + List< double > doubleList; + testList( doubleList, "double" ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/list.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/list.h new file mode 100644 index 0000000..bf97eb2 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/list.h @@ -0,0 +1,216 @@ +// Fig. 17.4: list.h +// Template List class definition. +#ifndef LIST_H +#define LIST_H + +#include + +using std::cout; + +#include +#include "listnode.h" // ListNode class definition + +template< class NODETYPE > +class List { + +public: + List(); // constructor + ~List(); // destructor + void insertAtFront( const NODETYPE & ); + void insertAtBack( const NODETYPE & ); + bool removeFromFront( NODETYPE & ); + bool removeFromBack( NODETYPE & ); + bool isEmpty() const; + void print() const; + +private: + ListNode< NODETYPE > *firstPtr; // pointer to first node + ListNode< NODETYPE > *lastPtr; // pointer to last node + + // utility function to allocate new node + ListNode< NODETYPE > *getNewNode( const NODETYPE & ); + +}; // end class List + +// default constructor +template< class NODETYPE > +List< NODETYPE >::List() + : firstPtr( 0 ), + lastPtr( 0 ) +{ + // empty body + +} // end List constructor + +// destructor +template< class NODETYPE > +List< NODETYPE >::~List() +{ + if ( !isEmpty() ) { // List is not empty + cout << "Destroying nodes ...\n"; + + ListNode< NODETYPE > *currentPtr = firstPtr; + ListNode< NODETYPE > *tempPtr; + + while ( currentPtr != 0 ) { // delete remaining nodes + tempPtr = currentPtr; + cout << tempPtr->data << '\n'; + currentPtr = currentPtr->nextPtr; + delete tempPtr; + + } // end while + + } // end if + + cout << "All nodes destroyed\n\n"; + +} // end List destructor + +// insert node at front of list +template< class NODETYPE > +void List< NODETYPE >::insertAtFront( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + newPtr->nextPtr = firstPtr; + firstPtr = newPtr; + + } // end else + +} // end function insertAtFront + +// insert node at back of list +template< class NODETYPE > +void List< NODETYPE >::insertAtBack( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + lastPtr->nextPtr = newPtr; + lastPtr = newPtr; + + } // end else + +} // end function insertAtBack + +// delete node from front of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromFront( NODETYPE &value ) +{ + if ( isEmpty() ) // List is empty + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = firstPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else + firstPtr = firstPtr->nextPtr; + + value = tempPtr->data; // data being removed + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function removeFromFront + +// delete node from back of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromBack( NODETYPE &value ) +{ + if ( isEmpty() ) + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = lastPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else { + ListNode< NODETYPE > *currentPtr = firstPtr; + + // locate second-to-last element + while ( currentPtr->nextPtr != lastPtr ) + currentPtr = currentPtr->nextPtr; + + lastPtr = currentPtr; + currentPtr->nextPtr = 0; + + } // end else + + value = tempPtr->data; + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function removeFromBack + +// is List empty? +template< class NODETYPE > +bool List< NODETYPE >::isEmpty() const +{ + return firstPtr == 0; + +} // end function isEmpty + +// return pointer to newly allocated node +template< class NODETYPE > +ListNode< NODETYPE > *List< NODETYPE >::getNewNode( + const NODETYPE &value ) +{ + return new ListNode< NODETYPE >( value ); + +} // end function getNewNode + +// display contents of List +template< class NODETYPE > +void List< NODETYPE >::print() const +{ + if ( isEmpty() ) { + cout << "The list is empty\n\n"; + return; + + } // end if + + ListNode< NODETYPE > *currentPtr = firstPtr; + + cout << "The list is: "; + + while ( currentPtr != 0 ) { + cout << currentPtr->data << ' '; + currentPtr = currentPtr->nextPtr; + + } // end while + + cout << "\n\n"; + +} // end function print + +#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/Prog2/Codebeispiele/6_ch17/fig17_03_05/listnode.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/listnode.h new file mode 100644 index 0000000..325d5f1 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_03_05/listnode.h @@ -0,0 +1,56 @@ +// Fig. 17.3: listnode.h +// Template ListNode class definition. +#ifndef LISTNODE_H +#define LISTNODE_H + +// forward declaration of class List +template< class NODETYPE > class List; + +template< class NODETYPE > +class ListNode { + friend class List< NODETYPE >; // make List a friend + +public: + ListNode( const NODETYPE & ); // constructor + NODETYPE getData() const; // return data in node + +private: + NODETYPE data; // data + ListNode< NODETYPE > *nextPtr; // next node in list + +}; // end class ListNode + +// constructor +template< class NODETYPE > +ListNode< NODETYPE >::ListNode( const NODETYPE &info ) + : data( info ), + nextPtr( 0 ) +{ + // empty body + +} // end ListNode constructor + +// return copy of data in node +template< class NODETYPE > +NODETYPE ListNode< NODETYPE >::getData() const +{ + return data; + +} // end function getData + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/fig17_11.cpp b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/fig17_11.cpp new file mode 100644 index 0000000..0571059 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/fig17_11.cpp @@ -0,0 +1,72 @@ +// Fig. 17.11: fig17_11.cpp +// Template Stack class test program. +#include + +using std::endl; + +#include "stack.h" // Stack class definition + +int main() +{ + Stack< int > intStack; // create Stack of ints + + cout << "processing an integer Stack" << endl; + + // push integers onto intStack + for ( int i = 0; i < 4; i++ ) { + intStack.push( i ); + intStack.printStack(); + + } // end for + + // pop integers from intStack + int popInteger; + + while ( !intStack.isStackEmpty() ) { + intStack.pop( popInteger ); + cout << popInteger << " popped from stack" << endl; + intStack.printStack(); + + } // end while + + Stack< double > doubleStack; // create Stack of doubles + double value = 1.1; + + cout << "processing a double Stack" << endl; + + // push floating-point values onto doubleStack + for ( int j = 0; j < 4; j++ ) { + doubleStack.push( value ); + doubleStack.printStack(); + value += 1.1; + + } // end for + + // pop floating-point values from doubleStack + double popDouble; + + while ( !doubleStack.isStackEmpty() ) { + doubleStack.pop( popDouble ); + cout << popDouble << " popped from stack" << endl; + doubleStack.printStack(); + + } // end while + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/list.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/list.h new file mode 100644 index 0000000..c6d2219 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/list.h @@ -0,0 +1,216 @@ +// Fig. 15.4: list.h +// Template List class definition. +#ifndef LIST_H +#define LIST_H + +#include + +using std::cout; + +#include +#include "listnode.h" // ListNode class definition + +template< class NODETYPE > +class List { + +public: + List(); // constructor + ~List(); // destructor + void insertAtFront( const NODETYPE & ); + void insertAtBack( const NODETYPE & ); + bool removeFromFront( NODETYPE & ); + bool removeFromBack( NODETYPE & ); + bool isEmpty() const; + void print() const; + +private: + ListNode< NODETYPE > *firstPtr; // pointer to first node + ListNode< NODETYPE > *lastPtr; // pointer to last node + + // utility function to allocate new node + ListNode< NODETYPE > *getNewNode( const NODETYPE & ); + +}; // end class List + +// default constructor +template< class NODETYPE > +List< NODETYPE >::List() + : firstPtr( 0 ), + lastPtr( 0 ) +{ + // empty body + +} // end List constructor + +// destructor +template< class NODETYPE > +List< NODETYPE >::~List() +{ + if ( !isEmpty() ) { // List is not empty + cout << "Destroying nodes ...\n"; + + ListNode< NODETYPE > *currentPtr = firstPtr; + ListNode< NODETYPE > *tempPtr; + + while ( currentPtr != 0 ) { // delete remaining nodes + tempPtr = currentPtr; + cout << tempPtr->data << '\n'; + currentPtr = currentPtr->nextPtr; + delete tempPtr; + + } // end while + + } // end if + + cout << "All nodes destroyed\n\n"; + +} // end ~List destructor + +// insert node at front of list +template< class NODETYPE > +void List< NODETYPE >::insertAtFront( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + newPtr->nextPtr = firstPtr; + firstPtr = newPtr; + + } // end else + +} // end function insertAtFront + +// insert node at back of list +template< class NODETYPE > +void List< NODETYPE >::insertAtBack( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + lastPtr->nextPtr = newPtr; + lastPtr = newPtr; + + } // end else + +} // end function insertAtBack + +// delete node from front of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromFront( NODETYPE &value ) +{ + if ( isEmpty() ) // List is empty + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = firstPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else + firstPtr = firstPtr->nextPtr; + + value = tempPtr->data; // data being removed + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function deleteFromFront + +// delete node from back of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromBack( NODETYPE &value ) +{ + if ( isEmpty() ) + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = lastPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else { + ListNode< NODETYPE > *currentPtr = firstPtr; + + // locate second-to-last element + while ( currentPtr->nextPtr != lastPtr ) + currentPtr = currentPtr->nextPtr; + + lastPtr = currentPtr; + currentPtr->nextPtr = 0; + + } // end else + + value = tempPtr->data; + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function deleteFromBack + +// is List empty? +template< class NODETYPE > +bool List< NODETYPE >::isEmpty() const +{ + return firstPtr == 0; + +} // end function isEmpty + +// return pointer to newly allocated node +template< class NODETYPE > +ListNode< NODETYPE > *List< NODETYPE >::getNewNode( + const NODETYPE &value ) +{ + return new ListNode< NODETYPE >( value ); + +} // end function getNewNode + +// display contents of List +template< class NODETYPE > +void List< NODETYPE >::print() const +{ + if ( isEmpty() ) { + cout << "The list is empty\n\n"; + return; + + } // end if + + ListNode< NODETYPE > *currentPtr = firstPtr; + + cout << "The list is: "; + + while ( currentPtr != 0 ) { + cout << currentPtr->data << ' '; + currentPtr = currentPtr->nextPtr; + + } // end while + + cout << "\n\n"; + +} // end function print + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/listnode.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/listnode.h new file mode 100644 index 0000000..7980786 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/listnode.h @@ -0,0 +1,56 @@ +// Fig. 15.3: listnode.h +// Template ListNode class definition. +#ifndef LISTNODE_H +#define LISTNODE_H + +// forward declaration of class List +template< class NODETYPE > class List; + +template< class NODETYPE > +class ListNode { + friend class List< NODETYPE >; // make List a friend + +public: + ListNode( const NODETYPE & ); // constructor + NODETYPE getData() const; // return data in node + +private: + NODETYPE data; // data + ListNode< NODETYPE > *nextPtr; // next node in list + +}; // end class ListNode + +// constructor +template< class NODETYPE > +ListNode< NODETYPE >::ListNode( const NODETYPE &info ) + : data( info ), + nextPtr( 0 ) +{ + // empty body + +} // end ListNode constructor + +// return copy of data in node +template< class NODETYPE > +NODETYPE ListNode< NODETYPE >::getData() const +{ + return data; + +} // end function getData + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/stack.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/stack.h new file mode 100644 index 0000000..bc883ea --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_10_11/stack.h @@ -0,0 +1,57 @@ +// Fig. 17.10: stack.h +// Template Stack class definition derived from class List. +#ifndef STACK_H +#define STACK_H + +#include "list.h" // List class definition + +template< class STACKTYPE > +class Stack : private List< STACKTYPE > { + +public: + // push calls List function insertAtFront + void push( const STACKTYPE &data ) + { + insertAtFront( data ); + + } // end function push + + // pop calls List function removeFromFront + bool pop( STACKTYPE &data ) + { + return removeFromFront( data ); + + } // end function pop + + // isStackEmpty calls List function isEmpty + bool isStackEmpty() const + { + return isEmpty(); + + } // end function isStackEmpty + + // printStack calls List function print + void printStack() const + { + print(); + + } // end function print + +}; // end class Stack + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/fig17_12test.cpp b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/fig17_12test.cpp new file mode 100644 index 0000000..3adb315 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/fig17_12test.cpp @@ -0,0 +1,69 @@ +// Fig. 17.11: fig17_11.cpp +// Template Stack class test program. +#include + +using std::endl; + +#include "stackcomposition.h" // Stack class definition + +int main() +{ + Stack< int > intStack; // create Stack of ints + int popInteger; + + cout << "processing an integer Stack" << endl; + + // push integers onto intStack + for ( int i = 0; i < 4; i++ ) { + intStack.push( i ); + intStack.printStack(); + + } // end for + + // pop integers from intStack + while ( !intStack.isStackEmpty() ) { + intStack.pop( popInteger ); + cout << popInteger << " popped from stack" << endl; + intStack.printStack(); + + } // end while + + Stack< double > doubleStack; // create Stack of doubles + double value = 1.1, popdouble; + + cout << "processing a double Stack" << endl; + + // push floating-point values onto doubleStack + for ( int j = 0; j< 4; j++ ) { + doubleStack.push( value ); + doubleStack.printStack(); + value += 1.1; + + } // end for + + // pop floating-point values from doubleStack + while ( !doubleStack.isStackEmpty() ) { + doubleStack.pop( popdouble ); + cout << popdouble << " popped from stack" << endl; + doubleStack.printStack(); + + } // end while + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/list.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/list.h new file mode 100644 index 0000000..c6d2219 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/list.h @@ -0,0 +1,216 @@ +// Fig. 15.4: list.h +// Template List class definition. +#ifndef LIST_H +#define LIST_H + +#include + +using std::cout; + +#include +#include "listnode.h" // ListNode class definition + +template< class NODETYPE > +class List { + +public: + List(); // constructor + ~List(); // destructor + void insertAtFront( const NODETYPE & ); + void insertAtBack( const NODETYPE & ); + bool removeFromFront( NODETYPE & ); + bool removeFromBack( NODETYPE & ); + bool isEmpty() const; + void print() const; + +private: + ListNode< NODETYPE > *firstPtr; // pointer to first node + ListNode< NODETYPE > *lastPtr; // pointer to last node + + // utility function to allocate new node + ListNode< NODETYPE > *getNewNode( const NODETYPE & ); + +}; // end class List + +// default constructor +template< class NODETYPE > +List< NODETYPE >::List() + : firstPtr( 0 ), + lastPtr( 0 ) +{ + // empty body + +} // end List constructor + +// destructor +template< class NODETYPE > +List< NODETYPE >::~List() +{ + if ( !isEmpty() ) { // List is not empty + cout << "Destroying nodes ...\n"; + + ListNode< NODETYPE > *currentPtr = firstPtr; + ListNode< NODETYPE > *tempPtr; + + while ( currentPtr != 0 ) { // delete remaining nodes + tempPtr = currentPtr; + cout << tempPtr->data << '\n'; + currentPtr = currentPtr->nextPtr; + delete tempPtr; + + } // end while + + } // end if + + cout << "All nodes destroyed\n\n"; + +} // end ~List destructor + +// insert node at front of list +template< class NODETYPE > +void List< NODETYPE >::insertAtFront( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + newPtr->nextPtr = firstPtr; + firstPtr = newPtr; + + } // end else + +} // end function insertAtFront + +// insert node at back of list +template< class NODETYPE > +void List< NODETYPE >::insertAtBack( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + lastPtr->nextPtr = newPtr; + lastPtr = newPtr; + + } // end else + +} // end function insertAtBack + +// delete node from front of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromFront( NODETYPE &value ) +{ + if ( isEmpty() ) // List is empty + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = firstPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else + firstPtr = firstPtr->nextPtr; + + value = tempPtr->data; // data being removed + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function deleteFromFront + +// delete node from back of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromBack( NODETYPE &value ) +{ + if ( isEmpty() ) + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = lastPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else { + ListNode< NODETYPE > *currentPtr = firstPtr; + + // locate second-to-last element + while ( currentPtr->nextPtr != lastPtr ) + currentPtr = currentPtr->nextPtr; + + lastPtr = currentPtr; + currentPtr->nextPtr = 0; + + } // end else + + value = tempPtr->data; + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function deleteFromBack + +// is List empty? +template< class NODETYPE > +bool List< NODETYPE >::isEmpty() const +{ + return firstPtr == 0; + +} // end function isEmpty + +// return pointer to newly allocated node +template< class NODETYPE > +ListNode< NODETYPE > *List< NODETYPE >::getNewNode( + const NODETYPE &value ) +{ + return new ListNode< NODETYPE >( value ); + +} // end function getNewNode + +// display contents of List +template< class NODETYPE > +void List< NODETYPE >::print() const +{ + if ( isEmpty() ) { + cout << "The list is empty\n\n"; + return; + + } // end if + + ListNode< NODETYPE > *currentPtr = firstPtr; + + cout << "The list is: "; + + while ( currentPtr != 0 ) { + cout << currentPtr->data << ' '; + currentPtr = currentPtr->nextPtr; + + } // end while + + cout << "\n\n"; + +} // end function print + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/listnode.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/listnode.h new file mode 100644 index 0000000..7980786 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/listnode.h @@ -0,0 +1,56 @@ +// Fig. 15.3: listnode.h +// Template ListNode class definition. +#ifndef LISTNODE_H +#define LISTNODE_H + +// forward declaration of class List +template< class NODETYPE > class List; + +template< class NODETYPE > +class ListNode { + friend class List< NODETYPE >; // make List a friend + +public: + ListNode( const NODETYPE & ); // constructor + NODETYPE getData() const; // return data in node + +private: + NODETYPE data; // data + ListNode< NODETYPE > *nextPtr; // next node in list + +}; // end class ListNode + +// constructor +template< class NODETYPE > +ListNode< NODETYPE >::ListNode( const NODETYPE &info ) + : data( info ), + nextPtr( 0 ) +{ + // empty body + +} // end ListNode constructor + +// return copy of data in node +template< class NODETYPE > +NODETYPE ListNode< NODETYPE >::getData() const +{ + return data; + +} // end function getData + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/stackcomposition.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/stackcomposition.h new file mode 100644 index 0000000..2a603d7 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_12/stackcomposition.h @@ -0,0 +1,62 @@ +// Fig. 17.12: stackcomposition.h +// Template Stack class definition with composed List object. +#ifndef STACKCOMPOSITION +#define STACKCOMPOSITION + +#include "list.h" // List class definition + +template< class STACKTYPE > +class Stack { + +public: + // no constructor; List constructor does initialization + + // push calls stackList object's insertAtFront function + void push( const STACKTYPE &data ) + { + stackList.insertAtFront( data ); + + } // end function push + + // pop calls stackList object's removeFromFront function + bool pop( STACKTYPE &data ) + { + return stackList.removeFromFront( data ); + + } // end function pop + + // isStackEmpty calls stackList object's isEmpty function + bool isStackEmpty() const + { + return stackList.isEmpty(); + + } // end function isStackEmpty + + // printStack calls stackList object's print function + void printStack() const + { + stackList.print(); + + } // end function printStack + +private: + List< STACKTYPE > stackList; // composed List object + +}; // end class Stack + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/fig17_14.cpp b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/fig17_14.cpp new file mode 100644 index 0000000..a5c24d5 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/fig17_14.cpp @@ -0,0 +1,72 @@ +// Fig. 17.14: fig17_14.cpp +// Template Queue class test program. +#include + +using std::endl; + +#include "queue.h" // Queue class definition + +int main() +{ + Queue< int > intQueue; // create Queue of ints + + cout << "processing an integer Queue" << endl; + + // enqueue integers onto intQueue + for ( int i = 0; i < 4; i++ ) { + intQueue.enqueue( i ); + intQueue.printQueue(); + + } // end for + + // dequeue integers from intQueue + int dequeueInteger; + + while ( !intQueue.isQueueEmpty() ) { + intQueue.dequeue( dequeueInteger ); + cout << dequeueInteger << " dequeued" << endl; + intQueue.printQueue(); + + } // end while + + Queue< double > doubleQueue; // create Queue of doubles + double value = 1.1; + + cout << "processing a double Queue" << endl; + + // enqueue floating-point values onto doubleQueue + for ( int j = 0; j< 4; j++ ) { + doubleQueue.enqueue( value ); + doubleQueue.printQueue(); + value += 1.1; + + } // end for + + // dequeue floating-point values from doubleQueue + double dequeueDouble; + + while ( !doubleQueue.isQueueEmpty() ) { + doubleQueue.dequeue( dequeueDouble ); + cout << dequeueDouble << " dequeued" << endl; + doubleQueue.printQueue(); + + } // end while + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/list.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/list.h new file mode 100644 index 0000000..c6d2219 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/list.h @@ -0,0 +1,216 @@ +// Fig. 15.4: list.h +// Template List class definition. +#ifndef LIST_H +#define LIST_H + +#include + +using std::cout; + +#include +#include "listnode.h" // ListNode class definition + +template< class NODETYPE > +class List { + +public: + List(); // constructor + ~List(); // destructor + void insertAtFront( const NODETYPE & ); + void insertAtBack( const NODETYPE & ); + bool removeFromFront( NODETYPE & ); + bool removeFromBack( NODETYPE & ); + bool isEmpty() const; + void print() const; + +private: + ListNode< NODETYPE > *firstPtr; // pointer to first node + ListNode< NODETYPE > *lastPtr; // pointer to last node + + // utility function to allocate new node + ListNode< NODETYPE > *getNewNode( const NODETYPE & ); + +}; // end class List + +// default constructor +template< class NODETYPE > +List< NODETYPE >::List() + : firstPtr( 0 ), + lastPtr( 0 ) +{ + // empty body + +} // end List constructor + +// destructor +template< class NODETYPE > +List< NODETYPE >::~List() +{ + if ( !isEmpty() ) { // List is not empty + cout << "Destroying nodes ...\n"; + + ListNode< NODETYPE > *currentPtr = firstPtr; + ListNode< NODETYPE > *tempPtr; + + while ( currentPtr != 0 ) { // delete remaining nodes + tempPtr = currentPtr; + cout << tempPtr->data << '\n'; + currentPtr = currentPtr->nextPtr; + delete tempPtr; + + } // end while + + } // end if + + cout << "All nodes destroyed\n\n"; + +} // end ~List destructor + +// insert node at front of list +template< class NODETYPE > +void List< NODETYPE >::insertAtFront( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + newPtr->nextPtr = firstPtr; + firstPtr = newPtr; + + } // end else + +} // end function insertAtFront + +// insert node at back of list +template< class NODETYPE > +void List< NODETYPE >::insertAtBack( const NODETYPE &value ) +{ + ListNode< NODETYPE > *newPtr = getNewNode( value ); + + if ( isEmpty() ) // List is empty + firstPtr = lastPtr = newPtr; + + else { // List is not empty + lastPtr->nextPtr = newPtr; + lastPtr = newPtr; + + } // end else + +} // end function insertAtBack + +// delete node from front of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromFront( NODETYPE &value ) +{ + if ( isEmpty() ) // List is empty + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = firstPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else + firstPtr = firstPtr->nextPtr; + + value = tempPtr->data; // data being removed + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function deleteFromFront + +// delete node from back of list +template< class NODETYPE > +bool List< NODETYPE >::removeFromBack( NODETYPE &value ) +{ + if ( isEmpty() ) + return false; // delete unsuccessful + + else { + ListNode< NODETYPE > *tempPtr = lastPtr; + + if ( firstPtr == lastPtr ) + firstPtr = lastPtr = 0; + else { + ListNode< NODETYPE > *currentPtr = firstPtr; + + // locate second-to-last element + while ( currentPtr->nextPtr != lastPtr ) + currentPtr = currentPtr->nextPtr; + + lastPtr = currentPtr; + currentPtr->nextPtr = 0; + + } // end else + + value = tempPtr->data; + delete tempPtr; + + return true; // delete successful + + } // end else + +} // end function deleteFromBack + +// is List empty? +template< class NODETYPE > +bool List< NODETYPE >::isEmpty() const +{ + return firstPtr == 0; + +} // end function isEmpty + +// return pointer to newly allocated node +template< class NODETYPE > +ListNode< NODETYPE > *List< NODETYPE >::getNewNode( + const NODETYPE &value ) +{ + return new ListNode< NODETYPE >( value ); + +} // end function getNewNode + +// display contents of List +template< class NODETYPE > +void List< NODETYPE >::print() const +{ + if ( isEmpty() ) { + cout << "The list is empty\n\n"; + return; + + } // end if + + ListNode< NODETYPE > *currentPtr = firstPtr; + + cout << "The list is: "; + + while ( currentPtr != 0 ) { + cout << currentPtr->data << ' '; + currentPtr = currentPtr->nextPtr; + + } // end while + + cout << "\n\n"; + +} // end function print + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/listnode.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/listnode.h new file mode 100644 index 0000000..7980786 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/listnode.h @@ -0,0 +1,56 @@ +// Fig. 15.3: listnode.h +// Template ListNode class definition. +#ifndef LISTNODE_H +#define LISTNODE_H + +// forward declaration of class List +template< class NODETYPE > class List; + +template< class NODETYPE > +class ListNode { + friend class List< NODETYPE >; // make List a friend + +public: + ListNode( const NODETYPE & ); // constructor + NODETYPE getData() const; // return data in node + +private: + NODETYPE data; // data + ListNode< NODETYPE > *nextPtr; // next node in list + +}; // end class ListNode + +// constructor +template< class NODETYPE > +ListNode< NODETYPE >::ListNode( const NODETYPE &info ) + : data( info ), + nextPtr( 0 ) +{ + // empty body + +} // end ListNode constructor + +// return copy of data in node +template< class NODETYPE > +NODETYPE ListNode< NODETYPE >::getData() const +{ + return data; + +} // end function getData + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queue.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queue.h new file mode 100644 index 0000000..84054c9 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queue.h @@ -0,0 +1,57 @@ +// Fig. 17.13: queue.h +// Template Queue class definition derived from class List. +#ifndef QUEUE_H +#define QUEUE_H + +#include "list.h" // List class definition + +template< class QUEUETYPE > +class Queue: private List< QUEUETYPE > { + +public: + // enqueue calls List function insertAtBack + void enqueue( const QUEUETYPE &data ) + { + insertAtBack( data ); + + } // end function enqueue + + // dequeue calls List function removeFromFront + bool dequeue( QUEUETYPE &data ) + { + return removeFromFront( data ); + + } // end function dequeue + + // isQueueEmpty calls List function isEmpty + bool isQueueEmpty() const + { + return isEmpty(); + + } // end function isQueueEmpty + + // printQueue calls List function print + void printQueue() const + { + print(); + + } // end function printQueue + +}; // end class Queue + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queuecomposition.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queuecomposition.h new file mode 100644 index 0000000..01c2ad4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_13_14/queuecomposition.h @@ -0,0 +1,59 @@ +// Definition of Queue class composed of List object +#ifndef QUEUE_C +#define QUEUE_C + +#include "list.h" + +template< class QUEUETYPE > +class Queue { + +public: + // enqueue calls queueList object's insertAtBack function + void enqueue( const QUEUETYPE &data ) + { + queueList.insertAtBack( data ); + + } // end function enqueue + + // dequeue calls queueList object's removeFromFront function + bool dequeue( QUEUETYPE &data ) + { + return queueList.removeFromFront( data ); + + } // end function dequeue + + // isQueueEmpty calls queueList object's isEmpty function + bool isQueueEmpty() const + { + return queueList.isEmpty(); + + } // end function isQueueEmpty + + // printQueue calls queueList object's print function + void printQueue() const + { + queueList.print(); + + } // end function printQueue + +private: + List< QUEUETYPE > queueList; // composed List object + +}; // end class Queue + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h new file mode 100644 index 0000000..d44276c --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Tree.h @@ -0,0 +1,159 @@ +// Fig. 17.18: tree.h +// Template Tree class definition. +#ifndef TREE_H +#define TREE_H + +#include + +using std::endl; + +#include +#include "treenode.h" + +template< class NODETYPE > +class Tree { + +public: + Tree(); + void insertNode( const NODETYPE & ); + void preOrderTraversal() const; + void inOrderTraversal() const; + void postOrderTraversal() const; + +private: + TreeNode< NODETYPE > *rootPtr; + + // utility functions + void insertNodeHelper( + TreeNode< NODETYPE > **, const NODETYPE & ); + void preOrderHelper( TreeNode< NODETYPE > * ) const; + void inOrderHelper( TreeNode< NODETYPE > * ) const; + void postOrderHelper( TreeNode< NODETYPE > * ) const; + +}; // end class Tree + +// constructor +template< class NODETYPE > +Tree< NODETYPE >::Tree() +{ + rootPtr = 0; + +} // end Tree constructor + +// insert node in Tree +template< class NODETYPE > +void Tree< NODETYPE >::insertNode( const NODETYPE &value ) +{ + insertNodeHelper( &rootPtr, value ); + +} // end function insertNode + +// utility function called by insertNode; receives a pointer +// to a pointer so that the function can modify pointer's value +template< class NODETYPE > +void Tree< NODETYPE >::insertNodeHelper( + TreeNode< NODETYPE > **ptr, const NODETYPE &value ) +{ + // subtree is empty; create new TreeNode containing value + if ( *ptr == 0 ) + *ptr = new TreeNode< NODETYPE >( value ); + + else // subtree is not empty + + // data to insert is less than data in current node + if ( value < ( *ptr )->data ) + insertNodeHelper( &( ( *ptr )->leftPtr ), value ); + + else + + // data to insert is greater than data in current node + if ( value > ( *ptr )->data ) + insertNodeHelper( &( ( *ptr )->rightPtr ), value ); + + else // duplicate data value ignored + cout << value << " dup" << endl; + +} // end function insertNodeHelper + +// begin preorder traversal of Tree +template< class NODETYPE > +void Tree< NODETYPE >::preOrderTraversal() const +{ + preOrderHelper( rootPtr ); + +} // end function preOrderTraversal + +// utility function to perform preorder traversal of Tree +template< class NODETYPE > +void Tree< NODETYPE >::preOrderHelper( + TreeNode< NODETYPE > *ptr ) const +{ + if ( ptr != 0 ) { + cout << ptr->data << ' '; // process node + preOrderHelper( ptr->leftPtr ); // go to left subtree + preOrderHelper( ptr->rightPtr ); // go to right subtree + + } // end if + +} // end function preOrderHelper + +// begin inorder traversal of Tree +template< class NODETYPE > +void Tree< NODETYPE >::inOrderTraversal() const +{ + inOrderHelper( rootPtr ); + +} // end function inOrderTraversal + +// utility function to perform inorder traversal of Tree +template< class NODETYPE > +void Tree< NODETYPE >::inOrderHelper( + TreeNode< NODETYPE > *ptr ) const +{ + if ( ptr != 0 ) { + inOrderHelper( ptr->leftPtr ); // go to left subtree + cout << ptr->data << ' '; // process node + inOrderHelper( ptr->rightPtr ); // go to right subtree + + } // end if + +} // end function inOrderHelper + +// begin postorder traversal of Tree +template< class NODETYPE > +void Tree< NODETYPE >::postOrderTraversal() const +{ + postOrderHelper( rootPtr ); + +} // end function postOrderTraversal + +// utility function to perform postorder traversal of Tree +template< class NODETYPE > +void Tree< NODETYPE >::postOrderHelper( + TreeNode< NODETYPE > *ptr ) const +{ + if ( ptr != 0 ) { + postOrderHelper( ptr->leftPtr ); // go to left subtree + postOrderHelper( ptr->rightPtr ); // go to right subtree + cout << ptr->data << ' '; // process node + + } // end if + +} // end function postOrderHelper + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h new file mode 100644 index 0000000..a79cee9 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/Treenode.h @@ -0,0 +1,54 @@ +// Fig. 17.17: treenode.h +// Template TreeNode class definition. +#ifndef TREENODE_H +#define TREENODE_H + +// forward declaration of class Tree +template< class NODETYPE > class Tree; + +template< class NODETYPE > +class TreeNode { + friend class Tree< NODETYPE >; + +public: + + // constructor + TreeNode( const NODETYPE &d ) + : leftPtr( 0 ), + data( d ), + rightPtr( 0 ) + { + // empty body + + } // end TreeNode constructor + + // return copy of node's data + NODETYPE getData() const + { + return data; + + } // end getData function + +private: + TreeNode< NODETYPE > *leftPtr; // pointer to left subtree + NODETYPE data; + TreeNode< NODETYPE > *rightPtr; // pointer to right subtree + +}; // end class TreeNode + +#endif + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp new file mode 100644 index 0000000..3fb8def --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/6_ch17/fig17_17_19/fig17_19.cpp @@ -0,0 +1,76 @@ +// Fig. 17.19: fig17_19.cpp +// Tree class test program. +#include + +using std::cout; +using std::cin; +using std::fixed; + +#include +using std::setprecision; + +#include "tree.h" // Tree class definition + +int main() +{ + Tree< int > intTree; // create Tree of int values + int intValue; + + cout << "Enter 10 integer values:\n"; + + for( int i = 0; i < 10; i++ ) { + cin >> intValue; + intTree.insertNode( intValue ); + + } // end for + + cout << "\nPreorder traversal\n"; + intTree.preOrderTraversal(); + + cout << "\nInorder traversal\n"; + intTree.inOrderTraversal(); + + cout << "\nPostorder traversal\n"; + intTree.postOrderTraversal(); + + Tree< double > doubleTree; // create Tree of double values + double doubleValue; + + cout << fixed << setprecision( 1 ) + << "\n\n\nEnter 10 double values:\n"; + + for ( int j = 0; j < 10; j++ ) { + cin >> doubleValue; + doubleTree.insertNode( doubleValue ); + + } // end for + + cout << "\nPreorder traversal\n"; + doubleTree.preOrderTraversal(); + + cout << "\nInorder traversal\n"; + doubleTree.inOrderTraversal(); + + cout << "\nPostorder traversal\n"; + doubleTree.postOrderTraversal(); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP new file mode 100644 index 0000000..0fe4205 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_05.CPP @@ -0,0 +1,46 @@ +// Fig. 21.5: fig21_05.cpp +// Demonstrating input and output with iterators. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include // ostream_iterator and istream_iterator + +int main() +{ + cout << "Enter two integers: "; + + // create istream_iterator for reading int values from cin + std::istream_iterator< int > inputInt( cin ); + + int number1 = *inputInt; // read int from standard input + ++inputInt; // move iterator to next input value + int number2 = *inputInt; // read int from standard input + + // create ostream_iterator for writing int values to cout + std::ostream_iterator< int > outputInt( cout ); + + cout << "The sum is: "; + *outputInt = number1 + number2; // output result to cout + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP new file mode 100644 index 0000000..8a42288 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_14.CPP @@ -0,0 +1,85 @@ +// Fig. 21.14: fig21_14.cpp +// Demonstrating standard library vector class template. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include // vector class-template definition + +// prototype for function template printVector +template < class T > +void printVector( const std::vector< T > &integers2 ); + +int main() +{ + const int SIZE = 6; + int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; + + std::vector< int > integers; + + cout << "The initial size of integers is: " + << integers.size() + << "\nThe initial capacity of integers is: " + << integers.capacity(); + + // function push_back is in every sequence collection + integers.push_back( 2 ); + integers.push_back( 3 ); + integers.push_back( 4 ); + + cout << "\nThe size of integers is: " << integers.size() + << "\nThe capacity of integers is: " + << integers.capacity(); + + cout << "\n\nOutput array using pointer notation: "; + + for ( int *ptr = array; ptr != array + SIZE; ++ptr ) + cout << *ptr << ' '; + + cout << "\nOutput vector using iterator notation: "; + printVector( integers ); + + cout << "\nReversed contents of vector integers: "; + + std::vector< int >::reverse_iterator reverseIterator; + + for ( reverseIterator = integers.rbegin(); + reverseIterator!= integers.rend(); + ++reverseIterator ) + cout << *reverseIterator << ' '; + + cout << endl; + + return 0; + +} // end main + +// function template for outputting vector elements +template < class T > +void printVector( const std::vector< T > &integers2 ) +{ + std::vector< T >::const_iterator constIterator; + + for ( constIterator = integers2.begin(); + constIterator != integers2.end(); + constIterator++ ) + cout << *constIterator << ' '; + +} // end function printVector + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP new file mode 100644 index 0000000..bb74911 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_15.CPP @@ -0,0 +1,86 @@ +// Fig. 21.15: fig21_15.cpp +// Testing Standard Library vector class template +// element-manipulation functions. +#include + +using std::cout; +using std::endl; + +#include // vector class-template definition +#include // copy algorithm + +int main() +{ + const int SIZE = 6; + int array[ SIZE ] = { 1, 2, 3, 4, 5, 6 }; + + std::vector< int > integers( array, array + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector integers contains: "; + std::copy( integers.begin(), integers.end(), output ); + + cout << "\nFirst element of integers: " << integers.front() + << "\nLast element of integers: " << integers.back(); + + integers[ 0 ] = 7; // set first element to 7 + integers.at( 2 ) = 10; // set element at position 2 to 10 + + // insert 22 as 2nd element + integers.insert( integers.begin() + 1, 22 ); + + cout << "\n\nContents of vector integers after changes: "; + std::copy( integers.begin(), integers.end(), output ); + + // access out-of-range element + try { + integers.at( 100 ) = 777; + + } // end try + + // catch out_of_range exception + catch ( std::out_of_range outOfRange ) { + cout << "\n\nException: " << outOfRange.what(); + + } // end catch + + // erase first element + integers.erase( integers.begin() ); + cout << "\n\nVector integers after erasing first element: "; + std::copy( integers.begin(), integers.end(), output ); + + // erase remaining elements + integers.erase( integers.begin(), integers.end() ); + cout << "\nAfter erasing all elements, vector integers " + << ( integers.empty() ? "is" : "is not" ) << " empty"; + + // insert elements from array + integers.insert( integers.begin(), array, array + SIZE ); + cout << "\n\nContents of vector integers before clear: "; + std::copy( integers.begin(), integers.end(), output ); + + // empty integers; clear calls erase to empty a collection + integers.clear(); + cout << "\nAfter clear, vector integers " + << ( integers.empty() ? "is" : "is not" ) << " empty"; + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP new file mode 100644 index 0000000..bd6398d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_17.CPP @@ -0,0 +1,145 @@ +// Fig. 21.17: fig21_17.cpp +// Standard library list class template test program. +#include + +using std::cout; +using std::endl; + +#include // list class-template definition +#include // copy algorithm + +// prototype for function template printList +template < class T > +void printList( const std::list< T > &listRef ); + +int main() +{ + const int SIZE = 4; + int array[ SIZE ] = { 2, 6, 4, 8 }; + + std::list< int > values; + std::list< int > otherValues; + + // insert items in values + values.push_front( 1 ); + values.push_front( 2 ); + values.push_back( 4 ); + values.push_back( 3 ); + + cout << "values contains: "; + printList( values ); + + values.sort(); // sort values + + cout << "\nvalues after sorting contains: "; + printList( values ); + + // insert elements of array into otherValues + otherValues.insert( otherValues.begin(), + array, array + SIZE ); + + cout << "\nAfter insert, otherValues contains: "; + printList( otherValues ); + + // remove otherValues elements and insert at end of values + values.splice( values.end(), otherValues ); + + cout << "\nAfter splice, values contains: "; + printList( values ); + + values.sort(); // sort values + + cout << "\nAfter sort, values contains: "; + printList( values ); + + // insert elements of array into otherValues + otherValues.insert( otherValues.begin(), + array, array + SIZE ); + otherValues.sort(); + + cout << "\nAfter insert, otherValues contains: "; + printList( otherValues ); + + // remove otherValues elements and insert into values + // in sorted order + values.merge( otherValues ); + + cout << "\nAfter merge:\n values contains: "; + printList( values ); + cout << "\n otherValues contains: "; + printList( otherValues ); + + values.pop_front(); // remove element from front + values.pop_back(); // remove element from back + + cout << "\nAfter pop_front and pop_back:" + << "\n values contains: "; + printList( values ); + + values.unique(); // remove duplicate elements + + cout << "\nAfter unique, values contains: "; + printList( values ); + + // swap elements of values and otherValues + values.swap( otherValues ); + + cout << "\nAfter swap:\n values contains: "; + printList( values ); + cout << "\n otherValues contains: "; + printList( otherValues ); + + // replace contents of values with elements of otherValues + values.assign( otherValues.begin(), otherValues.end() ); + + cout << "\nAfter assign, values contains: "; + printList( values ); + + // remove otherValues elements and insert into values + // in sorted order + values.merge( otherValues ); + + cout << "\nAfter merge, values contains: "; + printList( values ); + + values.remove( 4 ); // remove all 4s + + cout << "\nAfter remove( 4 ), values contains: "; + printList( values ); + + cout << endl; + + return 0; + +} // end main + +// printList function template definition; uses +// ostream_iterator and copy algorithm to output list elements +template < class T > +void printList( const std::list< T > &listRef ) +{ + if ( listRef.empty() ) + cout << "List is empty"; + + else { + std::ostream_iterator< T > output( cout, " " ); + std::copy( listRef.begin(), listRef.end(), output ); + + } // end else + +} // end function printList + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP new file mode 100644 index 0000000..c8edb5d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_18.CPP @@ -0,0 +1,57 @@ +// Fig. 21.18: fig21_18.cpp +// Standard library class deque test program. +#include + +using std::cout; +using std::endl; + +#include // deque class-template definition +#include // copy algorithm + +int main() +{ + std::deque< double > values; + std::ostream_iterator< double > output( cout, " " ); + + // insert elements in values + values.push_front( 2.2 ); + values.push_front( 3.5 ); + values.push_back( 1.1 ); + + cout << "values contains: "; + + // use subscript operator to obtain elements of values + for ( int i = 0; i < values.size(); ++i ) + cout << values[ i ] << ' '; + + values.pop_front(); // remove first element + + cout << "\nAfter pop_front, values contains: "; + std::copy( values.begin(), values.end(), output ); + + // use subscript operator to modify element at location 1 + values[ 1 ] = 5.4; + + cout << "\nAfter values[ 1 ] = 5.4, values contains: "; + std::copy( values.begin(), values.end(), output ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP new file mode 100644 index 0000000..8b3e890 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_19.CPP @@ -0,0 +1,90 @@ +// Fig. 21.19: fig21_19.cpp +// Testing Standard Library class multiset +#include + +using std::cout; +using std::endl; + +#include // multiset class-template definition + +// define short name for multiset type used in this program +typedef std::multiset< int, std::less< int > > ims; + +#include // copy algorithm + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 }; + + ims intMultiset; // ims is typedef for "integer multiset" + std::ostream_iterator< int > output( cout, " " ); + + cout << "There are currently " << intMultiset.count( 15 ) + << " values of 15 in the multiset\n"; + + intMultiset.insert( 15 ); // insert 15 in intMultiset + intMultiset.insert( 15 ); // insert 15 in intMultiset + + cout << "After inserts, there are " + << intMultiset.count( 15 ) + << " values of 15 in the multiset\n\n"; + + // iterator that cannot be used to change element values + ims::const_iterator result; + + // find 15 in intMultiset; find returns iterator + result = intMultiset.find( 15 ); + + if ( result != intMultiset.end() ) // if iterator not at end + cout << "Found value 15\n"; // found search value 15 + + // find 20 in intMultiset; find returns iterator + result = intMultiset.find( 20 ); + + if ( result == intMultiset.end() ) // will be true hence + cout << "Did not find value 20\n"; // did not find 20 + + // insert elements of array a into intMultiset + intMultiset.insert( a, a + SIZE ); + + cout << "\nAfter insert, intMultiset contains:\n"; + std::copy( intMultiset.begin(), intMultiset.end(), output ); + + // determine lower and upper bound of 22 in intMultiset + cout << "\n\nLower bound of 22: " + << *( intMultiset.lower_bound( 22 ) ); + cout << "\nUpper bound of 22: " + << *( intMultiset.upper_bound( 22 ) ); + + // p represents pair of const_iterators + std::pair< ims::const_iterator, ims::const_iterator > p; + + // use equal_range to determine lower and upper bound + // of 22 in intMultiset + p = intMultiset.equal_range( 22 ); + + cout << "\n\nequal_range of 22:" + << "\n Lower bound: " << *( p.first ) + << "\n Upper bound: " << *( p.second ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP new file mode 100644 index 0000000..76415d7 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_20.CPP @@ -0,0 +1,68 @@ +// Fig. 21.20: fig21_20.cpp +// Standard library class set test program. +#include + +using std::cout; +using std::endl; + +#include + +// define short name for set type used in this program +typedef std::set< double, std::less< double > > double_set; + +#include + +int main() +{ + const int SIZE = 5; + double a[ SIZE ] = { 2.1, 4.2, 9.5, 2.1, 3.7 }; + + double_set doubleSet( a, a + SIZE );; + std::ostream_iterator< double > output( cout, " " ); + + cout << "doubleSet contains: "; + std::copy( doubleSet.begin(), doubleSet.end(), output ); + + // p represents pair containing const_iterator and bool + std::pair< double_set::const_iterator, bool > p; + + // insert 13.8 in doubleSet; insert returns pair in which + // p.first represents location of 13.8 in doubleSet and + // p.second represents whether 13.8 was inserted + p = doubleSet.insert( 13.8 ); // value not in set + + cout << "\n\n" << *( p.first ) + << ( p.second ? " was" : " was not" ) << " inserted"; + + cout << "\ndoubleSet contains: "; + std::copy( doubleSet.begin(), doubleSet.end(), output ); + + // insert 9.5 in doubleSet + p = doubleSet.insert( 9.5 ); // value already in set + + cout << "\n\n" << *( p.first ) + << ( p.second ? " was" : " was not" ) << " inserted"; + + cout << "\ndoubleSet contains: "; + std::copy( doubleSet.begin(), doubleSet.end(), output ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP new file mode 100644 index 0000000..66036ae --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_21.CPP @@ -0,0 +1,62 @@ +// Fig. 21.21: fig21_21.cpp +// Standard library class multimap test program. +#include + +using std::cout; +using std::endl; + +#include // map class-template definition + +// define short name for multimap type used in this program +typedef std::multimap< int, double, std::less< int > > mmid; + +int main() +{ + mmid pairs; + + cout << "There are currently " << pairs.count( 15 ) + << " pairs with key 15 in the multimap\n"; + + // insert two value_type objects in pairs + pairs.insert( mmid::value_type( 15, 2.7 ) ); + pairs.insert( mmid::value_type( 15, 99.3 ) ); + + cout << "After inserts, there are " + << pairs.count( 15 ) + << " pairs with key 15\n\n"; + + // insert five value_type objects in pairs + pairs.insert( mmid::value_type( 30, 111.11 ) ); + pairs.insert( mmid::value_type( 10, 22.22 ) ); + pairs.insert( mmid::value_type( 25, 33.333 ) ); + pairs.insert( mmid::value_type( 20, 9.345 ) ); + pairs.insert( mmid::value_type( 5, 77.54 ) ); + + cout << "Multimap pairs contains:\nKey\tValue\n"; + + // use const_iterator to walk through elements of pairs + for ( mmid::const_iterator iter = pairs.begin(); + iter != pairs.end(); ++iter ) + cout << iter->first << '\t' + << iter->second << '\n'; + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP new file mode 100644 index 0000000..5dc0f95 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_22.CPP @@ -0,0 +1,68 @@ +// Fig. 21.22: fig21_22.cpp +// Standard library class map test program. +#include + +using std::cout; +using std::endl; + +#include // map class-template definition + +// define short name for map type used in this program +typedef std::map< int, double, std::less< int > > mid; + +int main() +{ + mid pairs; + + // insert eight value_type objects in pairs + pairs.insert( mid::value_type( 15, 2.7 ) ); + pairs.insert( mid::value_type( 30, 111.11 ) ); + pairs.insert( mid::value_type( 5, 1010.1 ) ); + pairs.insert( mid::value_type( 10, 22.22 ) ); + pairs.insert( mid::value_type( 25, 33.333 ) ); + pairs.insert( mid::value_type( 5, 77.54 ) ); // dupe ignored + pairs.insert( mid::value_type( 20, 9.345 ) ); + pairs.insert( mid::value_type( 15, 99.3 ) ); // dupe ignored + + cout << "pairs contains:\nKey\tValue\n"; + + // use const_iterator to walk through elements of pairs + for ( mid::const_iterator iter = pairs.begin(); + iter != pairs.end(); ++iter ) + cout << iter->first << '\t' + << iter->second << '\n'; + + // use subscript operator to change value for key 25 + pairs[ 25 ] = 9999.99; + + // use subscript operator insert value for key 40 + pairs[ 40 ] = 8765.43; + + cout << "\nAfter subscript operations, pairs contains:" + << "\nKey\tValue\n"; + + for ( mid::const_iterator iter2 = pairs.begin(); + iter2 != pairs.end(); ++iter2 ) + cout << iter2->first << '\t' + << iter2->second << '\n'; + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP new file mode 100644 index 0000000..6614c19 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_23.CPP @@ -0,0 +1,74 @@ +// Fig. 21.23: fig21_23.cpp +// Standard library adapter stack test program. +#include + +using std::cout; +using std::endl; + +#include // stack adapter definition +#include // vector class-template definition +#include // list class-template definition + +// popElements function-template prototype +template< class T > +void popElements( T &stackRef ); + +int main() +{ + // stack with default underlying deque + std::stack< int > intDequeStack; + + // stack with underlying vector + std::stack< int, std::vector< int > > intVectorStack; + + // stack with underlying list + std::stack< int, std::list< int > > intListStack; + + // push the values 0-9 onto each stack + for ( int i = 0; i < 10; ++i ) { + intDequeStack.push( i ); + intVectorStack.push( i ); + intListStack.push( i ); + + } // end for + + // display and remove elements from each stack + cout << "Popping from intDequeStack: "; + popElements( intDequeStack ); + cout << "\nPopping from intVectorStack: "; + popElements( intVectorStack ); + cout << "\nPopping from intListStack: "; + popElements( intListStack ); + + cout << endl; + + return 0; + +} // end main + +// pop elements from stack object to which stackRef refers +template< class T > +void popElements( T &stackRef ) +{ + while ( !stackRef.empty() ) { + cout << stackRef.top() << ' '; // view top element + stackRef.pop(); // remove top element + + } // end while + +} // end function popElements + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP new file mode 100644 index 0000000..14fd951 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_24.CPP @@ -0,0 +1,46 @@ +// Fig. 21.24: fig21_24.cpp +// Standard library adapter queue test program. +#include + +using std::cout; +using std::endl; + +#include // queue adapter definition + +int main() +{ + std::queue< double > values; + + // push elements onto queue values + values.push( 3.2 ); + values.push( 9.8 ); + values.push( 5.4 ); + + cout << "Popping from values: "; + + while ( !values.empty() ) { + cout << values.front() << ' '; // view front element + values.pop(); // remove element + + } // end while + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP new file mode 100644 index 0000000..f4b0e2d --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_25.CPP @@ -0,0 +1,46 @@ +// Fig. 21.25: fig21_25.cpp +// Standard library adapter priority_queue test program. +#include + +using std::cout; +using std::endl; + +#include // priority_queue adapter definition + +int main() +{ + std::priority_queue< double > priorities; + + // push elements onto priorities + priorities.push( 3.2 ); + priorities.push( 9.8 ); + priorities.push( 5.4 ); + + cout << "Popping from priorities: "; + + while ( !priorities.empty() ) { + cout << priorities.top() << ' '; // view top element + priorities.pop(); // remove top element + + } // end while + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP new file mode 100644 index 0000000..b1aa127 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_26.CPP @@ -0,0 +1,73 @@ +// Fig. 21.26: fig21_26.cpp +// Standard library algorithms fill, fill_n, generate +// and generate_n. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +char nextLetter(); // prototype + +int main() +{ + std::vector< char > chars( 10 ); + std::ostream_iterator< char > output( cout, " " ); + + // fill chars with 5s + std::fill( chars.begin(), chars.end(), '5' ); + + cout << "Vector chars after filling with 5s:\n"; + std::copy( chars.begin(), chars.end(), output ); + + // fill first five elements of chars with As + std::fill_n( chars.begin(), 5, 'A' ); + + cout << "\n\nVector chars after filling five elements" + << " with As:\n"; + std::copy( chars.begin(), chars.end(), output ); + + // generate values for all elements of chars with nextLetter + std::generate( chars.begin(), chars.end(), nextLetter ); + + cout << "\n\nVector chars after generating letters A-J:\n"; + std::copy( chars.begin(), chars.end(), output ); + + // generate values for first five elements of chars + // with nextLetter + std::generate_n( chars.begin(), 5, nextLetter ); + + cout << "\n\nVector chars after generating K-O for the" + << " first five elements:\n"; + std::copy( chars.begin(), chars.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// returns next letter in the alphabet (starts with A) +char nextLetter() +{ + static char letter = 'A'; + return letter++; + +} // end function nextLetter + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp new file mode 100644 index 0000000..c016b57 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_27.cpp @@ -0,0 +1,86 @@ +// Fig. 21.27: fig21_27.cpp +// Standard library functions equal, +// mismatch and lexicographical_compare. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +int main() +{ + const int SIZE = 10; + int a1[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int a2[ SIZE ] = { 1, 2, 3, 4, 1000, 6, 7, 8, 9, 10 }; + + std::vector< int > v1( a1, a1 + SIZE ); + std::vector< int > v2( a1, a1 + SIZE ); + std::vector< int > v3( a2, a2 + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + cout << "\nVector v2 contains: "; + std::copy( v2.begin(), v2.end(), output ); + cout << "\nVector v3 contains: "; + std::copy( v3.begin(), v3.end(), output ); + + // compare vectors v1 and v2 for equality + bool result = + std::equal( v1.begin(), v1.end(), v2.begin() ); + + cout << "\n\nVector v1 " << ( result ? "is" : "is not" ) + << " equal to vector v2.\n"; + + // compare vectors v1 and v3 for equality + result = std::equal( v1.begin(), v1.end(), v3.begin() ); + cout << "Vector v1 " << ( result ? "is" : "is not" ) + << " equal to vector v3.\n"; + + // location represents pair of vector iterators + std::pair< std::vector< int >::iterator, + std::vector< int >::iterator > location; + + // check for mismatch between v1 and v3 + location = + std::mismatch( v1.begin(), v1.end(), v3.begin() ); + + cout << "\nThere is a mismatch between v1 and v3 at " + << "location " << ( location.first - v1.begin() ) + << "\nwhere v1 contains " << *location.first + << " and v3 contains " << *location.second + << "\n\n"; + + char c1[ SIZE ] = "HELLO"; + char c2[ SIZE ] = "BYE BYE"; + + // perform lexicographical comparison of c1 and c2 + result = std::lexicographical_compare( + c1, c1 + SIZE, c2, c2 + SIZE ); + + cout << c1 + << ( result ? " is less than " : + " is greater than or equal to " ) + << c2 << 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/Prog2/Codebeispiele/7_ch21/fig21_28.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp new file mode 100644 index 0000000..9d36184 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_28.cpp @@ -0,0 +1,102 @@ +// Fig. 21.28: fig21_28.cpp +// Standard library functions remove, remove_if, +// remove_copy and remove_copy_if. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +bool greater9( int ); // prototype + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; + + std::ostream_iterator< int > output( cout, " " ); + + std::vector< int > v( a, a + SIZE ); + std::vector< int >::iterator newLastElement; + + cout << "Vector v before removing all 10s:\n "; + std::copy( v.begin(), v.end(), output ); + + // remove 10 from v + newLastElement = std::remove( v.begin(), v.end(), 10 ); + + cout << "\nVector v after removing all 10s:\n "; + std::copy( v.begin(), newLastElement, output ); + + std::vector< int > v2( a, a + SIZE ); + std::vector< int > c( SIZE, 0 ); + + cout << "\n\nVector v2 before removing all 10s " + << "and copying:\n "; + std::copy( v2.begin(), v2.end(), output ); + + // copy from v2 to c, removing 10s in the process + std::remove_copy( v2.begin(), v2.end(), c.begin(), 10 ); + + cout << "\nVector c after removing all 10s from v2:\n "; + std::copy( c.begin(), c.end(), output ); + + std::vector< int > v3( a, a + SIZE ); + + cout << "\n\nVector v3 before removing all elements" + << "\ngreater than 9:\n "; + std::copy( v3.begin(), v3.end(), output ); + + // remove elements greater than 9 from v3 + newLastElement = + std::remove_if( v3.begin(), v3.end(), greater9 ); + + cout << "\nVector v3 after removing all elements" + << "\ngreater than 9:\n "; + std::copy( v3.begin(), newLastElement, output ); + + std::vector< int > v4( a, a + SIZE ); + std::vector< int > c2( SIZE, 0 ); + + cout << "\n\nVector v4 before removing all elements" + << "\ngreater than 9 and copying:\n "; + std::copy( v4.begin(), v4.end(), output ); + + // copy elements from v4 to c2, removing elements greater + // than 9 in the process + std::remove_copy_if( + v4.begin(), v4.end(), c2.begin(), greater9 ); + + cout << "\nVector c2 after removing all elements" + << "\ngreater than 9 from v4:\n "; + std::copy( c2.begin(), c2.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 9 +bool greater9( int x ) +{ + return x > 9; + +} // end greater9 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP new file mode 100644 index 0000000..252fed4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_29.CPP @@ -0,0 +1,99 @@ +// Fig. 21.29: fig21_29.cpp +// Standard library functions replace, replace_if, +// replace_copy and replace_copy_if. +#include + +using std::cout; +using std::endl; + +#include +#include + +bool greater9( int ); + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; + + std::ostream_iterator< int > output( cout, " " ); + + std::vector< int > v1( a, a + SIZE ); + cout << "Vector v1 before replacing all 10s:\n "; + std::copy( v1.begin(), v1.end(), output ); + + // replace 10s in v1 with 100 + std::replace( v1.begin(), v1.end(), 10, 100 ); + + cout << "\nVector v1 after replacing 10s with 100s:\n "; + std::copy( v1.begin(), v1.end(), output ); + + std::vector< int > v2( a, a + SIZE ); + std::vector< int > c1( SIZE ); + + cout << "\n\nVector v2 before replacing all 10s " + << "and copying:\n "; + std::copy( v2.begin(), v2.end(), output ); + + // copy from v2 to c1, replacing 10s with 100s + std::replace_copy( + v2.begin(), v2.end(), c1.begin(), 10, 100 ); + + cout << "\nVector c1 after replacing all 10s in v2:\n "; + std::copy( c1.begin(), c1.end(), output ); + + std::vector< int > v3( a, a + SIZE ); + + cout << "\n\nVector v3 before replacing values greater" + << " than 9:\n "; + std::copy( v3.begin(), v3.end(), output ); + + // replace values greater than 9 in v3 with 100 + std::replace_if( v3.begin(), v3.end(), greater9, 100 ); + + cout << "\nVector v3 after replacing all values greater" + << "\nthan 9 with 100s:\n "; + std::copy( v3.begin(), v3.end(), output ); + + std::vector< int > v4( a, a + SIZE ); + std::vector< int > c2( SIZE ); + + cout << "\n\nVector v4 before replacing all values greater " + << "than 9 and copying:\n "; + std::copy( v4.begin(), v4.end(), output ); + + // copy v4 to c2, replacing elements greater than 9 with 100 + std::replace_copy_if( + v4.begin(), v4.end(), c2.begin(), greater9, 100 ); + + cout << "\nVector c2 after replacing all values greater " + << "than 9 in v4:\n "; + std::copy( c2.begin(), c2.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 9 +bool greater9( int x ) +{ + return x > 9; + +} // end function greater9 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP new file mode 100644 index 0000000..c961976 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_30.CPP @@ -0,0 +1,116 @@ +// Fig. 21.30: fig21_30.cpp +// Mathematical algorithms of the standard library. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // accumulate is defined here +#include + +bool greater9( int ); +void outputSquare( int ); +int calculateCube( int ); + +int main() +{ + const int SIZE = 10; + int a1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + std::vector< int > v( a1, a1 + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v before random_shuffle: "; + std::copy( v.begin(), v.end(), output ); + + // shuffle elements of v + std::random_shuffle( v.begin(), v.end() ); + + cout << "\nVector v after random_shuffle: "; + std::copy( v.begin(), v.end(), output ); + + int a2[] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 }; + std::vector< int > v2( a2, a2 + SIZE ); + + cout << "\n\nVector v2 contains: "; + std::copy( v2.begin(), v2.end(), output ); + + // count number of elements in v2 with value 8 + int result = std::count( v2.begin(), v2.end(), 8 ); + + std::cout << "\nNumber of elements matching 8: " << result; + + // count number of elements in v2 that are greater than 9 + result = std::count_if( v2.begin(), v2.end(), greater9 ); + + cout << "\nNumber of elements greater than 9: " << result; + + // locate minimum element in v2 + cout << "\n\nMinimum element in Vector v2 is: " + << *( std::min_element( v2.begin(), v2.end() ) ); + + // locate maximum element in v2 + cout << "\nMaximum element in Vector v2 is: " + << *( std::max_element( v2.begin(), v2.end() ) ); + + // calculate sum of elements in v + cout << "\n\nThe total of the elements in Vector v is: " + << std::accumulate( v.begin(), v.end(), 0 ); + + cout << "\n\nThe square of every integer in Vector v is:\n"; + + // output square of every element in v + std::for_each( v.begin(), v.end(), outputSquare ); + + std::vector< int > cubes( SIZE ); + + // calculate cube of each element in v; + // place results in cubes + std::transform( + v.begin(), v.end(), cubes.begin(), calculateCube ); + + cout << "\n\nThe cube of every integer in Vector v is:\n"; + std::copy( cubes.begin(), cubes.end(), output ); + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 9 +bool greater9( int value ) +{ + return value > 9; + +} // end function greater9 + +// output square of argument +void outputSquare( int value ) +{ + cout << value * value << ' '; + +} // end function outputSquare + +// return cube of argument +int calculateCube( int value ) +{ + return value * value * value; + +} // end function calculateCube + +/************************************************************************** + * (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/Prog2/Codebeispiele/7_ch21/fig21_31.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP new file mode 100644 index 0000000..97720e4 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_31.CPP @@ -0,0 +1,97 @@ +// Fig. 21.31: fig21_31.cpp +// Standard library search and sort algorithms. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +bool greater10( int value ); // prototype + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 10, 2, 17, 5, 16, 8, 13, 11, 20, 7 }; + + std::vector< int > v( a, a + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v contains: "; + std::copy( v.begin(), v.end(), output ); + + // locate first occurrence of 16 in v + std::vector< int >::iterator location; + location = std::find( v.begin(), v.end(), 16 ); + + if ( location != v.end() ) + cout << "\n\nFound 16 at location " + << ( location - v.begin() ); + else + cout << "\n\n16 not found"; + + // locate first occurrence of 100 in v + location = std::find( v.begin(), v.end(), 100 ); + + if ( location != v.end() ) + cout << "\nFound 100 at location " + << ( location - v.begin() ); + else + cout << "\n100 not found"; + + // locate first occurrence of value greater than 10 in v + location = std::find_if( v.begin(), v.end(), greater10 ); + + if ( location != v.end() ) + cout << "\n\nThe first value greater than 10 is " + << *location << "\nfound at location " + << ( location - v.begin() ); + else + cout << "\n\nNo values greater than 10 were found"; + + // sort elements of v + std::sort( v.begin(), v.end() ); + + cout << "\n\nVector v after sort: "; + std::copy( v.begin(), v.end(), output ); + + // use binary_search to locate 13 in v + if ( std::binary_search( v.begin(), v.end(), 13 ) ) + cout << "\n\n13 was found in v"; + else + cout << "\n\n13 was not found in v"; + + // use binary_search to locate 100 in v + if ( std::binary_search( v.begin(), v.end(), 100 ) ) + cout << "\n100 was found in v"; + else + cout << "\n100 was not found in v"; + + cout << endl; + + return 0; + +} // end main + +// determine whether argument is greater than 10 +bool greater10( int value ) +{ + return value > 10; + +} // end function greater10 + +/************************************************************************** + * (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice * + * Hall. All Rights Reserved. * + * * + * DISCLAIMER: The authors and publisher of this book have used their * + * best efforts in preparing the book. These efforts include the * + * development, research, and testing of the theories and programs * + * to determine their effectiveness. The authors and publisher make * + * no warranty of any kind, expressed or implied, with regard to these * + * programs or to the documentation contained in these books. The authors * + * and publisher shall not be liable in any event for incidental or * + * consequential damages in connection with, or arising out of, the * + * furnishing, performance, or use of these programs. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP new file mode 100644 index 0000000..439a8da --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_32.CPP @@ -0,0 +1,60 @@ +// Fig. 21.32: fig21_32.cpp +// Standard library algorithms iter_swap, swap and swap_ranges. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + std::ostream_iterator< int > output( cout, " " ); + + cout << "Array a contains:\n "; + std::copy( a, a + SIZE, output ); + + // swap elements at locations 0 and 1 of array a + std::swap( a[ 0 ], a[ 1 ] ); + + cout << "\nArray a after swapping a[0] and a[1] " + << "using swap:\n "; + std::copy( a, a + SIZE, output ); + + // use iterators to swap elements at locations + // 0 and 1 of array a + std::iter_swap( &a[ 0 ], &a[ 1 ] ); + cout << "\nArray a after swapping a[0] and a[1] " + << "using iter_swap:\n "; + std::copy( a, a + SIZE, output ); + + // swap elements in first five elements of array a with + // elements in last five elements of array a + std::swap_ranges( a, a + 5, a + 5 ); + + cout << "\nArray a after swapping the first five elements\n" + << "with the last five elements:\n "; + std::copy( a, a + SIZE, output ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP new file mode 100644 index 0000000..f489c5e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_33.CPP @@ -0,0 +1,79 @@ +// Fig. 21.33: fig21_33.cpp +// Standard library functions copy_backward, merge, +// unique and reverse. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +int main() +{ + const int SIZE = 5; + int a1[ SIZE ] = { 1, 3, 5, 7, 9 }; + int a2[ SIZE ] = { 2, 4, 5, 7, 9 }; + + std::vector< int > v1( a1, a1 + SIZE ); + std::vector< int > v2( a2, a2 + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + cout << "\nVector v2 contains: "; + std::copy( v2.begin(), v2.end(), output ); + + std::vector< int > results( v1.size() ); + + // place elements of v1 into results in reverse order + std::copy_backward( v1.begin(), v1.end(), results.end() ); + + cout << "\n\nAfter copy_backward, results contains: "; + std::copy( results.begin(), results.end(), output ); + + std::vector< int > results2( v1.size() + v2.size() ); + + // merge elements of v1 and v2 into results2 in sorted order + std::merge( v1.begin(), v1.end(), v2.begin(), v2.end(), + results2.begin() ); + + cout << "\n\nAfter merge of v1 and v2 results2 contains:\n"; + std::copy( results2.begin(), results2.end(), output ); + + // eliminate duplicate values from results2 + std::vector< int >::iterator endLocation; + endLocation = + std::unique( results2.begin(), results2.end() ); + + cout << "\n\nAfter unique results2 contains:\n"; + std::copy( results2.begin(), endLocation, output ); + + cout << "\n\nVector v1 after reverse: "; + + // reverse elements of v1 + std::reverse( v1.begin(), v1.end() ); + + std::copy( v1.begin(), v1.end(), output ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP new file mode 100644 index 0000000..e893200 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_34.CPP @@ -0,0 +1,69 @@ +// Fig. 21.34: fig21_34.cpp +// Standard library algorithms inplace_merge, +// reverse_copy and unique_copy. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition +#include // back_inserter definition + +int main() +{ + const int SIZE = 10; + int a1[ SIZE ] = { 1, 3, 5, 7, 9, 1, 3, 5, 7, 9 }; + std::vector< int > v1( a1, a1 + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + + // merge first half of v1 with second half of v1 such that + // v1 contains sorted set of elements after merge + std::inplace_merge( v1.begin(), v1.begin() + 5, v1.end() ); + + cout << "\nAfter inplace_merge, v1 contains: "; + std::copy( v1.begin(), v1.end(), output ); + + std::vector< int > results1; + + // copy only unique elemements of v1 into results1 + std::unique_copy( + v1.begin(), v1.end(), std::back_inserter( results1 ) ); + + cout << "\nAfter unique_copy results1 contains: "; + std::copy( results1.begin(), results1.end(), output ); + + std::vector< int > results2; + + cout << "\nAfter reverse_copy, results2 contains: "; + + // copy elements of v1 into results2 in reverse order + std::reverse_copy( + v1.begin(), v1.end(), std::back_inserter( results2 ) ); + + std::copy( results2.begin(), results2.end(), output ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP new file mode 100644 index 0000000..10df8f5 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_35.CPP @@ -0,0 +1,94 @@ +// Fig. 21.35: fig21_35.cpp +// Standard library algorithms includes, set_difference, +// set_intersection, set_symmetric_difference and set_union. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions + +int main() +{ + const int SIZE1 = 10, SIZE2 = 5, SIZE3 = 20; + int a1[ SIZE1 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + int a2[ SIZE2 ] = { 4, 5, 6, 7, 8 }; + int a3[ SIZE2 ] = { 4, 5, 6, 11, 15 }; + std::ostream_iterator< int > output( cout, " " ); + + cout << "a1 contains: "; + std::copy( a1, a1 + SIZE1, output ); + cout << "\na2 contains: "; + std::copy( a2, a2 + SIZE2, output ); + cout << "\na3 contains: "; + std::copy( a3, a3 + SIZE2, output ); + + // determine whether set a2 is completely contained in a1 + if ( std::includes( a1, a1 + SIZE1, a2, a2 + SIZE2 ) ) + cout << "\n\na1 includes a2"; + else + cout << "\n\na1 does not include a2"; + + // determine whether set a3 is completely contained in a1 + if ( std::includes( a1, a1 + SIZE1, a3, a3 + SIZE2 ) ) + cout << "\na1 includes a3"; + else + cout << "\na1 does not include a3"; + + int difference[ SIZE1 ]; + + // determine elements of a1 not in a2 + int *ptr = std::set_difference( a1, a1 + SIZE1, + a2, a2 + SIZE2, difference ); + + cout << "\n\nset_difference of a1 and a2 is: "; + std::copy( difference, ptr, output ); + + int intersection[ SIZE1 ]; + + // determine elements in both a1 and a2 + ptr = std::set_intersection( a1, a1 + SIZE1, + a2, a2 + SIZE2, intersection ); + + cout << "\n\nset_intersection of a1 and a2 is: "; + std::copy( intersection, ptr, output ); + + int symmetric_difference[ SIZE1 ]; + + // determine elements of a1 that are not in a2 and + // elements of a2 that are not in a1 + ptr = std::set_symmetric_difference( a1, a1 + SIZE1, + a2, a2 + SIZE2, symmetric_difference ); + + cout << "\n\nset_symmetric_difference of a1 and a2 is: "; + std::copy( symmetric_difference, ptr, output ); + + int unionSet[ SIZE3 ]; + + // determine elements that are in either or both sets + ptr = std::set_union( a1, a1 + SIZE1, + a3, a3 + SIZE2, unionSet ); + + cout << "\n\nset_union of a1 and a3 is: "; + std::copy( unionSet, ptr, output ); + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP new file mode 100644 index 0000000..b742eec --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_36.CPP @@ -0,0 +1,96 @@ +// Fig. 21.36: fig21_36.cpp +// Standard library functions lower_bound, upper_bound and +// equal_range for a sorted sequence of values. +#include + +using std::cout; +using std::endl; + +#include // algorithm definitions +#include // vector class-template definition + +int main() +{ + const int SIZE = 10; + int a1[] = { 2, 2, 4, 4, 4, 6, 6, 6, 6, 8 }; + std::vector< int > v( a1, a1 + SIZE ); + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v contains:\n"; + std::copy( v.begin(), v.end(), output ); + + // determine lower-bound insertion point for 6 in v + std::vector< int >::iterator lower; + lower = std::lower_bound( v.begin(), v.end(), 6 ); + + cout << "\n\nLower bound of 6 is element " + << ( lower - v.begin() ) << " of vector v"; + + // determine upper-bound insertion point for 6 in v + std::vector< int >::iterator upper; + upper = std::upper_bound( v.begin(), v.end(), 6 ); + + cout << "\nUpper bound of 6 is element " + << ( upper - v.begin() ) << " of vector v"; + + // use equal_range to determine both the lower- and + // upper-bound insertion points for 6 + std::pair< std::vector< int >::iterator, + std::vector< int >::iterator > eq; + eq = std::equal_range( v.begin(), v.end(), 6 ); + + cout << "\nUsing equal_range:\n" + << " Lower bound of 6 is element " + << ( eq.first - v.begin() ) << " of vector v"; + cout << "\n Upper bound of 6 is element " + << ( eq.second - v.begin() ) << " of vector v"; + + cout << "\n\nUse lower_bound to locate the first point\n" + << "at which 5 can be inserted in order"; + + // determine lower-bound insertion point for 5 in v + lower = std::lower_bound( v.begin(), v.end(), 5 ); + + cout << "\n Lower bound of 5 is element " + << ( lower - v.begin() ) << " of vector v"; + + cout << "\n\nUse upper_bound to locate the last point\n" + << "at which 7 can be inserted in order"; + + // determine upper-bound insertion point for 7 in v + upper = std::upper_bound( v.begin(), v.end(), 7 ); + + cout << "\n Upper bound of 7 is element " + << ( upper - v.begin() ) << " of vector v"; + + cout << "\n\nUse equal_range to locate the first and\n" + << "last point at which 5 can be inserted in order"; + + // use equal_range to determine both the lower- and + // upper-bound insertion points for 5 + eq = std::equal_range( v.begin(), v.end(), 5 ); + + cout << "\n Lower bound of 5 is element " + << ( eq.first - v.begin() ) << " of vector v"; + cout << "\n Upper bound of 5 is element " + << ( eq.second - v.begin() ) << " of vector v" + << 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP new file mode 100644 index 0000000..496b449 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_37.CPP @@ -0,0 +1,79 @@ +// Fig. 21.37: fig21_37.cpp +// Standard library algorithms push_heap, pop_heap, +// make_heap and sort_heap. +#include + +using std::cout; +using std::endl; + +#include +#include + +int main() +{ + const int SIZE = 10; + int a[ SIZE ] = { 3, 100, 52, 77, 22, 31, 1, 98, 13, 40 }; + std::vector< int > v( a, a + SIZE ), v2; + std::ostream_iterator< int > output( cout, " " ); + + cout << "Vector v before make_heap:\n"; + std::copy( v.begin(), v.end(), output ); + + // create heap from vector v + std::make_heap( v.begin(), v.end() ); + + cout << "\nVector v after make_heap:\n"; + std::copy( v.begin(), v.end(), output ); + + // sort elements of v with sort_heap + std::sort_heap( v.begin(), v.end() ); + + cout << "\nVector v after sort_heap:\n"; + std::copy( v.begin(), v.end(), output ); + + // perform the heapsort with push_heap and pop_heap + cout << "\n\nArray a contains: "; + std::copy( a, a + SIZE, output ); + + cout << endl; + + // place elements of array a into v2 and + // maintain elements of v2 in heap + for ( int i = 0; i < SIZE; ++i ) { + v2.push_back( a[ i ] ); + std::push_heap( v2.begin(), v2.end() ); + cout << "\nv2 after push_heap(a[" << i << "]): "; + std::copy( v2.begin(), v2.end(), output ); + + } // end for + + cout << endl; + + // remove elements from heap in sorted order + for ( int j = 0; j < v2.size(); ++j ) { + cout << "\nv2 after " << v2[ 0 ] << " popped from heap\n"; + std::pop_heap( v2.begin(), v2.end() - j ); + std::copy( v2.begin(), v2.end(), output ); + + } // end for + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp new file mode 100644 index 0000000..907c83e --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_38.cpp @@ -0,0 +1,38 @@ +// Fig. 21.38: fig21_38.cpp +// Standard library algorithms min and max. +#include + +using std::cout; +using std::endl; + +#include + +int main() +{ + cout << "The minimum of 12 and 7 is: " + << std::min( 12, 7 ); + cout << "\nThe maximum of 12 and 7 is: " + << std::max( 12, 7 ); + cout << "\nThe minimum of 'G' and 'Z' is: " + << std::min( 'G', 'Z' ); + cout << "\nThe maximum of 'G' and 'Z' is: " + << std::max( 'G', 'Z' ) << 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP new file mode 100644 index 0000000..e1d8a91 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_40.CPP @@ -0,0 +1,82 @@ +// Fig. 21.40: fig21_40.cpp +// Using a bitset to demonstrate the Sieve of Eratosthenes. +#include + +using std::cin; +using std::cout; +using std::endl; + +#include + +using std::setw; + +#include // bitset class definition +#include // sqrt prototype + +int main() +{ + const int size = 1024; + int value; + std::bitset< size > sieve; + + sieve.flip(); + + // perform Sieve of Eratosthenes + int finalBit = sqrt( sieve.size() ) + 1; + + for ( int i = 2; i < finalBit; ++i ) + + if ( sieve.test( i ) ) + + for ( int j = 2 * i; j < size; j += i ) + sieve.reset( j ); + + cout << "The prime numbers in the range 2 to 1023 are:\n"; + + // display prime numbers in range 2-1023 + for ( int k = 2, counter = 0; k < size; ++k ) + + if ( sieve.test( k ) ) { + cout << setw( 5 ) << k; + + if ( ++counter % 12 == 0 ) + cout << '\n'; + + } // end outer if + + cout << endl; + + // get value from user to determine whether value is prime + cout << "\nEnter a value from 1 to 1023 (-1 to end): "; + cin >> value; + + while ( value != -1 ) { + + if ( sieve[ value ] ) + cout << value << " is a prime number\n"; + else + cout << value << " is not a prime number\n"; + + cout << "\nEnter a value from 2 to 1023 (-1 to end): "; + cin >> value; + + } // end while + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp new file mode 100644 index 0000000..4dc79f8 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/7_ch21/fig21_42.cpp @@ -0,0 +1,86 @@ +// Fig. 21.42: fig21_42.cpp +// Demonstrating function objects. +#include + +using std::cout; +using std::endl; + +#include // vector class-template definition +#include // copy algorithm +#include // accumulate algorithm +#include // binary_function definition + +// binary function adds square of its second argument and +// running total in its first argument, then returns sum +int sumSquares( int total, int value ) +{ + return total + value * value; + +} // end function sumSquares + +// binary function class template defines overloaded operator() +// that adds suare of its second argument and running total in +// its first argument, then returns sum +template< class T > +class SumSquaresClass : public std::binary_function< T, T, T > { + +public: + + // add square of value to total and return result + const T operator()( const T &total, const T &value ) + { + return total + value * value; + + } // end function operator() + +}; // end class SumSquaresClass + +int main() +{ + const int SIZE = 10; + int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; + + std::vector< int > integers( array, array + SIZE ); + + std::ostream_iterator< int > output( cout, " " ); + + int result = 0; + + cout << "vector v contains:\n"; + std::copy( integers.begin(), integers.end(), output ); + + // calculate sum of squares of elements of vector integers + // using binary function sumSquares + result = std::accumulate( integers.begin(), integers.end(), + 0, sumSquares ); + + cout << "\n\nSum of squares of elements in integers using " + << "binary\nfunction sumSquares: " << result; + + // calculate sum of squares of elements of vector integers + // using binary-function object + result = std::accumulate( integers.begin(), integers.end(), + 0, SumSquaresClass< int >() ); + + cout << "\n\nSum of squares of elements in integers using " + << "binary\nfunction object of type " + << "SumSquaresClass< int >: " << result << 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp new file mode 100644 index 0000000..1d3ba0b --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/Fig13_01.cpp @@ -0,0 +1,89 @@ +// Fig. 13.1: fig13_01.cpp +// A simple exception-handling example that checks for +// divide-by-zero exceptions. +#include + +using std::cout; +using std::cin; +using std::endl; + +#include + +using std::exception; + +// DivideByZeroException objects should be thrown by functions +// upon detecting division-by-zero exceptions +class DivideByZeroException : public exception { + +public: + + // constructor specifies default error message + DivideByZeroException::DivideByZeroException() + : exception( "attempted to divide by zero" ) {} + +}; // end class DivideByZeroException + +// perform division and throw DivideByZeroException object if +// divide-by-zero exception occurs +double quotient( int numerator, int denominator ) +{ + // throw DivideByZeroException if trying to divide by zero + if ( denominator == 0 ) + throw DivideByZeroException(); // terminate function + + // return division result + return static_cast< double >( numerator ) / denominator; + +} // end function quotient + +int main() +{ + int number1; // user-specified numerator + int number2; // user-specified denominator + double result; // result of division + + cout << "Enter two integers (end-of-file to end): "; + + // enable user to enter two integers to divide + while ( cin >> number1 >> number2 ) { + + // try block contains code that might throw exception + // and code that should not execute if an exception occurs + try { + result = quotient( number1, number2 ); + cout << "The quotient is: " << result << endl; + + } // end try + + // exception handler handles a divide-by-zero exception + catch ( DivideByZeroException ÷ByZeroException ) { + cout << "Exception occurred: " << + divideByZeroException.what() << endl; + + } // end catch + + cout << "\nEnter two integers (end-of-file to end): "; + + } // end while + + cout << endl; + + return 0; // terminate normally + +} // 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp new file mode 100644 index 0000000..f685afb --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_02.cpp @@ -0,0 +1,71 @@ +// Fig. 13.2: fig13_02.cpp +// Demonstrating exception rethrowing. +#include + +using std::cout; +using std::endl; + +#include + +using std::exception; + +// throw, catch and rethrow exception +void throwException() +{ + // throw exception and catch it immediately + try { + cout << " Function throwException throws an exception\n"; + throw exception(); // generate exception + + } // end try + + // handle exception + catch ( exception &caughtException ) { + cout << " Exception handled in function throwException" + << "\n Function throwException rethrows exception"; + + throw; // rethrow exception for further processing + + } // end catch + + cout << "This also should not print\n"; + +} // end function throwException + +int main() +{ + // throw exception + try { + cout << "\nmain invokes function throwException\n"; + throwException(); + cout << "This should not print\n"; + + } // end try + + // handle exception + catch ( exception &caughtException ) { + cout << "\n\nException handled in main\n"; + + } // end catch + + cout << "Program control continues after catch in main\n"; + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp new file mode 100644 index 0000000..c9147e8 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_03.cpp @@ -0,0 +1,63 @@ +// Fig. 13.3: fig13_03.cpp +// Demonstrating stack unwinding. +#include + +using std::cout; +using std::endl; + +#include + +using std::runtime_error; + +// function3 throws run-time error +void function3() throw ( runtime_error ) +{ + throw runtime_error( "runtime_error in function3" ); // fourth +} + +// function2 invokes function3 +void function2() throw ( runtime_error ) +{ + function3(); // third +} + +// function1 invokes function2 +void function1() throw ( runtime_error ) +{ + function2(); // second +} + +// demonstrate stack unwinding +int main() +{ + // invoke function1 + try { + function1(); // first + + } // end try + + // handle run-time error + catch ( runtime_error &error ) // fifth + { + cout << "Exception occurred: " << error.what() << endl; + + } // end catch + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp new file mode 100644 index 0000000..34f588a --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_04.cpp @@ -0,0 +1,49 @@ +// Fig. 13.4: fig13_04.cpp +// Demonstrating pre-standard new returning 0 when memory +// is not allocated. +#include + +using std::cout; + +int main() +{ + double *ptr[ 50 ]; + + // allocate memory for ptr + for ( int i = 0; i < 50; i++ ) { + ptr[ i ] = new double[ 5000000 ]; + + // new returns 0 on failure to allocate memory + if ( ptr[ i ] == 0 ) { + cout << "Memory allocation failed for ptr[ " + << i << " ]\n"; + + break; + + } // end if + + // successful memory allocation + else + cout << "Allocated 5000000 doubles in ptr[ " + << i << " ]\n"; + + } // end for + + 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/Prog2/Codebeispiele/8_ch13/fig13_05.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp new file mode 100644 index 0000000..4fde5dd --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_05.cpp @@ -0,0 +1,54 @@ +// Fig. 13.5: fig13_05.cpp +// Demonstrating standard new throwing bad_alloc when memory +// cannot be allocated. +#include + +using std::cout; +using std::endl; + +#include // standard operator new + +using std::bad_alloc; + +int main() +{ + double *ptr[ 50 ]; + + // attempt to allocate memory + try { + + // allocate memory for ptr[ i ]; new throws bad_alloc + // on failure + for ( int i = 0; i < 50; i++ ) { + ptr[ i ] = new double[ 5000000 ]; + cout << "Allocated 5000000 doubles in ptr[ " + << i << " ]\n"; + } + + } // end try + + // handle bad_alloc exception + catch ( bad_alloc &memoryAllocationException ) { + cout << "Exception occurred: " + << memoryAllocationException.what() << endl; + + } // end catch + + 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/Prog2/Codebeispiele/8_ch13/fig13_06.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp new file mode 100644 index 0000000..b9656fb --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_06.cpp @@ -0,0 +1,53 @@ +// Fig. 13.6: fig13_06.cpp +// Demonstrating set_new_handler. +#include + +using std::cout; +using std::cerr; + +#include // standard operator new +#include // abort function prototype + +void customNewHandler() +{ + cerr << "customNewHandler was called"; + abort(); +} + +// using set_new_handler to handle failed memory allocation +int main() +{ + double *ptr[ 50 ]; + + // specify that customNewHandler should be called on failed + // memory allocation + set_new_handler( customNewHandler ); + + // allocate memory for ptr[ i ]; customNewHandler will be + // called on failed memory allocation + for ( int i = 0; i < 50; i++ ) { + ptr[ i ] = new double[ 5000000 ]; + + cout << "Allocated 5000000 doubles in ptr[ " + << i << " ]\n"; + + } // end for + + 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp new file mode 100644 index 0000000..975c0fc --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/8_ch13/fig13_07.cpp @@ -0,0 +1,86 @@ +// Fig. 13.7: fig13_07.cpp +// Demonstrating auto_ptr. +#include + +using std::cout; +using std::endl; + +#include + +using std::auto_ptr; // auto_ptr class definition + +class Integer { + +public: + + // Integer constructor + Integer( int i = 0 ) + : value( i ) + { + cout << "Constructor for Integer " << value << endl; + + } // end Integer constructor + + // Integer destructor + ~Integer() + { + cout << "Destructor for Integer " << value << endl; + + } // end Integer destructor + + // functions to set Integer + void setInteger( int i ) + { + value = i; + + } // end function setInteger + + // function to return Integer + int getInteger() const + { + return value; + + } // end function getInteger + +private: + int value; + +}; // end class Integer + +// use auto_ptr to manipulate Integer object +int main() +{ + cout << "Creating an auto_ptr object that points to an " + << "Integer\n"; + + // "aim" auto_ptr at Integer object + auto_ptr< Integer > ptrToInteger( new Integer( 7 ) ); + + cout << "\nUsing the auto_ptr to manipulate the Integer\n"; + + // use auto_ptr to set Integer value + ptrToInteger->setInteger( 99 ); + + // use auto_ptr to get Integer value + cout << "Integer after setInteger: " + << ( *ptrToInteger ).getInteger() + << "\n\nTerminating program" << 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. * + *************************************************************************/ \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp b/Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp new file mode 100644 index 0000000..306ae62 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/Student/StudDat.cpp @@ -0,0 +1,35 @@ +// Persistence of objects: Storing of Student-Objects +// Author: Hans-Peter Weber +// Date: 07.03.05 + +#include +using std::cout; +using std::endl; +using std::ios; + +#include +using std::fstream; + +#include "Student.h" + +int main() +{ + Student stud; + + cout << "Adresse von Student-Objekt: " << &stud << endl; + cout << "Groesse von Student-Objekt: " << sizeof( Student ) << " Byte" << endl; + +/* fstream outFile( "Studs.seq", ios::binary | ios::out ); // write + stud.set( "Hans Castorp", 578111, 23 ); + stud.write( outFile ); + stud.set( "Claudia Chauchat", 578666, 27 ); + stud.write( outFile ); + outFile.close(); +*/ + fstream inFile( "Studs.seq", ios::binary | ios::in ); // read + while( stud.read( inFile ) ) + stud.print(); + inFile.close(); + + return 0; +} \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/Student/Student.cpp b/Bachelor/Prog2/Codebeispiele/Student/Student.cpp new file mode 100644 index 0000000..d58d6df --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/Student/Student.cpp @@ -0,0 +1,49 @@ +// Student.cpp: Implementation of class Student. + +#include +using std::cout; +using std::endl; +using std::istream; +using std::ostream; + +#include +using std::string; +using std::getline; + +#include "Student.h" + +Student::Student() +{ +} + +Student::~Student() +{ +} + +void Student::set( string n, int m, int a ) +{ + name = n; + matNr = m; + age = a; +} + +void Student::print() +{ + cout << name << ", Matrikelnummer: " << matNr << ", Alter: " << age << endl; +} + +ostream& Student::write( std::ostream& os ) const +{ +// os.write( ( char* )&matNr, sizeof matNr ); // stores '578111' as an int in 4 Bytes +// os << matNr; // stores '578111' as ASCII-Code in 6 Bytes! + os << name << '\0'; // write string + os.write( ( char* ) &matNr, 2 * sizeof( int ) ); // write 2 int starting at address of matNr + return os; +} + +istream& Student::read( std::istream& is ) +{ + getline( is, name, '\0' ); // read string + is.read( ( char* ) &matNr, 2 * sizeof( int ) ); // read 2 int starting at address of matNr + return is; +} \ No newline at end of file diff --git a/Bachelor/Prog2/Codebeispiele/Student/Student.h b/Bachelor/Prog2/Codebeispiele/Student/Student.h new file mode 100644 index 0000000..3bcb426 --- /dev/null +++ b/Bachelor/Prog2/Codebeispiele/Student/Student.h @@ -0,0 +1,26 @@ +// Student.h: Interface for class Student. + +#if !defined STUDENT_H +#define STUDENT_H + +#include + +class Student +{ + +public: + Student(); + ~Student(); + void set( std::string, int, int ); + void print(); + std::ostream& write( std::ostream& ) const; + std::istream& read( std::istream& ); + +private: + std::string name; + int matNr; + int age; + +}; + +#endif diff --git a/Bachelor/Prog2/Codebeispiele/ex9_21.zip b/Bachelor/Prog2/Codebeispiele/ex9_21.zip new file mode 100644 index 0000000..36256da Binary files /dev/null and b/Bachelor/Prog2/Codebeispiele/ex9_21.zip differ -- cgit v1.2.3