├── Beginning C++ 17 source code ├── Examples │ ├── Chapter 01 │ │ ├── Ex1_01.cpp │ │ └── Ex1_02.cpp │ ├── Chapter 02 │ │ ├── Ex2_01.cpp │ │ ├── Ex2_02.cpp │ │ ├── Ex2_03.cpp │ │ ├── Ex2_04.cpp │ │ └── Ex2_05.cpp │ ├── Chapter 03 │ │ ├── Ex3_01.cpp │ │ ├── Ex3_02.cpp │ │ └── Ex3_03.cpp │ ├── Chapter 04 │ │ ├── Ex4_01.cpp │ │ ├── Ex4_02.cpp │ │ ├── Ex4_03.cpp │ │ ├── Ex4_03A.cpp │ │ ├── Ex4_04.cpp │ │ ├── Ex4_05.cpp │ │ ├── Ex4_06.cpp │ │ ├── Ex4_07.cpp │ │ ├── Ex4_08.cpp │ │ └── Ex4_08A.cpp │ ├── Chapter 05 │ │ ├── Ex5_01.cpp │ │ ├── Ex5_02.cpp │ │ ├── Ex5_03.cpp │ │ ├── Ex5_03A.cpp │ │ ├── Ex5_04.cpp │ │ ├── Ex5_04A.cpp │ │ ├── Ex5_05.cpp │ │ ├── Ex5_06.cpp │ │ ├── Ex5_07.cpp │ │ ├── Ex5_07A.cpp │ │ ├── Ex5_08.cpp │ │ ├── Ex5_09.cpp │ │ ├── Ex5_10.cpp │ │ ├── Ex5_11.cpp │ │ ├── Ex5_12.cpp │ │ ├── Ex5_12A.cpp │ │ ├── Ex5_13.cpp │ │ ├── Ex5_14.cpp │ │ └── Ex5_15.cpp │ ├── Chapter 06 │ │ ├── Ex6_01.cpp │ │ ├── Ex6_02.cpp │ │ ├── Ex6_03.cpp │ │ ├── Ex6_04.cpp │ │ ├── Ex6_05.cpp │ │ ├── Ex6_06.cpp │ │ └── Ex6_07.cpp │ ├── Chapter 07 │ │ ├── Ex7_01.cpp │ │ ├── Ex7_01A.cpp │ │ ├── Ex7_02.cpp │ │ ├── Ex7_02A.cpp │ │ ├── Ex7_03.cpp │ │ ├── Ex7_04.cpp │ │ ├── Ex7_05.cpp │ │ ├── Ex7_06.cpp │ │ ├── Ex7_07.cpp │ │ └── Ex7_08.cpp │ ├── Chapter 08 │ │ ├── Ex8_01.cpp │ │ ├── Ex8_02.cpp │ │ ├── Ex8_03.cpp │ │ ├── Ex8_04.cpp │ │ ├── Ex8_05.cpp │ │ ├── Ex8_05A.cpp │ │ ├── Ex8_06.cpp │ │ ├── Ex8_06A.cpp │ │ ├── Ex8_07.cpp │ │ ├── Ex8_08.cpp │ │ ├── Ex8_08A.cpp │ │ ├── Ex8_09A.cpp │ │ ├── Ex8_09B.cpp │ │ ├── Ex8_09C.cpp │ │ ├── Ex8_10.cpp │ │ ├── Ex8_11.cpp │ │ ├── Ex8_12.cpp │ │ ├── Ex8_13.cpp │ │ ├── Ex8_14.cpp │ │ ├── Ex8_15.cpp │ │ ├── Ex8_16.cpp │ │ ├── Ex8_17.cpp │ │ └── Ex8_18.cpp │ ├── Chapter 09 │ │ ├── Ex9_01.cpp │ │ ├── Ex9_02.cpp │ │ ├── Ex9_03.cpp │ │ ├── Ex9_04.cpp │ │ └── Ex9_05.cpp │ ├── Chapter 10 │ │ ├── Ex10_01 │ │ │ ├── Ex10_01.cpp │ │ │ └── Power.cpp │ │ ├── Ex10_02 │ │ │ ├── Ex10_02.cpp │ │ │ ├── Power.cpp │ │ │ └── Range.cpp │ │ ├── Ex10_02A │ │ │ ├── Ex10_02A.cpp │ │ │ ├── Power.cpp │ │ │ └── Range.cpp │ │ ├── Ex10_03 │ │ │ ├── Ex10_03.cpp │ │ │ └── Power.cpp │ │ ├── Ex10_04 │ │ │ ├── Ex10_04.cpp │ │ │ ├── Power.cpp │ │ │ └── Power.h │ │ ├── Ex10_05 │ │ │ ├── Ex10_05.cpp │ │ │ ├── Power.cpp │ │ │ └── Power.h │ │ ├── Ex10_06 │ │ │ ├── Constants.h │ │ │ └── Ex10_06.cpp │ │ ├── Ex10_06A │ │ │ ├── Constants.cpp │ │ │ ├── Constants.h │ │ │ └── Ex10_06A.cpp │ │ ├── Ex10_07 │ │ │ ├── Ex10_07.cpp │ │ │ ├── compare.cpp │ │ │ └── compare.h │ │ ├── Ex10_08 │ │ │ ├── Ex10_08.cpp │ │ │ ├── functions.cpp │ │ │ └── functions.h │ │ ├── Ex10_09 │ │ │ └── Ex10_09.cpp │ │ └── Ex10_10 │ │ │ ├── Ex10_10.cpp │ │ │ └── average.h │ ├── Chapter 11 │ │ ├── Ex11_01 │ │ │ └── Ex11_01.cpp │ │ ├── Ex11_01A │ │ │ └── Ex11_01A.cpp │ │ ├── Ex11_01B │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_01B.cpp │ │ ├── Ex11_02 │ │ │ ├── Cube.cpp │ │ │ ├── Cube.h │ │ │ └── Ex11_02.cpp │ │ ├── Ex11_03 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_03.cpp │ │ ├── Ex11_04 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_04.cpp │ │ ├── Ex11_05 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_05.cpp │ │ ├── Ex11_06 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_06.cpp │ │ ├── Ex11_07 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_07.cpp │ │ ├── Ex11_08 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_08.cpp │ │ ├── Ex11_09 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_09.cpp │ │ ├── Ex11_10 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_10.cpp │ │ ├── Ex11_11 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_11.cpp │ │ ├── Ex11_12 │ │ │ ├── CylindricalBox.cpp │ │ │ ├── CylindricalBox.h │ │ │ └── Ex11_12.cpp │ │ ├── Ex11_13 │ │ │ ├── Box.cpp │ │ │ ├── Box.h │ │ │ └── Ex11_13.cpp │ │ ├── Ex11_14 │ │ │ ├── Box.h │ │ │ ├── Ex11_14.cpp │ │ │ ├── Package.h │ │ │ ├── Truckload.cpp │ │ │ └── Truckload.h │ │ └── Ex11_15 │ │ │ ├── Box.h │ │ │ ├── Ex11_15.cpp │ │ │ ├── Truckload.cpp │ │ │ └── Truckload.h │ ├── Chapter 12 │ │ ├── Ex12_01 │ │ │ ├── Box.h │ │ │ └── Ex12_01.cpp │ │ ├── Ex12_02 │ │ │ ├── Box.h │ │ │ └── Ex12_02.cpp │ │ ├── Ex12_03 │ │ │ ├── Box.h │ │ │ └── Ex12_03.cpp │ │ ├── Ex12_03A │ │ │ ├── Box.h │ │ │ └── Ex12_03A.cpp │ │ ├── Ex12_04 │ │ │ ├── Box.h │ │ │ └── Ex12_04.cpp │ │ ├── Ex12_05 │ │ │ ├── Box.h │ │ │ └── Ex12_05.cpp │ │ ├── Ex12_06 │ │ │ ├── Box.h │ │ │ └── Ex12_06.cpp │ │ ├── Ex12_07 │ │ │ ├── Ex12_07.cpp │ │ │ └── Integer.h │ │ ├── Ex12_08 │ │ │ ├── Box.h │ │ │ └── Ex12_08.cpp │ │ ├── Ex12_09 │ │ │ ├── Box.h │ │ │ └── Ex12_09.cpp │ │ ├── Ex12_10 │ │ │ ├── Box.h │ │ │ ├── Ex12_10.cpp │ │ │ ├── Truckload.cpp │ │ │ └── Truckload.h │ │ ├── Ex12_11 │ │ │ ├── Box.h │ │ │ ├── Ex12_11.cpp │ │ │ ├── Truckload.cpp │ │ │ └── Truckload.h │ │ ├── Ex12_12 │ │ │ ├── Ex12_12.cpp │ │ │ └── Message.h │ │ └── Ex12_12A │ │ │ ├── Ex12_12A.cpp │ │ │ └── Message.h │ ├── Chapter 13 │ │ ├── Ex13_01 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ └── Ex13_01.cpp │ │ ├── Ex13_01A │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ └── Ex13_01A.cpp │ │ ├── Ex13_02 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ └── Ex13_02.cpp │ │ ├── Ex13_03 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ └── Ex13_03.cpp │ │ ├── Ex13_04 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ └── Ex13_04.cpp │ │ ├── Ex13_05 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ └── Ex13_05.cpp │ │ ├── Ex13_06 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── CerealPack.h │ │ │ ├── Contents.h │ │ │ └── Ex13_06.cpp │ │ ├── Ex13_06A │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── CerealPack.h │ │ │ ├── Contents.h │ │ │ └── Ex13_06A.cpp │ │ └── Ex13_06B │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── CerealPack.h │ │ │ ├── Contents.h │ │ │ └── Ex13_06B.cpp │ ├── Chapter 14 │ │ ├── Ex14_01 │ │ │ ├── Box.h │ │ │ ├── Ex14_01.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_02 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_02.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_03 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_03.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_04 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_04.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_05 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_05.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_06 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_06.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_07 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_07.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_07A │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_07A.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_08 │ │ │ ├── Box.h │ │ │ ├── Ex14_08.cpp │ │ │ └── ToughPack.h │ │ ├── Ex14_09 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ └── Ex14_09.cpp │ │ ├── Ex14_10 │ │ │ ├── Box.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_10.cpp │ │ │ └── ToughPack.h │ │ └── Ex14_11 │ │ │ ├── Box.h │ │ │ ├── Can.h │ │ │ ├── Carton.h │ │ │ ├── Ex14_11.cpp │ │ │ ├── ToughPack.h │ │ │ └── Vessel.h │ ├── Chapter 15 │ │ ├── Ex15_01 │ │ │ └── Ex15_01.cpp │ │ ├── Ex15_02 │ │ │ └── Ex15_02.cpp │ │ ├── Ex15_03 │ │ │ ├── Ex15_03.cpp │ │ │ └── MyTroubles.h │ │ ├── Ex15_04 │ │ │ ├── Ex15_04.cpp │ │ │ └── MyTroubles.h │ │ ├── Ex15_05 │ │ │ ├── Ex15_05.cpp │ │ │ └── MyTroubles.h │ │ ├── Ex15_06 │ │ │ ├── Ex15_06.cpp │ │ │ └── MyTroubles.h │ │ ├── Ex15_07 │ │ │ ├── Ex15_07.cpp │ │ │ └── MyTroubles.h │ │ ├── Ex15_08 │ │ │ ├── Ex15_08.cpp │ │ │ └── MyTroubles.h │ │ ├── Ex15_08A │ │ │ ├── Ex15_08A.cpp │ │ │ └── MyTroubles.h │ │ ├── Ex15_08B │ │ │ ├── DoubleArrayRAII.h │ │ │ ├── Ex15_08B.cpp │ │ │ └── MyTroubles.h │ │ └── Ex15_09 │ │ │ ├── Box.h │ │ │ ├── Dimension_error.h │ │ │ └── Ex15_09.cpp │ ├── Chapter 16 │ │ ├── Ex16_01 │ │ │ ├── Array.h │ │ │ ├── Box.h │ │ │ └── Ex16_01.cpp │ │ ├── Ex16_01A │ │ │ ├── Array.h │ │ │ ├── Box.h │ │ │ └── Ex16_01A.cpp │ │ ├── Ex16_02 │ │ │ ├── Array.h │ │ │ ├── Box.h │ │ │ └── Ex16_02.cpp │ │ ├── Ex16_03 │ │ │ ├── Array.h │ │ │ ├── Box.h │ │ │ └── Ex16_03.cpp │ │ ├── Ex16_04 │ │ │ ├── Ex16_04.cpp │ │ │ └── Stack.h │ │ ├── Ex16_04A │ │ │ ├── Ex16_04A.cpp │ │ │ └── Stack.h │ │ └── Ex16_05 │ │ │ └── Ex16_05.cpp │ ├── Chapter 17 │ │ ├── Ex17_01 │ │ │ ├── Array.h │ │ │ └── Ex17_01.cpp │ │ ├── Ex17_02 │ │ │ ├── Array.h │ │ │ └── Ex17_02.cpp │ │ ├── Ex17_03 │ │ │ ├── Array.h │ │ │ └── Ex17_03.cpp │ │ ├── Ex17_04 │ │ │ ├── Array.h │ │ │ └── Ex17_04.cpp │ │ ├── Ex17_05 │ │ │ ├── Array.h │ │ │ └── Ex17_05.cpp │ │ ├── Ex17_06 │ │ │ ├── Array.h │ │ │ └── Ex17_06.cpp │ │ ├── Ex17_07 │ │ │ ├── Array.h │ │ │ └── Ex17_07.cpp │ │ └── Ex17_08 │ │ │ ├── Array.h │ │ │ └── Ex17_08.cpp │ ├── Chapter 18 │ │ ├── Ex18_01 │ │ │ └── Ex18_01.cpp │ │ ├── Ex18_02 │ │ │ ├── Ex18_02.cpp │ │ │ └── Optimum.h │ │ ├── Ex18_03 │ │ │ ├── Ex18_03.cpp │ │ │ ├── Less.cpp │ │ │ ├── Less.h │ │ │ └── Optimum.h │ │ ├── Ex18_03A │ │ │ ├── Ex18_03A.cpp │ │ │ └── Optimum.h │ │ ├── Ex18_04 │ │ │ ├── Ex18_04.cpp │ │ │ ├── Nearer.h │ │ │ └── Optimum.h │ │ ├── Ex18_05 │ │ │ ├── Ex18_05.cpp │ │ │ └── Optimum.h │ │ ├── Ex18_06 │ │ │ ├── Ex18_06.cpp │ │ │ └── Optimum.h │ │ ├── Ex18_07 │ │ │ ├── Ex18_07.cpp │ │ │ ├── Finder.cpp │ │ │ ├── Finder.h │ │ │ └── Optimum.h │ │ └── Ex18_08 │ │ │ └── Ex18_08.cpp │ └── Chapter 19 │ │ ├── Ex19_01 │ │ └── Ex19_01.cpp │ │ ├── Ex19_02 │ │ └── Ex19_02.cpp │ │ ├── Ex19_03 │ │ └── Ex19_03.cpp │ │ ├── Ex19_04 │ │ └── Ex19_04.cpp │ │ ├── Ex19_05 │ │ └── Ex19_05.cpp │ │ ├── Ex19_06 │ │ └── Ex19_06.cpp │ │ ├── Ex19_07 │ │ └── Ex19_07.cpp │ │ ├── Ex19_08 │ │ ├── Box.cpp │ │ ├── Box.h │ │ └── Ex19_08.cpp │ │ ├── Ex19_09 │ │ └── Ex19_09.cpp │ │ ├── Ex19_10 │ │ └── Ex19_10.cpp │ │ ├── Ex19_11 │ │ └── Ex19_11.cpp │ │ ├── Ex19_12 │ │ ├── Box.h │ │ └── Ex19_12.cpp │ │ ├── Ex19_13 │ │ └── Ex19_13.cpp │ │ ├── Ex19_13A │ │ └── Ex19_13A.cpp │ │ ├── Ex19_14 │ │ └── Ex19_14.cpp │ │ └── Ex19_15 │ │ └── Ex19_15.cpp └── Exercises │ ├── Chapter 01 │ ├── Exer1_03.cpp │ ├── Soln1_01.cpp │ └── Soln1_02.cpp │ ├── Chapter 02 │ ├── Soln2_01.cpp │ ├── Soln2_02.cpp │ ├── Soln2_03.cpp │ ├── Soln2_04.cpp │ ├── Soln2_05.cpp │ └── Soln2_06.cpp │ ├── Chapter 03 │ ├── Soln3_01.cpp │ ├── Soln3_02.cpp │ ├── Soln3_03.cpp │ ├── Soln3_04.cpp │ ├── Soln3_05.cpp │ └── Soln3_06.cpp │ ├── Chapter 04 │ ├── Soln4_01.cpp │ ├── Soln4_02.cpp │ ├── Soln4_03.cpp │ ├── Soln4_04.cpp │ ├── Soln4_05.cpp │ ├── Soln4_06.cpp │ ├── Soln4_07.cpp │ └── Soln4_08.cpp │ ├── Chapter 05 │ ├── Soln5_01.cpp │ ├── Soln5_02.cpp │ ├── Soln5_03.cpp │ ├── Soln5_04.cpp │ ├── Soln5_05.cpp │ ├── Soln5_06.cpp │ ├── Soln5_07.cpp │ └── Soln5_08.cpp │ ├── Chapter 06 │ ├── Soln6_01.cpp │ ├── Soln6_02.cpp │ ├── Soln6_03.cpp │ ├── Soln6_04.cpp │ ├── Soln6_05.cpp │ └── Soln6_06.cpp │ ├── Chapter 07 │ ├── Soln7_01.cpp │ ├── Soln7_02.cpp │ ├── Soln7_03.cpp │ ├── Soln7_04.cpp │ ├── Soln7_05.cpp │ ├── Soln7_06.cpp │ ├── Soln7_07.cpp │ └── Soln7_08.cpp │ ├── Chapter 08 │ ├── Soln8_01.cpp │ ├── Soln8_02.cpp │ ├── Soln8_03.cpp │ ├── Soln8_04.cpp │ ├── Soln8_05.cpp │ ├── Soln8_06.cpp │ ├── Soln8_07.cpp │ ├── Soln8_07A.cpp │ ├── Soln8_08.cpp │ └── Soln8_09.cpp │ ├── Chapter 09 │ ├── Soln9_01.cpp │ ├── Soln9_02.cpp │ ├── Soln9_03.cpp │ ├── Soln9_04.cpp │ ├── Soln9_05.cpp │ └── Soln9_06.cpp │ ├── Chapter 10 │ ├── Soln10_01 │ │ ├── main.cpp │ │ ├── print.cpp │ │ ├── print.h │ │ ├── printthat.cpp │ │ ├── printthat.h │ │ ├── printthis.cpp │ │ └── printthis.h │ ├── Soln10_02 │ │ ├── main.cpp │ │ ├── print.cpp │ │ ├── print.h │ │ ├── printthat.cpp │ │ ├── printthat.h │ │ ├── printthis.cpp │ │ └── printthis.h │ ├── Soln10_03 │ │ ├── main.cpp │ │ ├── print.cpp │ │ ├── print.h │ │ ├── printthat.cpp │ │ ├── printthat.h │ │ ├── printthis.cpp │ │ └── printthis.h │ └── Soln10_04 │ │ ├── main.cpp │ │ ├── print.cpp │ │ ├── print.h │ │ ├── printthat.cpp │ │ ├── printthat.h │ │ ├── printthis.cpp │ │ └── printthis.h │ ├── Chapter 11 │ ├── Soln11_01 │ │ ├── Integer.cpp │ │ ├── Integer.h │ │ └── Soln11_01.cpp │ ├── Soln11_02 │ │ ├── Integer.cpp │ │ ├── Integer.h │ │ └── Soln11_02.cpp │ ├── Soln11_03 │ │ ├── Integer.cpp │ │ ├── Integer.h │ │ └── Soln11_03.cpp │ ├── Soln11_04 │ │ ├── Integer.cpp │ │ ├── Integer.h │ │ └── Soln11_04.cpp │ ├── Soln11_05 │ │ ├── Integer.cpp │ │ ├── Integer.h │ │ └── Soln11_05.cpp │ ├── Soln11_06 │ │ ├── Box.h │ │ ├── Soln11_06.cpp │ │ ├── Truckload.cpp │ │ └── Truckload.h │ ├── Soln11_07 │ │ ├── Box.h │ │ ├── Soln11_07.cpp │ │ ├── Truckload.cpp │ │ └── Truckload.h │ └── Soln11_08 │ │ ├── Box.h │ │ ├── Soln11_08.cpp │ │ ├── Truckload.cpp │ │ └── Truckload.h │ ├── Chapter 12 │ ├── Soln12_01 │ │ ├── Box.h │ │ └── Soln12_01.cpp │ ├── Soln12_02 │ │ ├── Box.h │ │ └── Soln12_02.cpp │ ├── Soln12_03 │ │ ├── Box.h │ │ └── Soln12_03.cpp │ ├── Soln12_04 │ │ ├── Box.h │ │ └── Soln12_04.cpp │ ├── Soln12_05 │ │ ├── Rational.h │ │ └── Soln12_05.cpp │ └── Soln12_06 │ │ ├── Box.h │ │ ├── Soln12_06.cpp │ │ ├── Truckload.cpp │ │ └── Truckload.h │ ├── Chapter 13 │ ├── Soln13_01 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ └── Soln13_01.cpp │ ├── Soln13_02 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ └── Soln13_02.cpp │ ├── Soln13_03 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ └── Soln13_03.cpp │ └── Soln13_04 │ │ ├── Person.cpp │ │ ├── Person.h │ │ └── Soln13_04.cpp │ ├── Chapter 14 │ ├── Soln14_01 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ ├── Soln14_01.cpp │ │ ├── Zoo.cpp │ │ └── Zoo.h │ ├── Soln14_02 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ ├── Soln14_02.cpp │ │ ├── Zoo.cpp │ │ └── Zoo.h │ ├── Soln14_03 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ ├── Soln14_03.cpp │ │ ├── Zoo.cpp │ │ └── Zoo.h │ ├── Soln14_04 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ ├── Soln14_04.cpp │ │ ├── Zoo.cpp │ │ └── Zoo.h │ ├── Soln14_05 │ │ ├── Animals.cpp │ │ ├── Animals.h │ │ ├── Soln14_05.cpp │ │ ├── Zoo.cpp │ │ └── Zoo.h │ └── Soln14_06 │ │ ├── Point.h │ │ ├── Shapes.h │ │ └── Soln14_06.cpp │ ├── Chapter 15 │ ├── Exer15_05 │ │ ├── Customer.cpp │ │ ├── Customer.h │ │ ├── DB.cpp │ │ ├── DB.h │ │ ├── DBException.h │ │ └── Exer15_05.cpp │ ├── Soln15_01 │ │ ├── CurveBall.cpp │ │ ├── CurveBall.h │ │ └── Soln15_01.cpp │ ├── Soln15_02 │ │ ├── CurveBall.cpp │ │ ├── CurveBall.h │ │ ├── Soln15_02.cpp │ │ ├── TooManyExceptions.cpp │ │ └── TooManyExceptions.h │ ├── Soln15_03 │ │ ├── Box.h │ │ ├── Soln15_03.cpp │ │ ├── Truckload.cpp │ │ └── Truckload.h │ ├── Soln15_04 │ │ ├── CurveBall.cpp │ │ ├── CurveBall.h │ │ ├── DomainExceptions.h │ │ └── Soln15_04.cpp │ └── Soln15_05 │ │ ├── Customer.cpp │ │ ├── Customer.h │ │ ├── DB.cpp │ │ ├── DB.h │ │ ├── DBException.h │ │ ├── DB_RAII.h │ │ └── Soln15_05.cpp │ ├── Chapter 16 │ ├── Soln16_01 │ │ ├── Array.h │ │ └── Soln16_01.cpp │ ├── Soln16_02 │ │ ├── Pair.h │ │ └── Soln16_02.cpp │ ├── Soln16_03 │ │ ├── Pair.h │ │ └── Soln16_03.cpp │ ├── Soln16_04 │ │ ├── Pair.h │ │ ├── Soln16_04.cpp │ │ └── SparseArray.h │ ├── Soln16_05 │ │ ├── LinkedList.h │ │ └── Soln16_05.cpp │ └── Soln16_06 │ │ ├── LinkedList.h │ │ ├── Pair.h │ │ ├── Soln16_06.cpp │ │ └── SparseArray.h │ ├── Chapter 17 │ ├── Soln17_01 │ │ ├── Box.h │ │ ├── Soln17_01.cpp │ │ ├── Truckload.cpp │ │ └── Truckload.h │ ├── Soln17_02 │ │ ├── LinkedList.h │ │ └── Soln17_02.cpp │ ├── Soln17_03 │ │ ├── Customer.cpp │ │ ├── Customer.h │ │ ├── DB.cpp │ │ ├── DB.h │ │ ├── DBException.h │ │ ├── DB_RAII.h │ │ └── Soln17_03.cpp │ └── Soln17_04 │ │ ├── Customer.cpp │ │ ├── Customer.h │ │ ├── DB.cpp │ │ ├── DB.h │ │ ├── DBException.h │ │ ├── DB_RAII.h │ │ └── Soln17_04.cpp │ ├── Chapter 18 │ ├── Soln18_01 │ │ └── Soln18_01.cpp │ ├── Soln18_02 │ │ ├── Soln18_02.cpp │ │ └── Sort.h │ ├── Soln18_03 │ │ ├── Soln18_03.cpp │ │ └── Sort.h │ ├── Soln18_04 │ │ ├── Collect.h │ │ └── Soln18_04.cpp │ └── Soln18_05 │ │ ├── Box.h │ │ ├── DeliveryTruck.cpp │ │ ├── DeliveryTruck.h │ │ ├── Soln18_05.cpp │ │ ├── Truckload.cpp │ │ └── Truckload.h │ └── Chapter 19 │ ├── Soln19_01 │ ├── Box.h │ ├── Soln19_01.cpp │ ├── Truckload.cpp │ └── Truckload.h │ ├── Soln19_02 │ └── Soln19_02.cpp │ ├── Soln19_03 │ └── Soln19_03.cpp │ ├── Soln19_03A │ └── Soln19_03A.cpp │ ├── Soln19_04 │ └── Soln19_04.cpp │ ├── Soln19_05 │ └── Soln19_05.cpp │ ├── Soln19_06 │ └── Soln19_06.cpp │ ├── Soln19_07 │ └── Soln19_07.cpp │ └── Soln19_08 │ └── Soln19_08.cpp ├── LICENSE.txt ├── README.md └── contributing.md /Beginning C++ 17 source code/Examples/Chapter 01/Ex1_01.cpp: -------------------------------------------------------------------------------- 1 | // A complete C++ program 2 | #include 3 | 4 | int main() 5 | { 6 | int answer {42}; // Defines answer with value 42 7 | 8 | std::cout << "The answer to life, the universe, and everything is " 9 | << answer 10 | << std::endl; 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 01/Ex1_02.cpp: -------------------------------------------------------------------------------- 1 | // Using escape sequences 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << "\"Least \'said\' \\\n\t\tsoonest \'mended\'.\"" << std::endl; 7 | } 8 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 02/Ex2_01.cpp: -------------------------------------------------------------------------------- 1 | // Writing values of variables to cout 2 | #include 3 | 4 | int main() 5 | { 6 | int apple_count {15}; // Number of apples 7 | int orange_count {5}; // Number of oranges 8 | int total_fruit {apple_count + orange_count}; // Total number of fruit 9 | 10 | std::cout << "The value of apple_count is " << apple_count << std::endl; 11 | std::cout << "The value of orange_count is " << orange_count << std::endl; 12 | std::cout << "The value of total_fruit is " << total_fruit << std::endl; 13 | } 14 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_01.cpp: -------------------------------------------------------------------------------- 1 | // Comparing data values 2 | #include 3 | 4 | int main() 5 | { 6 | char first {}; // Stores the first character 7 | char second {}; // Stores the second character 8 | 9 | std::cout << "Enter a character: "; 10 | std::cin >> first; 11 | 12 | std::cout << "Enter a second character: "; 13 | std::cin >> second; 14 | 15 | std::cout << "The value of the expression " << first << '<' << second 16 | << " is: " << (first < second) << std::endl; 17 | std::cout << "The value of the expression " << first << "==" << second 18 | << " is: " << (first == second) << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_02.cpp: -------------------------------------------------------------------------------- 1 | // Using an if statement 2 | #include 3 | 4 | int main() 5 | { 6 | std::cout << "Enter an integer between 50 and 100: "; 7 | 8 | int value {}; 9 | std::cin >> value; 10 | 11 | if (value) 12 | std::cout << "You have entered a value that is different from zero." << std::endl; 13 | 14 | if (value < 50) 15 | std::cout << "The value is invalid - it is less than 50." << std::endl; 16 | 17 | if (value > 100) 18 | std::cout << "The value is invalid - it is greater than 100." << std::endl; 19 | 20 | std::cout << "You entered " << value << std::endl; 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_03.cpp: -------------------------------------------------------------------------------- 1 | // Using a nested if 2 | #include 3 | 4 | int main() 5 | { 6 | char letter {}; // Store input here 7 | std::cout << "Enter a letter: "; // Prompt for the input 8 | std::cin >> letter; 9 | 10 | if (letter >= 'A') 11 | { // letter is 'A' or larger 12 | if (letter <= 'Z') 13 | { // letter is 'Z' or smaller 14 | std::cout << "You entered an uppercase letter." << std::endl; 15 | return 0; 16 | } 17 | } 18 | 19 | if (letter >= 'a') // Test for 'a' or larger 20 | if (letter <= 'z') 21 | { // letter is >= 'a' and <= 'z' 22 | std::cout << "You entered a lowercase letter." << std::endl; 23 | return 0; 24 | } 25 | std::cout << "You did not enter a letter." << std::endl; 26 | } 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_03A.cpp: -------------------------------------------------------------------------------- 1 | // Using Standard character classification functions instead of nested ifs 2 | #include 3 | #include // for std::isupper() and islower() 4 | 5 | int main() 6 | { 7 | char letter {}; // Store input here 8 | std::cout << "Enter a letter: "; // Prompt for the input 9 | std::cin >> letter; 10 | 11 | if (std::isupper(letter)) 12 | { 13 | std::cout << "You entered an uppercase letter." << std::endl; 14 | return 0; 15 | } 16 | 17 | if (std::islower(letter)) 18 | { 19 | std::cout << "You entered a lowercase letter." << std::endl; 20 | return 0; 21 | } 22 | std::cout << "You did not enter a letter." << std::endl; 23 | } 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_04.cpp: -------------------------------------------------------------------------------- 1 | // Using the if-else statement 2 | #include 3 | 4 | int main() 5 | { 6 | long number {}; // Stores input 7 | std::cout << "Enter an integer less than 2 billion: "; 8 | std::cin >> number; 9 | 10 | if (number % 2) // Test remainder after division by 2 11 | { // Here if remainder is 1 12 | std::cout << "Your number is odd." << std::endl; 13 | } 14 | else 15 | { // Here if remainder is 0 16 | std::cout << "\nYour number is even." << std::endl; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_06.cpp: -------------------------------------------------------------------------------- 1 | // Using the conditional operator to select output. 2 | #include 3 | 4 | int main() 5 | { 6 | int mice {}; // Count of all mice 7 | int brown {}; // Count of brown mice 8 | int white {}; // Count of white mice 9 | 10 | std::cout << "How many brown mice do you have? "; 11 | std::cin >> brown; 12 | std::cout << "How many white mice do you have? "; 13 | std::cin >> white; 14 | 15 | mice = brown + white; 16 | 17 | std::cout << "You have " << mice 18 | << (mice == 1? " mouse" : " mice") 19 | << " in total." << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_08.cpp: -------------------------------------------------------------------------------- 1 | // Multiple case actions 2 | #include 3 | #include // for std::isalpha() and tolower() 4 | 5 | int main() 6 | { 7 | char letter {}; 8 | std::cout << "Enter a letter: "; 9 | std::cin >> letter; 10 | 11 | if (std::isalpha(letter)) 12 | { 13 | switch (std::tolower(letter)) 14 | { 15 | case 'a': case 'e': case 'i': case 'o': case 'u': 16 | std::cout << "You entered a vowel." << std::endl; 17 | break; 18 | default: 19 | std::cout << "You entered a consonant." << std::endl; 20 | break; 21 | } 22 | } 23 | else 24 | { 25 | std::cout << "You did not enter a letter." << std::endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 04/Ex4_08A.cpp: -------------------------------------------------------------------------------- 1 | // Using return to exit a stitch statement (and the program) 2 | // (This is just to show it is possible: don't code like this!) 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | char letter {}; 9 | std::cout << "Enter a letter: "; 10 | std::cin >> letter; 11 | 12 | if (std::isalpha(letter)) 13 | { 14 | switch (std::tolower(letter)) 15 | { 16 | case 'a': case 'e': case 'i': case 'o': case 'u': 17 | std::cout << "You entered a vowel." << std::endl; 18 | return 0; 19 | } 20 | 21 | std::cout << "You entered a consonant." << std::endl; 22 | } 23 | else 24 | { 25 | std::cout << "You did not enter a letter." << std::endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 05/Ex5_01.cpp: -------------------------------------------------------------------------------- 1 | // Using a for loop with an array 2 | #include 3 | 4 | int main() 5 | { 6 | const unsigned int size {6}; // Array size 7 | unsigned int height[size] {26, 37, 47, 55, 62, 75}; // An array of heights 8 | unsigned int total{}; // Sum of heights 9 | 10 | for (size_t i{}; i 3 | #include // for std::size() 4 | 5 | int main() 6 | { 7 | int values[] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; 8 | std::cout << "There are " << sizeof (values) / sizeof(values[0]) 9 | << " elements in the array." << std::endl; 10 | 11 | int sum{}; 12 | for (size_t i{}; i < std::size(values); ++i) 13 | { 14 | sum += values[i]; 15 | } 16 | std::cout << "The sum of the array elements is " << sum << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 05/Ex5_03.cpp: -------------------------------------------------------------------------------- 1 | // Floating-point control in a for loop 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | const double pi{ 3.14159265 }; // The famous pi 8 | const size_t perline{ 3 }; // Outputs per line 9 | size_t linecount{}; // Count of output lines 10 | for (double radius{ 0.2 }; radius <= 3.0; radius += 0.2) 11 | { 12 | std::cout << std::fixed << std::setprecision(2) << " radius =" << std::setw(5) 13 | << radius << " area =" << std::setw(6) << pi * radius * radius; 14 | if (perline == ++linecount) // When perline outputs have been written... 15 | { 16 | std::cout << std::endl; // ...start a new line... 17 | linecount = 0; // ...and reset the line counter 18 | } 19 | } 20 | std::cout << std::endl; 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 05/Ex5_03A.cpp: -------------------------------------------------------------------------------- 1 | // Floating-point control in a for loop (corrected) 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | const double pi{ 3.14159265 }; // The famous pi 8 | const size_t perline{ 3 }; // Outputs per line 9 | size_t linecount{}; // Count of output lines 10 | for (double radius{ 0.2 }; radius < 3.0 + .001; radius += 0.2) 11 | { 12 | std::cout << std::fixed << std::setprecision(2) << " radius =" << std::setw(5) 13 | << radius << " area =" << std::setw(6) << pi * radius * radius; 14 | if (perline == ++linecount) // When perline outputs have been written... 15 | { 16 | std::cout << std::endl; // ...start a new line... 17 | linecount = 0; // ...and reset the line counter 18 | } 19 | } 20 | std::cout << std::endl; 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 05/Ex5_04.cpp: -------------------------------------------------------------------------------- 1 | // Multiple initializations in a loop expression 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | unsigned int limit{}; 8 | std::cout << "This program calculates n! and the sum of the integers up to n for values 1 to limit.\n"; 9 | std::cout << "What upper limit for n would you like? "; 10 | std::cin >> limit; 11 | 12 | // Output column headings 13 | std::cout << std::setw(8) << "integer" << std::setw(8) << " sum" 14 | << std::setw(20) << " factorial" << std::endl; 15 | 16 | for (unsigned long long n{1}, sum{}, factorial{1}; n <= limit; ++n) 17 | { 18 | sum += n; // Accumulate sum to current n 19 | factorial *= n; // Calculate n! for current n 20 | std::cout << std::setw(8) << n << std::setw(8) << sum 21 | << std::setw(20) << factorial << std::endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 05/Ex5_04A.cpp: -------------------------------------------------------------------------------- 1 | // Multiple calculations in a loop expression's third control expression 2 | // by using the comma operator 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | unsigned int limit{}; 9 | std::cout << "This program calculates n! and the sum of the integers up to n for values 1 to limit.\n"; 10 | std::cout << "What upper limit for n would you like? "; 11 | std::cin >> limit; 12 | 13 | // Output column headings 14 | std::cout << std::setw(8) << "integer" << std::setw(8) << " sum" 15 | << std::setw(20) << " factorial" << std::endl; 16 | 17 | for (unsigned long long n {1}, sum {1}, factorial {1}; n <= limit; 18 | ++n, sum += n, factorial *= n) 19 | { 20 | std::cout << std::setw(8) << n << std::setw(8) << sum 21 | << std::setw(20) << factorial << std::endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 06/Ex6_01.cpp: -------------------------------------------------------------------------------- 1 | // The size of pointers 2 | #include 3 | 4 | int main() 5 | { 6 | // Print out the size (in number of bytes) of some arbitrary data types 7 | // and the corresponding pointer types: 8 | std::cout << sizeof(double) << " > " << sizeof(char) << std::endl; 9 | std::cout << sizeof(double*) << " == " << sizeof(char*) << std::endl; 10 | } 11 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 06/Ex6_04.cpp: -------------------------------------------------------------------------------- 1 | // Using an array of pointers 2 | #include 3 | #include // for std::size() 4 | 5 | int main() 6 | { 7 | const char* pstars[] { 8 | "Fatty Arbuckle", "Clara Bow", 9 | "Lassie", "Slim Pickens", 10 | "Boris Karloff", "Mae West", 11 | "Oliver Hardy", "Greta Garbo" 12 | }; 13 | 14 | std::cout << "Pick a lucky star! Enter a number between 1 and " 15 | << std::size(pstars) << ": "; 16 | size_t choice{}; 17 | std::cin >> choice; 18 | 19 | if (choice >= 1 && choice <= std::size(pstars)) 20 | { 21 | std::cout << "Your lucky star is " << pstars[choice - 1] << std::endl; 22 | } 23 | else 24 | { 25 | std::cout << "Sorry, you haven't got a lucky star." << std::endl; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 07/Ex7_04.cpp: -------------------------------------------------------------------------------- 1 | // Searching within strings 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::string sentence {"Manners maketh man"}; 8 | std::string word {"man"}; 9 | std::cout << sentence.find(word) << std::endl; // Outputs 15 10 | std::cout << sentence.find("Ma") << std::endl; // Outputs 0 11 | std::cout << sentence.find('k') << std::endl; // Outputs 10 12 | std::cout << sentence.find('x') << std::endl; // Outputs std::string::npos 13 | } 14 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_01.cpp: -------------------------------------------------------------------------------- 1 | // Calculating powers 2 | #include 3 | #include 4 | 5 | // Function to calculate x to the power n 6 | double power(double x, int n) 7 | { 8 | double result{ 1.0 }; 9 | if (n >= 0) 10 | { 11 | for (int i {1}; i <= n; ++i) 12 | result *= x; 13 | } 14 | else // n < 0 15 | { 16 | for (int i {1}; i <= -n; ++i) 17 | result /= x; 18 | } 19 | return result; 20 | } 21 | 22 | int main() 23 | { 24 | // Calculate powers of 8 from -3 to +3 25 | for (int i{ -3 }; i <= 3; ++i) 26 | std::cout << std::setw(10) << power(8.0, i); 27 | 28 | std::cout << std::endl; 29 | } 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_02.cpp: -------------------------------------------------------------------------------- 1 | // Calculating powers - rearranged 2 | #include 3 | #include 4 | //double power(double x, int n); // Function prototype - uncomment for successful compilation 5 | 6 | int main() 7 | { 8 | // Calculate powers of 8 from -3 to +3 9 | for (int i{ -3 }; i <= 3; ++i) 10 | std::cout << std::setw(10) << power(8.0, i); 11 | 12 | std::cout << std::endl; 13 | } 14 | 15 | // Function to calculate x to the power n 16 | double power(double x, int n) 17 | { 18 | double result{ 1.0 }; 19 | if (n >= 0) 20 | { 21 | for (int i {1}; i <= n; ++i) 22 | result *= x; 23 | } 24 | else // n < 0 25 | { 26 | for (int i {1}; i <= -n; ++i) 27 | result /= x; 28 | } 29 | return result; 30 | } 31 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_03.cpp: -------------------------------------------------------------------------------- 1 | // Failing to modify the original value of a function argument 2 | #include 3 | 4 | double changeIt(double value_to_be_changed); // Function prototype 5 | 6 | int main() 7 | { 8 | double it{ 5.0 }; 9 | double result {changeIt(it)}; 10 | 11 | std::cout << "After function execution, it = " << it 12 | << "\nResult returned is " << result << std::endl; 13 | } 14 | 15 | // Function that attempts to modify an argument and return it 16 | double changeIt(double it) 17 | { 18 | it += 10.0; // This modifies the copy 19 | std::cout << "Within function, it = " << it << std::endl; 20 | return it; 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_04.cpp: -------------------------------------------------------------------------------- 1 | // Modifying the value of a caller variable 2 | #include 3 | 4 | double changeIt(double* pointer_to_it); // Function prototype 5 | 6 | int main() 7 | { 8 | double it{ 5.0 }; 9 | double result{ changeIt(&it) }; // Now we pass the address 10 | 11 | std::cout << "After function execution, it = " << it 12 | << "\nResult returned is " << result << std::endl; 13 | } 14 | 15 | // Function to modify an argument and return it 16 | double changeIt(double* pit) 17 | { 18 | *pit += 10.0; // This modifies the original it 19 | std::cout << "Within function, *pit = " << *pit << std::endl; 20 | return *pit; 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_05.cpp: -------------------------------------------------------------------------------- 1 | // Passing an array to a function 2 | #include 3 | #include // For std::size() 4 | 5 | double average(double array[], size_t count); // Function prototype 6 | 7 | int main() 8 | { 9 | double values[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; 10 | std::cout << "Average = " << average(values, std::size(values)) << std::endl; 11 | } 12 | 13 | // Function to compute an average 14 | double average(double array[], size_t count) 15 | { 16 | double sum{}; // Accumulate total in here 17 | for (size_t i{} ; i < count ; ++i) 18 | sum += array[i]; // Sum array elements 19 | return sum / count; // Return average 20 | } 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_05A.cpp: -------------------------------------------------------------------------------- 1 | // Passing an array to a function - false expectations 2 | // Note: with main() as defined in this file, this program will probably crash! 3 | #include 4 | 5 | double average10(double array[10]); // Function prototype 6 | 7 | int main() 8 | { 9 | // double values[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; 10 | double values[] { 1.0, 2.0, 3.0 }; // Only three values!!! 11 | std::cout << "Average = " << average10(values) << std::endl; 12 | } 13 | 14 | // Function to compute an average 15 | double average10(double array[10]) /* The [10] does not mean what you might expect! */ 16 | { 17 | double sum{}; // Accumulate total in here 18 | for (size_t i{} ; i < 10; ++i) 19 | sum += array[i]; // Sum array elements 20 | return sum / 10; // Return average 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_06.cpp: -------------------------------------------------------------------------------- 1 | // Passing a two-dimensional array to a function 2 | #include 3 | #include // For std::size() 4 | 5 | double yield(const double values[][4], size_t n); 6 | 7 | int main() 8 | { 9 | double beans[3][4] { 10 | { 1.0, 2.0, 3.0, 4.0}, 11 | { 5.0, 6.0, 7.0, 8.0 }, 12 | { 9.0, 10.0, 11.0, 12.0 } 13 | }; 14 | 15 | std::cout << "Yield = " << yield(beans, std::size(beans)) << std::endl; 16 | } 17 | 18 | // Function to compute total yield 19 | double yield(const double array[][4], size_t size) 20 | { 21 | double sum{}; 22 | for (size_t i{}; i < size; ++i) // Loop through rows 23 | { 24 | for (size_t j{}; j < 4; ++j) // Loop through elements in a row 25 | { 26 | sum += array[i][j]; 27 | } 28 | } 29 | return sum; 30 | } 31 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_06A.cpp: -------------------------------------------------------------------------------- 1 | // Passing a two-dimensional array to a function - magic number replaced 2 | #include 3 | #include // For std::size() 4 | 5 | double yield(const double values[][4], size_t n); 6 | 7 | int main() 8 | { 9 | double beans[3][4] { 10 | { 1.0, 2.0, 3.0, 4.0}, 11 | { 5.0, 6.0, 7.0, 8.0 }, 12 | { 9.0, 10.0, 11.0, 12.0 } 13 | }; 14 | 15 | std::cout << "Yield = " << yield(beans, std::size(beans)) << std::endl; 16 | } 17 | 18 | // Function to compute total yield 19 | double yield(const double array[][4], size_t size) 20 | { 21 | double sum{}; 22 | for (size_t i{}; i < size; ++i) // Loop through rows 23 | { 24 | for (double val : array[i]) // Loop through elements in a row 25 | { 26 | sum += val; 27 | } 28 | } 29 | return sum; 30 | } 31 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_07.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beg-cplusplus17/b86344273b1c4c51a597995b979a984e92dab022/Beginning C++ 17 source code/Examples/Chapter 08/Ex8_07.cpp -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_09B.cpp: -------------------------------------------------------------------------------- 1 | // Passing an array to a function - pass by reference improved 2 | #include 3 | #include // for std::size() 4 | 5 | double average10(const double (&)[10]); // Function prototype 6 | 7 | int main() 8 | { 9 | double values[] { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; 10 | // double values[] { 1.0, 2.0, 3.0 }; // Only three values!!! 11 | std::cout << "Average = " << average10(values) << std::endl; 12 | } 13 | 14 | // Function to compute an average 15 | double average10(const double (&array)[10]) 16 | { 17 | double sum {}; // Accumulate total in here 18 | for (double val : array) 19 | sum += val; // Sum array elements 20 | return sum / std::size(array); // Return average 21 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_09C.cpp: -------------------------------------------------------------------------------- 1 | // Passing an array to a function - use std::array<> 2 | #include 3 | #include 4 | 5 | double average10(const std::array& array); // Function prototype 6 | 7 | int main() 8 | { 9 | std::array values{ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 }; 10 | // std::array values{ 1.0, 2.0, 3.0 }; // Only three values!!! 11 | std::cout << "Average = " << average10(values) << std::endl; 12 | } 13 | 14 | // Function to compute an average 15 | double average10(const std::array& array) 16 | { 17 | double sum {}; // Accumulate total in here 18 | for (double val : array) 19 | sum += val; // Sum array elements 20 | return sum / array.size(); // Return average 21 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_10.cpp: -------------------------------------------------------------------------------- 1 | // Implicit conversions of reference parameters 2 | #include 3 | 4 | void double_it(double& it) { it *= 2; } 5 | void print_it(const double& it) { std::cout << it << std::endl; } 6 | 7 | int main() 8 | { 9 | double d{123}; 10 | double_it(d); 11 | print_it(d); 12 | 13 | int i{456}; 14 | // double_it(i); /* error, does not compile! */ 15 | print_it(i); 16 | } 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_12.cpp: -------------------------------------------------------------------------------- 1 | // Program that lists its command line arguments 2 | #include 3 | 4 | int main(int argc, char* argv[]) 5 | { 6 | for (int i{}; i < argc; ++i) 7 | std::cout << argv[i] << std::endl; 8 | } 9 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_14.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beg-cplusplus17/b86344273b1c4c51a597995b979a984e92dab022/Beginning C++ 17 source code/Examples/Chapter 08/Ex8_14.cpp -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 08/Ex8_17.cpp: -------------------------------------------------------------------------------- 1 | // Recursive version of function for x to the power n, n positive or negative 2 | #include 3 | #include 4 | 5 | double power(double x, int n); 6 | 7 | int main() 8 | { 9 | for (int i {-3}; i <= 3; ++i) // Calculate powers of 8 from -3 to +3 10 | std::cout << std::setw(10) << power(8.0, i); 11 | 12 | std::cout << std::endl; 13 | } 14 | 15 | // Recursive function to calculate x to the power n 16 | double power(double x, int n) 17 | { 18 | if (n == 0) return 1.0; 19 | else if (n > 0) return x * power(x, n - 1); 20 | else /* n < 0 */ return 1.0 / power(x, -n); 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 09/Ex9_03.cpp: -------------------------------------------------------------------------------- 1 | // Using return type deduction with templates 2 | #include 3 | #include 4 | 5 | // Template for functions to return the larger of two values 6 | // Supports implicit converion of differently-typed arguments 7 | template 8 | auto larger(T1 a, T2 b) 9 | { 10 | return a > b ? a : b; 11 | } 12 | 13 | int main() 14 | { 15 | int small_int {10}; 16 | std::cout << "Larger of " << small_int << " and 9.6 is " 17 | << larger(small_int, 9.6) << std::endl; 18 | 19 | std::string a_string {"A"}; 20 | std::cout << "Larger of \"" << a_string << "\" and \"Z\" is " 21 | << '"' << larger(a_string, "Z") << '"' << std::endl; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_01/Ex10_01.cpp: -------------------------------------------------------------------------------- 1 | // Calling external functions 2 | #include 3 | #include 4 | 5 | double power(double x, int n); // Declaration of an external power() function 6 | 7 | int main() 8 | { 9 | for (int i {-3}; i <= 3; ++i) // Calculate powers of 8 from -3 to +3 10 | std::cout << std::setw(10) << power(8.0, i); 11 | 12 | std::cout << std::endl; 13 | } 14 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_01/Power.cpp: -------------------------------------------------------------------------------- 1 | // The power function called from Ex10_01.cpp is defined in a different translation unit 2 | 3 | // Recursive function to calculate x to the power n 4 | double power(double x, int n) 5 | { 6 | if (n == 0) return 1.0; 7 | else if (n > 0) return x * power(x, n - 1); 8 | else /* n < 0 */ return 1.0 / power(x, -n); 9 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_02/Ex10_02.cpp: -------------------------------------------------------------------------------- 1 | // Using an externally defined variable 2 | // Add extern or comment/uncomment the lines below to fix the linker error... 3 | #include 4 | #include 5 | 6 | double power(double x, int n); // Declaration of an external power() function 7 | int power_range; // Not an unreasonable first attempt, right? 8 | // extern int power_range; // Declaration of an externally defined variable 9 | 10 | int main() 11 | { 12 | for (int i {-power_range}; i <= power_range; ++i) // Calculate powers of 8 13 | std::cout << std::setw(10) << power(8.0, i); 14 | 15 | std::cout << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_02/Power.cpp: -------------------------------------------------------------------------------- 1 | // The power function called from Ex10_01.cpp is defined in a different translation unit 2 | 3 | // Recursive function to calculate x to the power n 4 | double power(double x, int n) 5 | { 6 | if (n == 0) return 1.0; 7 | else if (n > 0) return x * power(x, n - 1); 8 | else /* n < 0 */ return 1.0 / power(x, -n); 9 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_02/Range.cpp: -------------------------------------------------------------------------------- 1 | 2 | int power_range{ 3 }; // A global variable with external linkage 3 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_02A/Ex10_02A.cpp: -------------------------------------------------------------------------------- 1 | // Using an externally defined global constant 2 | #include 3 | #include 4 | 5 | double power(double x, int n); // Declaration of an external power() function 6 | extern const int power_range; // Declaration of an external global constant 7 | 8 | int main() 9 | { 10 | for (int i {-power_range}; i <= power_range; ++i) // Calculate powers of 8 11 | std::cout << std::setw(10) << power(8.0, i); 12 | 13 | std::cout << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_02A/Power.cpp: -------------------------------------------------------------------------------- 1 | // The power function called from Ex10_01.cpp is defined in a different translation unit 2 | 3 | // Recursive function to calculate x to the power n 4 | double power(double x, int n) 5 | { 6 | if (n == 0) return 1.0; 7 | else if (n > 0) return x * power(x, n - 1); 8 | else /* n < 0 */ return 1.0 / power(x, -n); 9 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_02A/Range.cpp: -------------------------------------------------------------------------------- 1 | 2 | extern const int power_range{ 3 }; // Definition of a global constant with external linkage 3 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_03/Ex10_03.cpp: -------------------------------------------------------------------------------- 1 | // Creating functions with internal linkage. 2 | // You cannot call the compute() from this translation unit. 3 | // To demonstrate this, try uncommenting its prototype and call inside main() below... 4 | 5 | #include 6 | #include 7 | 8 | double power(double x, int n); 9 | // double compute(double, unsigned n); 10 | 11 | int main() 12 | { 13 | for (int i {-3}; i <= 3; ++i) // Calculate powers of 8 from -3 to +3 14 | std::cout << std::setw(10) << power(8.0, i); 15 | 16 | // unsigned exponent = 8; 17 | // std::cout << compute(2.0, exponent) << std::endl; 18 | 19 | std::cout << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_03/Power.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | The use of the static keyword to specify internal linkage is no longer recommended: 3 | you should always use a nameless namespace instead! 4 | */ 5 | 6 | // Recursive function to calculate x to the power n for unsigned exponents 7 | // This is a local helper function, 8 | // and hence has been specified to have internal linkage. 9 | static double compute(double x, unsigned n) 10 | { 11 | return n == 0? 1.0 : x * compute(x, n - 1); 12 | } 13 | 14 | double power(double x, int n) 15 | { 16 | return n >= 0? compute(x, static_cast(n)) 17 | : 1.0 / compute(x, static_cast(-n)); 18 | } 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_04/Ex10_04.cpp: -------------------------------------------------------------------------------- 1 | // Working with preprocessor operators 2 | #include 3 | 4 | #define DEFINE_PRINT_FUNCTION(NAME, COUNT, VALUE) \ 5 | void NAME##COUNT() { std::cout << #VALUE << std::endl; } 6 | 7 | DEFINE_PRINT_FUNCTION(fun, 123, Test 1 "2" 3) 8 | 9 | int main() 10 | { 11 | fun123(); 12 | } 13 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_04/Power.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Recursive function to calculate x to the power n 3 | double power(double x, int n) 4 | { 5 | if (n == 0) return 1.0; 6 | else if (n > 0) return x * power(x, n - 1); 7 | else /* n < 0 */ return 1.0 / power(x, -n); 8 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_04/Power.h: -------------------------------------------------------------------------------- 1 | #ifndef POWER_H 2 | #define POWER_H 3 | 4 | // Function to calculate x to the power n 5 | double power(double x, int n); 6 | 7 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_05/Ex10_05.cpp: -------------------------------------------------------------------------------- 1 | // Creating and including your own header file 2 | #include 3 | #include 4 | 5 | #include "Power.h" 6 | 7 | int main() 8 | { 9 | for (int i {-3}; i <= 3; ++i) // Calculate powers of 8 from -3 to +3 10 | std::cout << std::setw(10) << power(8.0, i); 11 | 12 | std::cout << std::endl; 13 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_05/Power.cpp: -------------------------------------------------------------------------------- 1 | 2 | // Recursive function to calculate x to the power n 3 | double power(double x, int n) 4 | { 5 | if (n == 0) return 1.0; 6 | else if (n > 0) return x * power(x, n - 1); 7 | else /* n < 0 */ return 1.0 / power(x, -n); 8 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_05/Power.h: -------------------------------------------------------------------------------- 1 | #ifndef POWER_H 2 | #define POWER_H 3 | 4 | // Function to calculate x to the power n 5 | double power(double x, int n); 6 | 7 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_06/Constants.h: -------------------------------------------------------------------------------- 1 | // Using a namespace 2 | #ifndef CONSTANTS_H 3 | #define CONSTANTS_H 4 | 5 | namespace constants 6 | { 7 | inline const double pi { 3.14159265358979323846 }; // the famous pi constant 8 | inline const double e { 2.71828182845904523536 }; // base of the natural logarithm 9 | inline const double sqrt_2 { 1.41421356237309504880 }; // square root of 2 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_06/Ex10_06.cpp: -------------------------------------------------------------------------------- 1 | // Using a namespace 2 | #include 3 | #include "Constants.h" 4 | 5 | int main() 6 | { 7 | std::cout << "pi has the value " << constants::pi << std::endl; 8 | std::cout << "This should be 2: " << constants::sqrt_2 * constants::sqrt_2 << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_06A/Constants.cpp: -------------------------------------------------------------------------------- 1 | // The one and only definitions of the various constants 2 | #include 3 | #include "Constants.h" 4 | 5 | namespace constants 6 | { 7 | extern const double pi { 3.14159265358979323846 }; 8 | extern const double e { 2.71828182845904523536 }; 9 | extern const double sqrt_2 { 1.41421356237309504880 }; 10 | } 11 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_06A/Constants.h: -------------------------------------------------------------------------------- 1 | // Declares three constants that are defined externally 2 | #ifndef CONSTANTS_H 3 | #define CONSTANTS_H 4 | 5 | namespace constants 6 | { 7 | extern const double pi; // the famous pi constant 8 | extern const double e; // base of the natural logarithm 9 | extern const double sqrt_2; // square root of 2 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_06A/Ex10_06A.cpp: -------------------------------------------------------------------------------- 1 | // Using a namespace 2 | #include 3 | #include "Constants.h" 4 | 5 | int main() 6 | { 7 | std::cout << "pi has the value " << constants::pi << std::endl; 8 | std::cout << "This should be 2: " << constants::sqrt_2 * constants::sqrt_2 << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_07/Ex10_07.cpp: -------------------------------------------------------------------------------- 1 | // Using functions in a namespace 2 | #include 3 | #include 4 | 5 | #include "compare.h" 6 | 7 | using compare::max; // Using declaration for max 8 | 9 | 10 | int main() 11 | { 12 | using compare::min; // Using declaration for min 13 | 14 | std::vector data{1.5, 4.6, 3.1, 1.1, 3.8, 2.1}; 15 | std::cout << "Minimum double is " << min(data) << std::endl; 16 | std::cout << "Maximum double is " << max(data) << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_07/compare.cpp: -------------------------------------------------------------------------------- 1 | #include "compare.h" 2 | #include // For std::numeric_limits<>::infinity() 3 | 4 | // Function to find the maximum 5 | double compare::max(const std::vector& data) 6 | { 7 | double result { -std::numeric_limits::infinity() }; 8 | for (const auto value : data) 9 | if (value > result) result = value; 10 | return result; 11 | } 12 | 13 | // Function to find the minimum 14 | double compare::min(const std::vector& data) 15 | { 16 | double result { std::numeric_limits::infinity() }; 17 | for (const auto value : data) 18 | if (value < result) result = value; 19 | return result; 20 | } 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_07/compare.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPARE_H 2 | #define COMPARE_H 3 | #include 4 | 5 | namespace compare 6 | { 7 | double max(const std::vector& data); 8 | double min(const std::vector& data); 9 | } 10 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_08/functions.cpp: -------------------------------------------------------------------------------- 1 | 2 | //#define TESTFUNCTION // Uncomment to get trace output 3 | 4 | #ifdef TESTFUNCTION 5 | #include // Only required for trace output... 6 | #endif 7 | 8 | #include "functions.h" 9 | 10 | // Definition of the function sum 11 | int fun::sum(int x, int y) 12 | { 13 | #ifdef TESTFUNCTION 14 | std::cout << "Function sum called." << std::endl; 15 | #endif 16 | 17 | return x + y; 18 | } 19 | 20 | // Definition of the function product 21 | int fun::product(int x, int y) 22 | { 23 | #ifdef TESTFUNCTION 24 | std::cout << "Function product called." << std::endl; 25 | #endif 26 | 27 | return x * y; 28 | } 29 | 30 | // Definition of the function difference 31 | int fun::difference(int x, int y) 32 | { 33 | #ifdef TESTFUNCTION 34 | std::cout << "Function difference called." << std::endl; 35 | #endif 36 | 37 | return x - y; 38 | } 39 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_08/functions.h: -------------------------------------------------------------------------------- 1 | #ifndef FUNCTIONS_H 2 | #define FUNCTIONS_H 3 | namespace fun 4 | { 5 | // Function prototypes 6 | int sum(int, int); // Sum arguments 7 | int product(int, int); // Product of arguments 8 | int difference(int, int); // Difference between arguments 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_09/Ex10_09.cpp: -------------------------------------------------------------------------------- 1 | // Demonstrating assertions 2 | #include 3 | #include 4 | int main() 5 | { 6 | int y{5}; 7 | 8 | for (int x{}; x < 20; ++x) 9 | { 10 | std::cout << "x = " << x << " y = " << y << std::endl; 11 | assert(x < y); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_10/Ex10_10.cpp: -------------------------------------------------------------------------------- 1 | // Using a static assertion 2 | #include 3 | #include 4 | #include 5 | #include "average.h" 6 | 7 | int main() 8 | { 9 | std::vector vectorData {1.5, 2.5, 3.5, 4.5}; 10 | std::cout << "The average of vectorData is " << average(vectorData) << std::endl; 11 | 12 | // Uncomment the next lines for compiler errors... 13 | // std::vector words {"this", "that", "them", "those"}; 14 | // std::cout << "The average of words values is " << average(words) << std::endl; 15 | 16 | std::vector emptyVector; 17 | average(emptyVector); // Will trigger a runtime assertion! 18 | } 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 10/Ex10_10/average.h: -------------------------------------------------------------------------------- 1 | #ifndef AVERAGE_H 2 | #define AVERAGE_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | T average(const std::vector& values) 10 | { 11 | static_assert(std::is_arithmetic_v, 12 | "Type parameter for average() must be arithmetic."); 13 | assert(!values.empty()); // Not possible using static_assert()! 14 | 15 | T sum {}; 16 | for (auto& value : values) 17 | sum += value; 18 | return sum / values.size(); 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_01B/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lengthValue, double widthValue, double heightValue) 6 | { 7 | std::cout << "Box constructor called." << std::endl; 8 | length = lengthValue; 9 | width = widthValue; 10 | height = heightValue; 11 | } 12 | 13 | // Function to calculate the volume of a box 14 | double Box::volume() 15 | { 16 | return length*width*height; 17 | } 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_01B/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length{1.0}; 8 | double width {1.0}; 9 | double height {1.0}; 10 | 11 | public: 12 | // Constructors 13 | Box(double lengthValue, double widthValue, double heightValue); 14 | Box() = default; 15 | 16 | // Function to calculate the volume of a box 17 | double volume(); 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_01B/Ex11_01B.cpp: -------------------------------------------------------------------------------- 1 | // Defining functions and constructors outside the class definition 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | Box firstBox {80.0, 50.0, 40.0}; // Create a box 8 | double firstBoxVolume{firstBox.volume()}; // Calculate the box volume 9 | std::cout << "Volume of the first Box object is " << firstBoxVolume << std::endl; 10 | 11 | Box secondBox; // Uses compiler-generated default constructor 12 | double secondBoxVolume{secondBox.volume()}; // Calculate the box volume 13 | std::cout << "Volume of the second Box object is " << secondBoxVolume << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_02/Cube.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Cube.h" 3 | 4 | Cube::Cube(double aSide) : side{ aSide } { std::cout << "Cube constructor called." << std::endl; } 5 | 6 | double Cube::volume() { return side*side*side; } 7 | 8 | bool Cube::hasLargerVolumeThan(Cube aCube) { return volume() > aCube.volume(); } 9 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_02/Cube.h: -------------------------------------------------------------------------------- 1 | #ifndef CUBE_H 2 | #define CUBE_H 3 | class Cube 4 | { 5 | private: 6 | double side; 7 | 8 | public: 9 | Cube(double aSide); // Constructor 10 | double volume(); // Calculate volume of a cube 11 | bool hasLargerVolumeThan(Cube aCube); // Compare volume of a cube with another 12 | }; 13 | #endif 14 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_02/Ex11_02.cpp: -------------------------------------------------------------------------------- 1 | // Problems of implicit object conversions 2 | #include 3 | #include "Cube.h" 4 | int main() 5 | { 6 | Cube box1 {7.0}; 7 | Cube box2 {3.0}; 8 | if (box1.hasLargerVolumeThan(box2)) 9 | std::cout << "box1 is larger than box2." << std::endl; 10 | else 11 | std::cout << "Volume of box1 is less than or equal to that of box2." << std::endl; 12 | 13 | std::cout << "volume of box1 is " << box1.volume() << std::endl; 14 | if (box1.hasLargerVolumeThan(50.0)) 15 | std::cout << "Volume of box1 is greater than 50"<< std::endl; 16 | else 17 | std::cout << "Volume of box1 is less than or equal to 50"<< std::endl; 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_03/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) 6 | : length{lv}, width{wv}, height{hv} 7 | { 8 | std::cout << "Box constructor 1 called." << std::endl; 9 | } 10 | 11 | // Constructor for Box that is a cube 12 | Box::Box(double side) : Box{side, side, side} 13 | { 14 | std::cout << "Box constructor 2 called." << std::endl; 15 | } 16 | 17 | // Function to calculate the volume of a box 18 | double Box::volume() 19 | { 20 | return length*width*height; 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_03/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length {1.0}; 8 | double width {1.0}; 9 | double height {1.0}; 10 | 11 | public: 12 | // Constructors 13 | Box(double lv, double wv, double hv); 14 | explicit Box(double side); // Constructor for a cube 15 | Box() = default; // No-arg constructor 16 | 17 | double volume(); // Function to calculate the volume of a box 18 | }; 19 | #endif 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_03/Ex11_03.cpp: -------------------------------------------------------------------------------- 1 | // Using a delegating constructor 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | Box box1 {2.0, 3.0, 4.0}; // An arbitrary box 8 | Box box2 {5.0}; // A box that is a cube 9 | std::cout << "box1 volume = " << box1.volume() << std::endl; 10 | std::cout << "box2 volume = " << box2.volume() << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_04/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) 6 | : length{lv}, width{wv}, height{hv} 7 | { 8 | } 9 | 10 | // Function to calculate the volume of a box 11 | double Box::volume() 12 | { 13 | return length * width * height; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_04/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length{1.0}; 8 | double width{1.0}; 9 | double height{1.0}; 10 | 11 | public: 12 | // Constructors 13 | Box() = default; 14 | Box(double length, double width, double height); 15 | 16 | double volume(); // Function to calculate the volume of a box 17 | 18 | // Functions to provide access to the values of member variables 19 | double getLength() { return length; } 20 | double getWidth() { return width; } 21 | double getHeight() { return height; } 22 | 23 | // Functions to set member variable values 24 | void setLength(double lv) { if (lv > 0) length = lv;} 25 | void setWidth(double wv) { if (wv > 0) width = wv; } 26 | void setHeight(double hv) { if (hv > 0) height = hv; } 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_04/Ex11_04.cpp: -------------------------------------------------------------------------------- 1 | // Adding public getters and setters for private member variables 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | Box myBox {3.0, 4.0, 5.0}; 8 | std::cout << "myBox dimensions are " << myBox.getLength() 9 | << " by " << myBox.getWidth() 10 | << " by " << myBox.getHeight() << std::endl; 11 | 12 | myBox.setLength(-20.0); // Ignored! 13 | myBox.setWidth(40.0); 14 | myBox.setHeight(10.0); 15 | std::cout << "myBox dimensions are now " << myBox.getLength() // 3 (unchanged) 16 | << " by " << myBox.getWidth() // by 40 17 | << " by " << myBox.getHeight() << std::endl; // by 10 18 | } 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_05/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) 6 | : length{lv}, width{wv}, height{hv} 7 | { 8 | } 9 | 10 | // Function to calculate the volume of a box 11 | double Box::volume() 12 | { 13 | return length * width * height; 14 | } 15 | 16 | // Functions to access the dimensions of a Box 17 | Box& Box::setLength(double lv) 18 | { 19 | if (lv > 0) length = lv; 20 | return *this; 21 | } 22 | Box& Box::setWidth(double wv) 23 | { 24 | if (wv > 0) width = wv; 25 | return *this; 26 | } 27 | Box& Box::setHeight(double hv) 28 | { 29 | if (hv > 0) height = hv; 30 | return *this; 31 | } 32 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_05/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length{1.0}; 8 | double width{1.0}; 9 | double height{1.0}; 10 | 11 | public: 12 | // Constructors 13 | Box() = default; 14 | Box(double length, double width, double height); 15 | 16 | double volume(); // Function to calculate the volume of a box 17 | 18 | // Functions to provide access to the values of member variables 19 | double getLength() { return length; } 20 | double getWidth() { return width; } 21 | double getHeight() { return height; } 22 | 23 | // Functions to set member variable values 24 | Box& setLength(double lv); 25 | Box& setWidth(double wv); 26 | Box& setHeight(double hv); 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_05/Ex11_05.cpp: -------------------------------------------------------------------------------- 1 | // Allowing method chaining by returning a reference to this 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | Box myBox {3.0, 4.0, 5.0}; 8 | std::cout << "myBox dimensions are " << myBox.getLength() 9 | << " by " << myBox.getWidth() 10 | << " by " << myBox.getHeight() << std::endl; 11 | 12 | // Method chaining: set all dimensions of a Box in one statement: 13 | myBox.setLength(-20.0).setWidth(40.0).setHeight(10.0); 14 | std::cout << "myBox dimensions are now " << myBox.getLength() // 3 (unchanged: -20 was ignored) 15 | << " by " << myBox.getWidth() // by 40 16 | << " by " << myBox.getHeight() << std::endl; // by 10 17 | } 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_06/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) 6 | : length{lv}, width{wv}, height{hv} 7 | { 8 | } 9 | 10 | // Function to calculate the volume of a box 11 | double Box::volume() const 12 | { 13 | return length * width * height; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_06/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length{1.0}; 8 | double width{1.0}; 9 | double height{1.0}; 10 | 11 | public: 12 | // Constructors 13 | Box() = default; 14 | Box(double length, double width, double height); 15 | 16 | double volume() const; // Function to calculate the volume of a box 17 | 18 | // Functions to provide access to the values of member variables 19 | double getLength() const { return length; } 20 | double getWidth() const { return width; } 21 | double getHeight() const { return height; } 22 | 23 | // Functions to set member variable values 24 | void setLength(double lv) { if (lv > 0) length = lv;} 25 | void setWidth(double wv) { if (wv > 0) width = wv; } 26 | void setHeight(double hv) { if (hv > 0) height = hv; } 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_07/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) 6 | : _length{lv}, _width{wv}, _height{hv} 7 | { 8 | } 9 | 10 | // Function to calculate the volume of a box 11 | double Box::volume() const 12 | { 13 | return _length * _width * _height; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_07/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double _length{1.0}; 8 | double _width{1.0}; 9 | double _height{1.0}; 10 | 11 | public: 12 | // Constructors 13 | Box() = default; 14 | Box(double length, double width, double height); 15 | 16 | double volume() const; // Function to calculate the volume of a box 17 | 18 | // Functions to provide access to the values of member variables 19 | double length() const { return _length; } 20 | double width() const { return _width; } 21 | double height() const { return _height; } 22 | 23 | // Functions to allow member variable values to be modified (for non-const objects only!) 24 | double& length() { return _length; } 25 | double& width() { return _width; } 26 | double& height() { return _height; } 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_07/Ex11_07.cpp: -------------------------------------------------------------------------------- 1 | // Overloaded const and non-const member functions 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | Box box; 8 | box.length() = 2; // References can be used to the right of an assignment 9 | std::cout << box.length() << std::endl; // Prints 2 10 | 11 | const Box constBox; 12 | // constBox.length() = 2; // Does not compile: good! 13 | std::cout << constBox.length() << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_08/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) 6 | : length{lv}, width{wv}, height{hv} 7 | { 8 | } 9 | 10 | // Function to calculate the volume of a box 11 | double Box::volume() const 12 | { 13 | return length * width * height; 14 | } 15 | 16 | // Function to print the volume of a Box to std::cout 17 | void Box::printVolume() const 18 | { 19 | // Count how many times printVolume() is called using a mutable member in a const function 20 | std::cout << "The volume of this box is " << volume() << std::endl; 21 | std::cout << "printVolume() has been called " << ++count << " time(s)" << std::endl; 22 | } 23 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_08/Ex11_08.cpp: -------------------------------------------------------------------------------- 1 | // Defining mutable member variables that can be modified within const member functions 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | Box myBox {3.0, 4.0, 5.0}; 8 | myBox.printVolume(); 9 | 10 | myBox.setHeight(55.5); 11 | myBox.printVolume(); 12 | 13 | // Even for constant Boxes mutable member variables can be modified: 14 | const Box constBox{ 1.0, 2.0, 3.0 }; 15 | constBox.printVolume(); 16 | constBox.printVolume(); 17 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_09/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) : length {lv}, width {wv}, height{hv} 6 | { 7 | std::cout << "Box constructor called." << std::endl; 8 | } 9 | 10 | // Function to calculate the volume of a box 11 | double Box::volume() const 12 | { 13 | return length*width*height; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_09/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length; 8 | double width; 9 | double height; 10 | 11 | public: 12 | // Constructors 13 | Box(double lv = 1.0, double wv = 1.0, double hv = 1.0); 14 | 15 | double volume() const; // Function to calculate the volume of a box 16 | 17 | friend double surfaceArea(const Box& aBox); // Friend function for the surface area 18 | }; 19 | #endif 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_10/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | private: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | /* Constructors */ 14 | Box(double lv, double wv, double hv); 15 | Box(double side); // Constructor for a cube 16 | Box(); // Default constructor 17 | Box(const Box& box); // Copy constructor 18 | 19 | double volume() const; // Function to calculate the volume of a box 20 | }; 21 | #endif 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_10/Ex11_10.cpp: -------------------------------------------------------------------------------- 1 | // Creating an array of objects 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | const Box box1 {2.0, 3.0, 4.0}; // An arbitrary box 8 | Box box2 {5.0}; // A box that is a cube 9 | std::cout << "box1 volume = " << box1.volume() << std::endl; 10 | std::cout << "box2 volume = " << box2.volume() << std::endl; 11 | Box box3 {box2}; 12 | std::cout << "box3 volume = " << box3.volume() << std::endl; // Volume = 125 13 | 14 | std::cout << std::endl; 15 | 16 | Box boxes[6] {box1, box2, box3, Box {2.0}}; 17 | } 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_11/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | private: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | static inline size_t objectCount {}; // Count of objects in existence 12 | 13 | public: 14 | // Constructors 15 | Box(double lv, double wv, double hv); 16 | Box(double side); // Constructor for a cube 17 | Box(); // Default constructor 18 | Box(const Box& box); // Copy constructor 19 | 20 | double volume() const; // Function to calculate the volume of a box 21 | 22 | size_t getObjectCount() const { return objectCount; } 23 | }; 24 | #endif 25 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_11/Ex11_11.cpp: -------------------------------------------------------------------------------- 1 | // Using a static data member 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | const Box box1 {2.0, 3.0, 4.0}; // An arbitrary box 8 | Box box2 {5.0}; // A box that is a cube 9 | std::cout << "box1 volume = " << box1.volume() << std::endl; 10 | std::cout << "box2 volume = " << box2.volume() << std::endl; 11 | Box box3 {box2}; 12 | std::cout << "box3 volume = " << box3.volume() << std::endl; // Volume = 125 13 | 14 | std::cout << std::endl; 15 | 16 | Box boxes[6] {box1, box2, box3, Box {2.0}}; 17 | 18 | std::cout << "\nThere are now " << box1.getObjectCount() << " Box objects." << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_12/CylindricalBox.cpp: -------------------------------------------------------------------------------- 1 | #include "CylindricalBox.h" 2 | 3 | CylindricalBox::CylindricalBox(float r, float h, std::string_view mat) 4 | : radius(r) 5 | , height(h) 6 | , material(mat) 7 | { 8 | } 9 | 10 | double CylindricalBox::volume() const 11 | { 12 | return PI * radius * radius * height; 13 | } 14 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_12/CylindricalBox.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | class CylindricalBox 5 | { 6 | public: 7 | static inline const float maxRadius{ 35.0f }; 8 | static inline const float maxHeight{ 60.0f }; 9 | static inline const std::string defaultMaterial{ "paperboard" }; 10 | 11 | CylindricalBox(float radius, float height, std::string_view material = defaultMaterial); 12 | 13 | double volume() const; 14 | 15 | private: 16 | // The value of PI used by CylindricalBox's volume() function 17 | static inline const float PI{ 3.141592f }; 18 | 19 | double radius; 20 | double height; 21 | std::string material; 22 | }; 23 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_12/Ex11_12.cpp: -------------------------------------------------------------------------------- 1 | // Defining and accessing static constants 2 | #include 3 | #include "CylindricalBox.h" 4 | 5 | int main() 6 | { 7 | const CylindricalBox bigBox{ 1.23f, CylindricalBox::maxHeight, CylindricalBox::defaultMaterial }; 8 | std::cout << "The volume of bigBox is " << bigBox.volume() << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_13/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | private: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | static inline size_t objectCount {}; // Count of objects in existence 12 | 13 | public: 14 | // Constructors 15 | Box(double lv, double wv, double hv); 16 | Box(double side); // Constructor for a cube 17 | Box(); // Default constructor 18 | Box(const Box& box); // Copy constructor 19 | 20 | double volume() const; // Function to calculate the volume of a box 21 | 22 | static size_t getObjectCount() { return objectCount; } 23 | 24 | ~Box(); // Destructor 25 | }; 26 | #endif 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 11/Ex11_14/Package.h: -------------------------------------------------------------------------------- 1 | #ifndef PACKAGE_H 2 | #define PACKAGE_H 3 | #include 4 | #include "Box.h" 5 | 6 | using SharedBox = std::shared_ptr; 7 | 8 | class Package 9 | { 10 | private: 11 | SharedBox pBox; // Pointer to the Box object contained in this Package 12 | Package* pNext; // Pointer to the next Package in the list 13 | 14 | public: 15 | Package(SharedBox pb) : pBox{pb}, pNext{nullptr} {} // Constructor 16 | ~Package() { delete pNext; } // Destructor 17 | 18 | // Retrieve the Box pointer 19 | SharedBox getBox() const { return pBox; } 20 | 21 | // Retrieve or update the pointer to the next Package 22 | Package* getNext() { return pNext; } 23 | void setNext(Package* pPackage) { pNext = pPackage; } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_01/Ex12_01.cpp: -------------------------------------------------------------------------------- 1 | // Implementing a less-than operator 2 | #include 3 | #include 4 | #include "Box.h" 5 | 6 | int main() 7 | { 8 | std::vector boxes { 9 | Box {2.0, 2.0, 3.0}, 10 | Box {1.0, 3.0, 2.0}, 11 | Box {1.0, 2.0, 1.0}, 12 | Box {2.0, 3.0, 3.0} 13 | }; 14 | 15 | Box smallBox {boxes[0]}; 16 | for (const auto& box : boxes) 17 | { 18 | if (box < smallBox) smallBox = box; 19 | } 20 | 21 | std::cout << "The smallest box has dimensions: " 22 | << smallBox.getLength() << 'x' 23 | << smallBox.getWidth() << 'x' 24 | << smallBox.getHeight() << std::endl; 25 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_02/Ex12_02.cpp: -------------------------------------------------------------------------------- 1 | // Using the overloaded 'less-than' operators for Box ojects 2 | #include 3 | #include 4 | #include "Box.h" 5 | 6 | // Display box dimensions 7 | void show(const Box& box) 8 | { 9 | std::cout << "Box " << box.getLength() 10 | << 'x' << box.getWidth() 11 | << 'x' << box.getHeight() << std::endl; 12 | } 13 | 14 | int main() 15 | { 16 | std::vector boxes { 17 | Box {2.0, 2.0, 3.0}, Box {1.0, 3.0, 2.0}, 18 | Box {1.0, 2.0, 1.0}, Box {2.0, 3.0, 3.0} 19 | }; 20 | 21 | const double minVolume{6.0}; 22 | std::cout << "Objects with volumes less than " << minVolume << " are:\n"; 23 | for (const auto& box : boxes) 24 | if (box < minVolume) show(box); 25 | 26 | std::cout << "Objects with volumes greater than " << minVolume << " are:\n"; 27 | for (const auto& box : boxes) 28 | if (minVolume < box) show(box); 29 | } 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_07/Ex12_07.cpp: -------------------------------------------------------------------------------- 1 | // Implicit conversions reduce the number of operator functions 2 | #include 3 | #include "Integer.h" 4 | 5 | int main() 6 | { 7 | const Integer i{1}; 8 | const Integer j{2}; 9 | const auto result = (i * 2 + 4 / j - 1) % j; 10 | std::cout << result.getValue() << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_07/Integer.h: -------------------------------------------------------------------------------- 1 | #ifndef INTEGER_H 2 | #define INTEGER_H 3 | 4 | class Integer 5 | { 6 | private: 7 | int n; 8 | public: 9 | Integer(int m = 0) : n{m} {} 10 | int getValue() const { return n; } 11 | void setValue(int m) { n = m; } 12 | }; 13 | 14 | Integer operator+(const Integer& one, const Integer& other) 15 | { 16 | return one.getValue() + other.getValue(); 17 | } 18 | Integer operator-(const Integer& one, const Integer& other) 19 | { 20 | return one.getValue() - other.getValue(); 21 | } 22 | Integer operator*(const Integer& one, const Integer& other) 23 | { 24 | return one.getValue() * other.getValue(); 25 | } 26 | Integer operator/(const Integer& one, const Integer& other) 27 | { 28 | return one.getValue() / other.getValue(); 29 | } 30 | Integer operator%(const Integer& one, const Integer& other) 31 | { 32 | return one.getValue() % other.getValue(); 33 | } 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_08/Ex12_08.cpp: -------------------------------------------------------------------------------- 1 | // Overloading a unary "rotate" operator 2 | #include 3 | #include "Box.h" 4 | 5 | int main() 6 | { 7 | Box someBox{ 1, 2, 3 }; 8 | std::cout << ~someBox << std::endl; 9 | } 10 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_09/Ex12_09.cpp: -------------------------------------------------------------------------------- 1 | // Overloading the prefix and postfix increment and decrement operators 2 | #include 3 | #include 4 | #include "Box.h" 5 | 6 | int main() 7 | { 8 | Box theBox {3.0, 1.0, 3.0}; 9 | 10 | std::cout << "Our test Box is " << theBox << std::endl; 11 | 12 | std::cout << "Postfix increment evaluates to the original object: " << theBox++ << std::endl; 13 | std::cout << "After postfix increment: " << theBox << std::endl; 14 | 15 | std::cout << "Prefix decrement evaluates to the decremented object: " << --theBox << std::endl; 16 | std::cout << "After prefix decrement: " << theBox << std::endl; 17 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_12/Ex12_12.cpp: -------------------------------------------------------------------------------- 1 | // Defining a copy assignment operator 2 | #include "Message.h" 3 | #include 4 | 5 | int main() 6 | { 7 | Message beware {"Careful"}; 8 | Message warning; 9 | 10 | warning = beware; // Call assignment operator 11 | 12 | std::cout << "After assignment beware is: " << beware.getText() << std::endl; 13 | std::cout << "After assignment warning is: " << warning.getText() << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_12/Message.h: -------------------------------------------------------------------------------- 1 | #ifndef MESSAGE_H 2 | #define MESSAGE_H 3 | 4 | #include 5 | #include 6 | 7 | class Message 8 | { 9 | public: 10 | explicit Message(std::string_view message = "") : pText{new std::string(message)} {} 11 | ~Message() { delete pText; } 12 | 13 | std::string_view getText() const { return *pText; } 14 | 15 | Message& operator=(const Message& message) 16 | { 17 | if (this != &message) 18 | { 19 | delete pText; // Delete the previous text 20 | pText = new std::string(*message.pText); // Duplicate the object in the heap 21 | } 22 | return *this; // Return the left operand 23 | } 24 | 25 | private: 26 | std::string* pText; 27 | }; 28 | 29 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_12A/Ex12_12A.cpp: -------------------------------------------------------------------------------- 1 | // Defining both a copy assignment operator and a copy contructor 2 | #include "Message.h" 3 | #include 4 | 5 | int main() 6 | { 7 | Message beware {"Careful"}; 8 | Message warning; 9 | 10 | warning = beware; // Call assignment operator 11 | Message otherWarning{warning}; // Calls the copy constructor 12 | 13 | std::cout << "After assignment beware is: " << beware.getText() << std::endl; 14 | std::cout << "After assignment warning is: " << warning.getText() << std::endl; 15 | std::cout << "The copy-constructed otherWarning is: " << otherWarning.getText() << std::endl; 16 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 12/Ex12_12A/Message.h: -------------------------------------------------------------------------------- 1 | #ifndef MESSAGE_H 2 | #define MESSAGE_H 3 | 4 | #include 5 | #include 6 | 7 | class Message 8 | { 9 | public: 10 | explicit Message(std::string_view message = "") : pText{new std::string(message)} {} 11 | ~Message() { delete pText; } 12 | Message(const Message& src) 13 | : pText{ new std::string(*src.pText) } 14 | { 15 | } 16 | 17 | std::string_view getText() const { return *pText; } 18 | 19 | Message& operator=(const Message& message) 20 | { 21 | if (this != &message) 22 | { 23 | delete pText; // Delete the previous text 24 | pText = new std::string(*message.pText); // Duplicate the object in the heap 25 | } 26 | return *this; // Return the left operand 27 | } 28 | 29 | private: 30 | std::string* pText; 31 | }; 32 | 33 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_01/Carton.h: -------------------------------------------------------------------------------- 1 | // Carton.h - defines the Carton class with the Box class as base 2 | #ifndef CARTON_H 3 | #define CARTON_H 4 | #include 5 | #include 6 | #include "Box.h" 7 | 8 | class Carton : public Box 9 | { 10 | private: 11 | std::string material; 12 | 13 | public: 14 | explicit Carton(std::string_view mat = "Cardboard") : material {mat}{} // Constructor 15 | // double carton_volume() const { return length*width*height; } // Uncomment for an error! 16 | }; 17 | #endif 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_01A/Carton.h: -------------------------------------------------------------------------------- 1 | // Carton.h - defines the Carton class with the Box class as base 2 | #ifndef CARTON_H 3 | #define CARTON_H 4 | #include 5 | #include 6 | #include "Box.h" 7 | 8 | class Carton : private Box 9 | { 10 | private: 11 | std::string material; 12 | 13 | public: 14 | using Box::volume; // Inherit as public 15 | explicit Carton(std::string_view mat = "Cardboard") : material {mat} {} // Constructor 16 | }; 17 | #endif 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_02/Ex13_02.cpp: -------------------------------------------------------------------------------- 1 | // Calling base class constructors in a derived class constructor 2 | #include 3 | #include "Carton.h" // For the Carton class 4 | 5 | int main() 6 | { 7 | // Create four Carton objects 8 | Carton carton1; std::cout << std::endl; 9 | Carton carton2 {"Thin cardboard"}; std::cout << std::endl; 10 | Carton carton3 {4.0, 5.0, 6.0, "Plastic"}; std::cout << std::endl; 11 | Carton carton4 {2.0, "paper"}; std::cout << std::endl; 12 | 13 | std::cout << "carton1 volume is " << carton1.volume() << std::endl; 14 | std::cout << "carton2 volume is " << carton2.volume() << std::endl; 15 | std::cout << "carton3 volume is " << carton3.volume() << std::endl; 16 | std::cout << "carton4 volume is " << carton4.volume() << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_03/Ex13_03.cpp: -------------------------------------------------------------------------------- 1 | // Using a derived class copy constructor 2 | #include 3 | #include "Carton.h" 4 | 5 | int main() 6 | { 7 | // Declare and initialize a Carton object 8 | Carton carton{20.0, 30.0, 40.0, "Glassine board"}; 9 | std::cout << std::endl; 10 | 11 | Carton cartonCopy{carton}; // Use copy constructor 12 | std::cout << std::endl; 13 | 14 | std::cout << "Volume of carton is " << carton.volume() << std::endl 15 | << "Volume of cartonCopy is " << cartonCopy.volume() << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_04/Carton.h: -------------------------------------------------------------------------------- 1 | // Carton.h - defines the Carton class with the Box class as base 2 | #ifndef CARTON_H 3 | #define CARTON_H 4 | #include 5 | #include 6 | #include "Box.h" 7 | 8 | class Carton : public Box 9 | { 10 | using Box::Box; // Inherit Box class constructors 11 | 12 | private: 13 | std::string material {"Cardboard"}; 14 | 15 | public: 16 | Carton(double lv, double wv, double hv, std::string_view mat) 17 | : Box {lv, wv, hv}, material {mat} 18 | { std::cout << "Carton(double,double,double,string_view) called.\n"; } 19 | 20 | // Carton() = default; // Uncomment if you want a default constructor for Carton 21 | }; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_04/Ex13_04.cpp: -------------------------------------------------------------------------------- 1 | // Inheriting constructors 2 | #include 3 | #include "Box.h" // For the Box class 4 | #include "Carton.h" // For the Carton class 5 | 6 | int main() 7 | { 8 | // Carton cart; // Does not compile: default constructor is not inherited! 9 | Carton cube{4.0}; // Calls inherited constructor 10 | 11 | std::cout << std::endl; 12 | Carton cartcopy { cube }; // Calls default copy constructor 13 | 14 | std::cout << std::endl; 15 | Carton carton {1.0, 2.0, 3.0}; // Calls inherited constructor 16 | 17 | std::cout << std::endl; 18 | Carton candyCarton (50.0, 30.0, 20.0, "Thin cardboard"); // Calls Carton class constructor 19 | } 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_05/Ex13_05.cpp: -------------------------------------------------------------------------------- 1 | // Destructors in a class hierarchy 2 | #include 3 | #include "Carton.h" // For the Carton class 4 | 5 | int main() 6 | { 7 | Carton carton; 8 | Carton candyCarton {50.0, 30.0, 20.0, "Thin cardboard"}; 9 | 10 | std::cout << "carton volume is " << carton.volume() << std::endl; 11 | std::cout << "candyCarton volume is " << candyCarton.volume() << std::endl; 12 | } 13 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_06/CerealPack.h: -------------------------------------------------------------------------------- 1 | // Cerealpack.h - Class defining a carton of cereal 2 | #ifndef CEREALPACK_H 3 | #define CEREALPACK_H 4 | #include 5 | #include "Carton.h" 6 | #include "Contents.h" 7 | 8 | class CerealPack : public Carton, public Contents 9 | { 10 | public: 11 | CerealPack(double length, double width, double height, std::string_view cerealType) 12 | : Carton {length, width, height, "cardboard"}, Contents {cerealType} 13 | { 14 | std::cout << "CerealPack constructor" << std::endl; 15 | Contents::volume = 0.9 * Carton::volume(); // Set contents volume 16 | } 17 | 18 | // Destructor 19 | ~CerealPack() 20 | { 21 | std::cout << "CerealPack destructor" << std::endl; 22 | } 23 | }; 24 | #endif 25 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_06/Ex13_06.cpp: -------------------------------------------------------------------------------- 1 | // Ex13_06 - doesn't compile! 2 | // Using multiple inheritance 3 | #include 4 | #include "CerealPack.h" // For the CerealPack class 5 | 6 | int main() 7 | { 8 | CerealPack cornflakes {8.0, 3.0, 10.0, "Cornflakes"}; 9 | 10 | std::cout << "cornflakes volume is " << cornflakes.volume() << std::endl 11 | << "cornflakes weight is " << cornflakes.getWeight() << std::endl; 12 | } 13 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_06A/CerealPack.h: -------------------------------------------------------------------------------- 1 | // Cerealpack.h - Class defining a carton of cereal 2 | #ifndef CEREALPACK_H 3 | #define CEREALPACK_H 4 | #include 5 | #include "Carton.h" 6 | #include "Contents.h" 7 | 8 | class CerealPack : public Carton, public Contents 9 | { 10 | public: 11 | CerealPack(double length, double width, double height, std::string_view cerealType) 12 | : Carton {length, width, height, "cardboard"}, Contents {cerealType} 13 | { 14 | std::cout << "CerealPack constructor" << std::endl; 15 | Contents::volume = 0.9 * Carton::volume(); // Set contents volume 16 | } 17 | 18 | // Destructor 19 | ~CerealPack() 20 | { 21 | std::cout << "CerealPack destructor" << std::endl; 22 | } 23 | }; 24 | #endif 25 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_06A/Ex13_06A.cpp: -------------------------------------------------------------------------------- 1 | // Ex13_05A 2 | // Using multiple inheritance 3 | #include 4 | #include "CerealPack.h" // For the CerealPack class 5 | 6 | int main() 7 | { 8 | CerealPack cornflakes {8.0, 3.0, 10.0, "Cornflakes"}; 9 | 10 | std::cout << "cornflakes volume is " << cornflakes.Carton::volume() << std::endl 11 | << "cornflakes weight is " << cornflakes.Contents::getWeight() << std::endl; 12 | } 13 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_06B/CerealPack.h: -------------------------------------------------------------------------------- 1 | // Cerealpack.h - Class defining a carton of cereal 2 | #ifndef CEREALPACK_H 3 | #define CEREALPACK_H 4 | #include 5 | #include "Carton.h" 6 | #include "Contents.h" 7 | 8 | class CerealPack : public Carton, public Contents 9 | { 10 | public: 11 | CerealPack(double length, double width, double height, std::string_view cerealType) 12 | : Carton {length, width, height, "cardboard"}, Contents {cerealType} 13 | { 14 | std::cout << "CerealPack constructor" << std::endl; 15 | Contents::volume = 0.9 * Carton::volume(); // Set contents volume 16 | } 17 | 18 | // Destructor 19 | ~CerealPack() 20 | { 21 | std::cout << "CerealPack destructor" << std::endl; 22 | } 23 | 24 | using Carton::volume; 25 | using Contents::getWeight; 26 | }; 27 | #endif 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 13/Ex13_06B/Ex13_06B.cpp: -------------------------------------------------------------------------------- 1 | // Ex13_06B 2 | // Using multiple inheritance 3 | #include 4 | #include "CerealPack.h" // For the CerealPack class 5 | 6 | int main() 7 | { 8 | CerealPack cornflakes {8.0, 3.0, 10.0, "Cornflakes"}; 9 | 10 | std::cout << "cornflakes volume is " << cornflakes.volume() << std::endl 11 | << "cornflakes weight is " << cornflakes.getWeight() << std::endl; 12 | } 13 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_01/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | // Function to show the volume of an object 17 | void showVolume() const 18 | { 19 | std::cout << "Box usable volume is " << volume() << std::endl; 20 | } 21 | 22 | // Function to calculate the volume of a Box object 23 | double volume() const { return length*width*height; } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_01/Ex14_01.cpp: -------------------------------------------------------------------------------- 1 | // Behavior of inherited functions in a derived class 2 | #include "Box.h" // For the Box class 3 | #include "ToughPack.h" // For the ToughPack class 4 | 5 | int main() 6 | { 7 | Box box {20.0, 30.0, 40.0}; // Define a box 8 | ToughPack hardcase {20.0, 30.0, 40.0}; // Declare tough box - same size 9 | 10 | box.showVolume(); // Display volume of base box 11 | hardcase.showVolume(); // Display volume of derived box 12 | 13 | //std::cout << "hardcase volume is " << hardcase.volume() << std::endl; 14 | //Box *pBox {&hardcase}; 15 | //std::cout << "hardcase volume through pBox is " << pBox->volume() << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_01/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | // Function to calculate volume of a ToughPack allowing 15% for packing 13 | double volume() const { return 0.85 * length*width*height; } 14 | }; 15 | #endif 16 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_02/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | // Function to show the volume of an object 17 | void showVolume() const 18 | { 19 | std::cout << "Box usable volume is " << volume() << std::endl; 20 | } 21 | 22 | // Function to calculate the volume of a Box object 23 | virtual double volume() const { return length*width*height; } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_02/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str) 15 | : Box {lv, wv, hv} 16 | { 17 | material = str; 18 | } 19 | 20 | // Function to calculate the volume of a Carton object 21 | double volume() const 22 | { 23 | const double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 24 | return vol > 0.0? vol : 0.0; 25 | } 26 | }; 27 | #endif 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_02/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | // Function to calculate volume of a ToughPack allowing 15% for packing 13 | double volume() const { return 0.85 * length*width*height; } 14 | }; 15 | #endif 16 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_03/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | // Function to show the volume of an object 17 | void showVolume() const 18 | { 19 | std::cout << "Box usable volume is " << volume() << std::endl; 20 | } 21 | 22 | // Function to calculate the volume of a Box object 23 | virtual double volume() const { return length*width*height; } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_03/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str) 15 | : Box {lv, wv, hv}, material {str} 16 | {} 17 | 18 | // Function to calculate the volume of a Carton object 19 | double volume() const override 20 | { 21 | const double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 22 | return vol > 0.0? vol : 0.0; 23 | } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_03/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | protected: 13 | // Function to calculate volume of a ToughPack allowing 15% for packing 14 | double volume() const override { return 0.85 * length*width*height; } 15 | }; 16 | #endif 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_04/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | // Function to show the volume of an object 17 | void showVolume() const 18 | { 19 | std::cout << "Box usable volume is " << volume() << std::endl; 20 | } 21 | 22 | // Function to calculate the volume of a Box object 23 | virtual double volume(int i=5) const 24 | { 25 | std::cout << "Box parameter = " << i << std::endl; 26 | return length*width*height; 27 | } 28 | }; 29 | #endif 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_04/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str = "material") 15 | : Box {lv, wv, hv}, material{ str } 16 | {} 17 | 18 | // Function to calculate the volume of a Carton object 19 | double volume(int i = 50) const override 20 | { 21 | std::cout << "Carton parameter = " << i << std::endl; 22 | const double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 23 | return vol > 0.0 ? vol : 0.0; 24 | } 25 | }; 26 | #endif 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_04/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | // Function to calculate volume of a ToughPack allowing 15% for packing 13 | double volume(int i = 500) const override 14 | { 15 | std::cout << "ToughPack parameter = " << i << std::endl; 16 | return 0.85 * length*width*height; 17 | } 18 | }; 19 | #endif 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_05/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | // Function to show the volume of an object 17 | void showVolume() const 18 | { 19 | std::cout << "Box usable volume is " << volume() << std::endl; 20 | } 21 | 22 | // Function to calculate the volume of a Box object 23 | virtual double volume() const { return length*width*height; } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_05/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str = "material") 15 | : Box {lv, wv, hv}, material{ str } 16 | {} 17 | 18 | // Function to calculate the volume of a Carton object 19 | double volume() const override 20 | { 21 | const double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 22 | return vol > 0.0 ? vol : 0.0; 23 | } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_05/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | // Function to calculate volume of a ToughPack allowing 15% for packing 13 | double volume() const override { return 0.85 * length*width*height; } 14 | }; 15 | #endif 16 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_06/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | // Function to show the volume of an object 17 | void showVolume() const 18 | { 19 | std::cout << "Box usable volume is " << volume() << std::endl; 20 | } 21 | 22 | // Function to calculate the volume of a Box object 23 | virtual double volume() const { return length*width*height; } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_06/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str) 15 | : Box {lv, wv, hv}, material {str} 16 | {} 17 | 18 | // Function to calculate the volume of a Carton object 19 | double volume() const override 20 | { 21 | double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 22 | return vol > 0.0? vol : 0.0; 23 | } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_06/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | protected: 13 | // Function to calculate volume of a ToughPack allowing 15% for packing 14 | double volume() const override { return 0.85 * length*width*height; } 15 | }; 16 | #endif 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_07/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | // Uncomment the virtual keyword to make destruction work as it should 17 | /*virtual*/ ~Box() { std::cout << "Box destructor called" << std::endl; } 18 | 19 | // Function to show the volume of an object 20 | void showVolume() const 21 | { 22 | std::cout << "Box usable volume is " << volume() << std::endl; 23 | } 24 | 25 | // Function to calculate the volume of a Box object 26 | virtual double volume() const { return length*width*height; } 27 | }; 28 | #endif 29 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_07/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str) 15 | : Box {lv, wv, hv}, material{ str } 16 | {} 17 | 18 | ~Carton() { std::cout << "Carton destructor called" << std::endl; } 19 | 20 | // Function to calculate the volume of a Carton object 21 | double volume() const 22 | { 23 | double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 24 | return vol > 0.0? vol : 0.0; 25 | } 26 | }; 27 | #endif 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_07/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | ~ToughPack() { std::cout << "ToughPack destructor called" << std::endl; } 13 | 14 | protected: 15 | // Function to calculate volume of a ToughPack allowing 15% for packing 16 | double volume() const override { return 0.85 * length*width*height; } 17 | }; 18 | #endif 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_07A/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | virtual ~Box() { std::cout << "Box destructor called" << std::endl; } 17 | 18 | // Function to show the volume of an object 19 | void showVolume() const 20 | { 21 | std::cout << "Box usable volume is " << volume() << std::endl; 22 | } 23 | 24 | // Function to calculate the volume of a Box object 25 | virtual double volume() const { return length*width*height; } 26 | }; 27 | #endif 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_07A/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str) 15 | : Box {lv, wv, hv}, material{ str } 16 | {} 17 | 18 | ~Carton() { std::cout << "Carton destructor called" << std::endl; } 19 | 20 | // Function to calculate the volume of a Carton object 21 | double volume() const 22 | { 23 | double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 24 | return vol > 0.0? vol : 0.0; 25 | } 26 | }; 27 | #endif 28 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_07A/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | ~ToughPack() { std::cout << "ToughPack destructor called" << std::endl; } 13 | 14 | protected: 15 | // Function to calculate volume of a ToughPack allowing 15% for packing 16 | double volume() const override { return 0.85 * Box::volume(); } 17 | }; 18 | #endif 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_08/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | private: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box(double lv, double wv, double hv) 14 | : length {lv}, width {wv}, height {hv} 15 | { 16 | std::cout << "Box constructor called for a Box of volume " << volume() << std::endl; 17 | } 18 | virtual ~Box() 19 | { 20 | std::cout << "Box destructor called for a Box of volume " << volume() << std::endl; 21 | } 22 | 23 | // Function to calculate volume of a Box 24 | virtual double volume() const { return length * width * height; } 25 | 26 | void showVolume() const 27 | { 28 | std::cout << "The volume from inside Box::showVolume() is " << volume() << std::endl; 29 | } 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_08/Ex14_08.cpp: -------------------------------------------------------------------------------- 1 | // Calling virtual functions from constructors and destructors 2 | #include "Box.h" 3 | #include "ToughPack.h" 4 | 5 | int main() 6 | { 7 | ToughPack toughPack{1.0, 2.0, 3.0}; 8 | toughPack.showVolume(); // Should show a volume equal to 85% of 1x2x3, or 5.1 9 | } 10 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_08/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGH_PACK_H 2 | #define TOUGH_PACK_H 3 | #include "Box.h" 4 | 5 | class ToughPack : public Box 6 | { 7 | public: 8 | ToughPack(double lv, double wv, double hv) 9 | : Box{lv, wv, hv} 10 | { 11 | std::cout << "ToughPack constructor called for a Box of volume " 12 | << volume() << std::endl; 13 | } 14 | virtual ~ToughPack() 15 | { 16 | std::cout << "ToughPack destructor called for a Box of volume " << volume() << std::endl; 17 | } 18 | 19 | // Function to calculate volume of a ToughPack allowing 15% for packing 20 | double volume() const override { return 0.85 * Box::volume(); } 21 | }; 22 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_09/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include 4 | 5 | class Box 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 14 | 15 | virtual ~Box() = default; 16 | 17 | // Function to show the volume of an object 18 | void showVolume() const 19 | { 20 | std::cout << "Box usable volume is " << volume() << std::endl; 21 | } 22 | 23 | // Function to calculate the volume of a Box object 24 | virtual double volume() const { return length*width*height; } 25 | }; 26 | #endif 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_09/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str) 15 | : Box {lv, wv, hv}, material {str} 16 | {} 17 | 18 | // Function to calculate the volume of a Carton object 19 | double volume() const override 20 | { 21 | double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 22 | return vol > 0.0? vol : 0.0; 23 | } 24 | }; 25 | #endif 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_10/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | protected: 7 | double length {1.0}; 8 | double width {1.0}; 9 | double height {1.0}; 10 | 11 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 12 | 13 | public: 14 | virtual ~Box() = default; // Virtual destructor 15 | virtual double volume() const = 0; // Function to calculate the volume 16 | }; 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_10/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str) 15 | : Box {lv, wv, hv}, material{ str } 16 | { 17 | } 18 | 19 | // Function to calculate the volume of a Carton object 20 | double volume() const override 21 | { 22 | const double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 23 | return vol > 0.0? vol : 0.0; 24 | } 25 | }; 26 | #endif 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_10/Ex14_10.cpp: -------------------------------------------------------------------------------- 1 | // Using an abstract class 2 | #include 3 | #include "Box.h" // For the Box class 4 | #include "ToughPack.h" // For the ToughPack class 5 | #include "Carton.h" // For the Carton class 6 | 7 | int main() 8 | { 9 | ToughPack hardcase {20.0, 30.0, 40.0}; // A derived box - same size 10 | Carton carton {20.0, 30.0, 40.0, "plastic"}; // A different derived box 11 | 12 | Box*pBox {&hardcase}; // Base pointer - derived address 13 | std::cout << "hardcase volume is " << pBox->volume() << std::endl; 14 | 15 | pBox = &carton; // New derived address 16 | std::cout << "carton volume is " << pBox->volume() << std::endl; 17 | } 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_10/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | protected: 13 | // Function to calculate volume of a ToughPack allowing 15% for packing 14 | double volume() const override { return 0.85 * length*width*height; } 15 | // double volume() const override { return 0.85 * Box::volume(); } // cannot call pure virtual base class function! 16 | }; 17 | #endif 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_11/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include "Vessel.h" 4 | 5 | class Box : public Vessel 6 | { 7 | protected: 8 | double length {1.0}; 9 | double width {1.0}; 10 | double height {1.0}; 11 | 12 | public: 13 | Box() = default; 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 15 | 16 | double volume() const override { return length*width*height; } 17 | }; 18 | #endif 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_11/Can.h: -------------------------------------------------------------------------------- 1 | // Can.h Class defining a cylindrical can of a given height and diameter 2 | #ifndef CAN_H 3 | #define CAN_H 4 | #include "Vessel.h" 5 | 6 | // A macro defining the mathematical constant p 7 | #define PI 3.141592653589793238462643383279502884 8 | 9 | class Can : public Vessel 10 | { 11 | protected: 12 | double diameter {1.0}; 13 | double height {1.0}; 14 | 15 | public: 16 | Can(double d, double h) : diameter {d}, height {h} {} 17 | 18 | double volume() const override { return PI * diameter * diameter * height / 4.0; } 19 | }; 20 | #endif 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_11/Carton.h: -------------------------------------------------------------------------------- 1 | #ifndef CARTON_H 2 | #define CARTON_H 3 | #include 4 | #include 5 | #include "Box.h" 6 | 7 | class Carton : public Box 8 | { 9 | private: 10 | std::string material; 11 | 12 | public: 13 | // Constructor explicitly calling the base constructor 14 | Carton(double lv, double wv, double hv, std::string_view str = "material") 15 | : Box {lv, wv, hv}, material{ str } 16 | { 17 | } 18 | 19 | // Function to calculate the volume of a Carton object 20 | double volume() const override 21 | { 22 | const double vol {(length - 0.5)*(width - 0.5)*(height - 0.5)}; 23 | return vol > 0.0 ? vol : 0.0; 24 | } 25 | }; 26 | #endif 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_11/Ex14_11.cpp: -------------------------------------------------------------------------------- 1 | // Using an interface class and indirect base classes 2 | #include 3 | #include // For the vector container 4 | #include "Box.h" // For the Box class 5 | #include "ToughPack.h" // For the ToughPack class 6 | #include "Carton.h" // For the Carton class 7 | #include "Can.h" // for the Can class 8 | 9 | int main() 10 | { 11 | Box box {40, 30, 20}; 12 | Can can {10, 3}; 13 | Carton carton {40, 30, 20, "Plastic"}; 14 | ToughPack hardcase {40, 30, 20}; 15 | 16 | std::vector vessels {&box, &can, &carton, &hardcase}; 17 | 18 | for (const auto* vessel : vessels) 19 | std::cout << "Volume is " << vessel->volume() << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_11/ToughPack.h: -------------------------------------------------------------------------------- 1 | #ifndef TOUGHPACK_H 2 | #define TOUGHPACK_H 3 | 4 | #include "Box.h" 5 | 6 | class ToughPack : public Box 7 | { 8 | public: 9 | // Constructor 10 | ToughPack(double lv, double wv, double hv) : Box {lv, wv, hv} {} 11 | 12 | // Function to calculate volume of a ToughPack allowing 15% for packing 13 | double volume() const override { return 0.85 * Box::volume(); } 14 | }; 15 | #endif 16 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 14/Ex14_11/Vessel.h: -------------------------------------------------------------------------------- 1 | // Vessel.h Abstract class defining a vessel 2 | #ifndef VESSEL_H 3 | #define VESSEL_H 4 | 5 | class Vessel 6 | { 7 | public: 8 | virtual ~Vessel() = default; 9 | virtual double volume() const = 0; 10 | }; 11 | #endif 12 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_01/Ex15_01.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beg-cplusplus17/b86344273b1c4c51a597995b979a984e92dab022/Beginning C++ 17 source code/Examples/Chapter 15/Ex15_01/Ex15_01.cpp -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_03/Ex15_03.cpp: -------------------------------------------------------------------------------- 1 | // Throw an exception object 2 | #include 3 | #include "MyTroubles.h" 4 | 5 | void trySomething(int i); 6 | 7 | int main() 8 | { 9 | for (int i {}; i < 2; ++i) 10 | { 11 | try 12 | { 13 | trySomething(i); 14 | } 15 | catch (const Trouble& t) 16 | { 17 | // What seems to be the trouble? 18 | std::cout << "Exception: " << t.what() << std::endl; 19 | } 20 | } 21 | } 22 | 23 | void trySomething(int i) 24 | { 25 | // There's always trouble when attempting trying something here... 26 | if (i == 0) 27 | throw Trouble {}; 28 | else 29 | throw Trouble {"Nobody knows the trouble I've seen..."}; 30 | } 31 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_03/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception class definition 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | 14 | std::string_view what() const { return message; } 15 | }; 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_04/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception classes 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | virtual ~Trouble() = default; // Base classes must always have a virtual destructor! 14 | 15 | virtual std::string_view what() const { return message; } 16 | }; 17 | 18 | // Derived exception class 19 | class MoreTrouble : public Trouble 20 | { 21 | public: 22 | MoreTrouble(std::string_view str = "There's more trouble...") : Trouble {str} {} 23 | }; 24 | 25 | // Derived exception class 26 | class BigTrouble : public MoreTrouble 27 | { 28 | public: 29 | BigTrouble(std::string_view str = "Really big trouble...") : MoreTrouble {str} {} 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_05/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception classes 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | virtual ~Trouble() = default; // Base classes must always have a virtual destructor! 14 | 15 | virtual std::string_view what() const { return message; } 16 | }; 17 | 18 | // Derived exception class 19 | class MoreTrouble : public Trouble 20 | { 21 | public: 22 | MoreTrouble(std::string_view str = "There's more trouble...") : Trouble {str} {} 23 | }; 24 | 25 | // Derived exception class 26 | class BigTrouble : public MoreTrouble 27 | { 28 | public: 29 | BigTrouble(std::string_view str = "Really big trouble...") : MoreTrouble {str} {} 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_06/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception classes 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | virtual ~Trouble() = default; // Base classes must always have a virtual destructor! 14 | 15 | virtual std::string_view what() const { return message; } 16 | }; 17 | 18 | // Derived exception class 19 | class MoreTrouble : public Trouble 20 | { 21 | public: 22 | MoreTrouble(std::string_view str = "There's more trouble...") : Trouble {str} {} 23 | }; 24 | 25 | // Derived exception class 26 | class BigTrouble : public MoreTrouble 27 | { 28 | public: 29 | BigTrouble(std::string_view str = "Really big trouble...") : MoreTrouble {str} {} 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_07/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception classes 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | virtual ~Trouble() = default; // Base classes must always have a virtual destructor! 14 | 15 | virtual std::string_view what() const { return message; } 16 | }; 17 | 18 | // Derived exception class 19 | class MoreTrouble : public Trouble 20 | { 21 | public: 22 | MoreTrouble(std::string_view str = "There's more trouble...") : Trouble {str} {} 23 | }; 24 | 25 | // Derived exception class 26 | class BigTrouble : public MoreTrouble 27 | { 28 | public: 29 | BigTrouble(std::string_view str = "Really big trouble...") : MoreTrouble {str} {} 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_08/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception classes 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | virtual ~Trouble() = default; // Base classes must always have a virtual destructor! 14 | 15 | virtual std::string_view what() const noexcept { return message; } 16 | }; 17 | 18 | // Derived exception class 19 | class MoreTrouble : public Trouble 20 | { 21 | public: 22 | MoreTrouble(std::string_view str = "There's more trouble...") : Trouble {str} {} 23 | }; 24 | 25 | // Derived exception class 26 | class BigTrouble : public MoreTrouble 27 | { 28 | public: 29 | BigTrouble(std::string_view str = "Really big trouble...") : MoreTrouble {str} {} 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_08A/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception classes 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | virtual ~Trouble() = default; // Base classes must always have a virtual destructor! 14 | 15 | virtual std::string_view what() const noexcept { return message; } 16 | }; 17 | 18 | // Derived exception class 19 | class MoreTrouble : public Trouble 20 | { 21 | public: 22 | MoreTrouble(std::string_view str = "There's more trouble...") : Trouble {str} {} 23 | }; 24 | 25 | // Derived exception class 26 | class BigTrouble : public MoreTrouble 27 | { 28 | public: 29 | BigTrouble(std::string_view str = "Really big trouble...") : MoreTrouble {str} {} 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_08B/MyTroubles.h: -------------------------------------------------------------------------------- 1 | // MyTroubles.h Exception classes 2 | #ifndef MYTROUBLES_H 3 | #define MYTROUBLES_H 4 | #include 5 | #include 6 | 7 | class Trouble 8 | { 9 | private: 10 | std::string message; 11 | public: 12 | Trouble(std::string_view str = "There's a problem") : message {str} {} 13 | virtual ~Trouble() = default; // Base classes must always have a virtual destructor! 14 | 15 | virtual std::string_view what() const noexcept { return message; } 16 | }; 17 | 18 | // Derived exception class 19 | class MoreTrouble : public Trouble 20 | { 21 | public: 22 | MoreTrouble(std::string_view str = "There's more trouble...") : Trouble {str} {} 23 | }; 24 | 25 | // Derived exception class 26 | class BigTrouble : public MoreTrouble 27 | { 28 | public: 29 | BigTrouble(std::string_view str = "Really big trouble...") : MoreTrouble {str} {} 30 | }; 31 | 32 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_09/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | #include // For std::min() function template 4 | #include "Dimension_error.h" 5 | 6 | class Box 7 | { 8 | private: 9 | double length {1.0}; 10 | double width {1.0}; 11 | double height {1.0}; 12 | 13 | public: 14 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} 15 | { 16 | if (lv <= 0.0 || wv <= 0.0 || hv <= 0.0) 17 | throw dimension_error{ std::min( {lv, wv, hv} ) }; 18 | } 19 | 20 | double volume() const { return length*width*height; } 21 | }; 22 | #endif 23 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_09/Dimension_error.h: -------------------------------------------------------------------------------- 1 | #ifndef DIMENSION_ERROR_H 2 | #define DIMENSION_ERROR_H 3 | #include // For derived exception classes such as std::out_of_range 4 | #include // For std::to_string() and the std::string type 5 | 6 | class dimension_error : public std::out_of_range 7 | { 8 | private: 9 | double value; 10 | public: 11 | explicit dimension_error(double dim) 12 | : std::out_of_range{"Zero or negative dimension: " + std::to_string(dim)}, value{dim} {} 13 | 14 | // Function to obtain the invalid dimension value 15 | double getValue() const noexcept { return value; } 16 | }; 17 | #endif 18 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 15/Ex15_09/Ex15_09.cpp: -------------------------------------------------------------------------------- 1 | // Using an exception class 2 | #include 3 | #include "Box.h" // For the Box class 4 | #include "Dimension_error.h" // For the dimension_error class 5 | 6 | int main() 7 | { 8 | try 9 | { 10 | Box box1 {1.0, 2.0, 3.0}; 11 | std::cout << "box1 volume is " << box1.volume() << std::endl; 12 | Box box2 {1.0, -2.0, 3.0}; 13 | std::cout << "box2 volume is " << box2.volume() << std::endl; 14 | } 15 | catch (const std::exception& ex) 16 | { 17 | std::cout << "Exception caught in main(): " << ex.what() << std::endl; 18 | } 19 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 16/Ex16_01/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length {1.0}; 8 | double width {1.0}; 9 | double height {1.0}; 10 | 11 | public: 12 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 13 | Box() = default; 14 | 15 | double volume() const { return length*width*height; } 16 | }; 17 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 16/Ex16_01A/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length {1.0}; 8 | double width {1.0}; 9 | double height {1.0}; 10 | 11 | public: 12 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 13 | Box() = default; 14 | 15 | double volume() const { return length*width*height; } 16 | }; 17 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 16/Ex16_02/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length {1.0}; 8 | double width {1.0}; 9 | double height {1.0}; 10 | 11 | public: 12 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 13 | Box() = default; 14 | 15 | double volume() const { return length*width*height; } 16 | }; 17 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 16/Ex16_03/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | protected: 7 | double length {1.0}; 8 | double width {1.0}; 9 | double height {1.0}; 10 | 11 | public: 12 | Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv} {} 13 | // Box() = default; 14 | 15 | double volume() const { return length*width*height; } 16 | }; 17 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 16/Ex16_05/Ex16_05.cpp: -------------------------------------------------------------------------------- 1 | // Disambiguating dependant names: this code will not compile. 2 | // You can uncomment the /*typename*/ to make it compile. 3 | 4 | template 5 | class Outer 6 | { 7 | public: 8 | class Nested { /* ... */ }; 9 | 10 | Nested getNested() const; 11 | }; 12 | 13 | template 14 | /*typename*/ Outer::Nested Outer::getNested() const 15 | { 16 | return Nested{ /* ... */ }; 17 | } 18 | 19 | template 20 | void foo(/* ... */) 21 | { 22 | Outer outer; 23 | Outer::Nested nested = outer.getNested(); 24 | } 25 | 26 | int main() 27 | { 28 | } 29 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 17/Ex17_03/Ex17_03.cpp: -------------------------------------------------------------------------------- 1 | // Defining and using a move assignment operator 2 | #include "Array.h" 3 | #include 4 | 5 | // Construct an Array<> of a given size, filled with some arbitrary string data 6 | Array buildStringArray(const size_t size) 7 | { 8 | Array result{ size }; 9 | for (size_t i = 0; i < size; ++i) 10 | result[i] = "You should learn from your competitor, but never copy. Copy and you die."; 11 | return result; 12 | } 13 | 14 | int main() 15 | { 16 | Array strings { 123 }; 17 | strings = buildStringArray(1'000); // Assign an rvalue to strings 18 | 19 | Array more_strings{ 2'000 }; 20 | strings = more_strings; // Assign an lvalue to strings 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 17/Ex17_04/Ex17_04.cpp: -------------------------------------------------------------------------------- 1 | // Using std::move() to force the move assignment of a named variable 2 | #include "Array.h" 3 | #include 4 | 5 | // Construct an Array<> of a given size, filled with some arbitrary string data 6 | Array buildStringArray(const size_t size) 7 | { 8 | Array result{ size }; 9 | for (size_t i = 0; i < size; ++i) 10 | result[i] = "You should learn from your competitor, but never copy. Copy and you die."; 11 | return result; 12 | } 13 | 14 | int main() 15 | { 16 | Array strings { 123 }; 17 | strings = buildStringArray(1'000); // Assign an rvalue to strings 18 | 19 | Array more_strings{ 2'000 }; 20 | strings = std::move(more_strings); // Move more_strings into strings 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 17/Ex17_05/Ex17_05.cpp: -------------------------------------------------------------------------------- 1 | // Exercising two overloads of push_back(): one for lvalue arguments, and one for rvalue arguments 2 | #include "Array.h" 3 | #include 4 | 5 | // Construct an Array<> of a given size, filled with some arbitrary string data 6 | Array buildStringArray(const size_t size) 7 | { 8 | Array result{ size }; 9 | for (size_t i = 0; i < size; ++i) 10 | result[i] = "You should learn from your competitor, but never copy. Copy and you die."; 11 | return result; 12 | } 13 | 14 | int main() 15 | { 16 | Array lvalue = buildStringArray(1'000); 17 | 18 | Array> array1; 19 | array1.push_back(lvalue); // Push an lvalue 20 | 21 | Array> array2; 22 | array2.push_back(std::move(lvalue)); // Push an rvalue 23 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 17/Ex17_06/Ex17_06.cpp: -------------------------------------------------------------------------------- 1 | // Use of a pass-by-value parameter to pass by either lvalue or rvalue 2 | #include "Array.h" 3 | #include 4 | 5 | // Construct an Array<> of a given size, filled with some arbitrary string data 6 | Array buildStringArray(const size_t size) 7 | { 8 | Array result{ size }; 9 | for (size_t i = 0; i < size; ++i) 10 | result[i] = "You should learn from your competitor, but never copy. Copy and you die."; 11 | return result; 12 | } 13 | 14 | int main() 15 | { 16 | Array lvalue = buildStringArray(1'000); 17 | 18 | Array> array1; 19 | array1.push_back(lvalue); // Push an lvalue 20 | 21 | Array> array2; 22 | array2.push_back(std::move(lvalue)); // Push an rvalue 23 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 17/Ex17_07/Ex17_07.cpp: -------------------------------------------------------------------------------- 1 | // The effect of not adding noexcept to move members 2 | #include "Array.h" 3 | #include 4 | #include 5 | 6 | // Construct an Array<> of a given size, filled with some arbitrary string data 7 | Array buildStringArray(const size_t size) 8 | { 9 | Array result{ size }; 10 | for (size_t i = 0; i < size; ++i) 11 | result[i] = "You should learn from your competitor, but never copy. Copy and you die."; 12 | return result; 13 | } 14 | 15 | int main() 16 | { 17 | std::vector> v; 18 | v.push_back(buildStringArray(1'000)); 19 | v.push_back(buildStringArray(2'000)); 20 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 17/Ex17_08/Ex17_08.cpp: -------------------------------------------------------------------------------- 1 | // The effect of not adding noexcept to move members 2 | #include "Array.h" 3 | #include 4 | #include 5 | 6 | // Construct an Array<> of a given size, filled with some arbitrary string data 7 | Array buildStringArray(const size_t size) 8 | { 9 | Array result{ size }; 10 | for (size_t i = 0; i < size; ++i) 11 | result[i] = "You should learn from your competitor, but never copy. Copy and you die."; 12 | return result; 13 | } 14 | 15 | int main() 16 | { 17 | Array lvalue = buildStringArray(1'000); 18 | 19 | std::vector> v; 20 | v.push_back(lvalue); // Push an lvalue 21 | v.push_back(std::move(lvalue)); // Push an rvalue 22 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_01/Ex18_01.cpp: -------------------------------------------------------------------------------- 1 | // Exercising pointers to functions 2 | #include 3 | 4 | long sum(long a, long b); // Function prototype 5 | long product(long a, long b); // Function prototype 6 | 7 | int main() 8 | { 9 | long(*do_it)(long, long) {}; // Pointer to function 10 | 11 | do_it = product; 12 | std::cout << "3 * 5 = " << do_it(3, 5) << std::endl; // Call product thru a pointer 13 | 14 | do_it = sum; // Reassign pointer to sum() 15 | std::cout << "3 * (4+5) + 6 = " 16 | << do_it(product(3, do_it(4, 5)), 6) << std::endl; // Call thru a pointer twice 17 | } 18 | 19 | // Function to multiply two values 20 | long product(long a, long b) { return a * b; } 21 | 22 | // Function to add two values 23 | long sum(long a, long b) { return a + b; } 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_02/Optimum.h: -------------------------------------------------------------------------------- 1 | // Optimum.h - a function template to determine the optimum element in a given vector 2 | #ifndef OPTIMUM_H 3 | #define OPTIMUM_H 4 | 5 | #include // For std::size_t (required here by some compilers) 6 | #include 7 | 8 | template 9 | const T* find_optimum(const std::vector& values, bool (*compare)(const T&, const T&)) 10 | { 11 | if (values.empty()) return nullptr; 12 | 13 | const T* optimum = &values[0]; 14 | for (size_t i = 1; i < values.size(); ++i) 15 | { 16 | if (compare(values[i], *optimum)) 17 | { 18 | optimum = &values[i]; 19 | } 20 | } 21 | return optimum; 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_03/Ex18_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercising the use of a functor as callback functions 2 | #include 3 | #include "Optimum.h" 4 | #include "Less.h" 5 | 6 | template 7 | bool greater(const T& one, const T& other) { return one > other; } 8 | 9 | int main() 10 | { 11 | Less less; // Create a 'less than' functor 12 | 13 | std::vector numbers{ 91, 18, 92, 22, 13, 43 }; 14 | std::cout << "Minimum element: " << *find_optimum(numbers, less) << std::endl; 15 | std::cout << "Maximum element: " << *find_optimum(numbers, greater) << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_03/Less.cpp: -------------------------------------------------------------------------------- 1 | // Definition of a basic function call operator 2 | #include "Less.h" 3 | 4 | bool Less::operator()(int a, int b) const 5 | { 6 | return a < b; 7 | } 8 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_03/Less.h: -------------------------------------------------------------------------------- 1 | // Less.h - A basic class of functor objects 2 | #ifndef LESS_H 3 | #define LESS_H 4 | 5 | class Less 6 | { 7 | public: 8 | bool operator()(int a, int b) const; 9 | }; 10 | 11 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_03/Optimum.h: -------------------------------------------------------------------------------- 1 | // Optimum.h - a function template to determine the optimum element in a given vector 2 | #ifndef OPTIMUM_H 3 | #define OPTIMUM_H 4 | 5 | #include // For std::size_t (required here by some compilers) 6 | #include 7 | 8 | template 9 | const T* find_optimum(const std::vector& values, Comparison compare) 10 | { 11 | if (values.empty()) return nullptr; 12 | 13 | const T* optimum = &values[0]; 14 | for (size_t i = 1; i < values.size(); ++i) 15 | { 16 | if (compare(values[i], *optimum)) 17 | { 18 | optimum = &values[i]; 19 | } 20 | } 21 | return optimum; 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_03A/Ex18_03A.cpp: -------------------------------------------------------------------------------- 1 | // Exercising the use of standard functors 2 | #include 3 | #include 4 | #include "Optimum.h" 5 | 6 | int main() 7 | { 8 | std::vector numbers{ 91, 18, 92, 22, 13, 43 }; 9 | std::cout << "Minimum element: " << *find_optimum(numbers, std::less<>{}) << std::endl; 10 | std::cout << "Maximum element: " << *find_optimum(numbers, std::greater<>{}) << std::endl; 11 | } 12 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_03A/Optimum.h: -------------------------------------------------------------------------------- 1 | // Optimum.h - a function template to determine the optimum element in a given vector 2 | #ifndef OPTIMUM_H 3 | #define OPTIMUM_H 4 | 5 | #include // For std::size_t (required here by some compilers) 6 | #include 7 | 8 | template 9 | const T* find_optimum(const std::vector& values, Comparison compare) 10 | { 11 | if (values.empty()) return nullptr; 12 | 13 | const T* optimum = &values[0]; 14 | for (size_t i = 1; i < values.size(); ++i) 15 | { 16 | if (compare(values[i], *optimum)) 17 | { 18 | optimum = &values[i]; 19 | } 20 | } 21 | return optimum; 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_04/Ex18_04.cpp: -------------------------------------------------------------------------------- 1 | // Exercising a function object with a member variable 2 | #include 3 | #include "Optimum.h" 4 | #include "Nearer.h" 5 | 6 | int main() 7 | { 8 | std::vector numbers{ 91, 18, 92, 22, 13, 43 }; 9 | 10 | int number_to_search_for {}; 11 | std::cout << "Please enter a number: "; 12 | std::cin >> number_to_search_for; 13 | 14 | std::cout << "The number nearest to " << number_to_search_for << " is " 15 | << *find_optimum(numbers, Nearer{ number_to_search_for }) << std::endl; 16 | } 17 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_04/Nearer.h: -------------------------------------------------------------------------------- 1 | // A class of function objects that compare two values based on how close they are 2 | // to some third value that was provided to the functor at construction time. 3 | #ifndef NEARER_H 4 | #define NEARER_H 5 | 6 | #include // For std::abs() 7 | 8 | class Nearer 9 | { 10 | public: 11 | Nearer(int value) : n(value) {} 12 | bool operator()(int x, int y) const { return std::abs(x - n) < std::abs(y - n); } 13 | private: 14 | int n; 15 | }; 16 | 17 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_04/Optimum.h: -------------------------------------------------------------------------------- 1 | // Optimum.h - a function template to determine the optimum element in a given vector 2 | #ifndef OPTIMUM_H 3 | #define OPTIMUM_H 4 | 5 | #include // For std::size_t (required here by some compilers) 6 | #include 7 | 8 | template 9 | const T* find_optimum(const std::vector& values, Comparison compare) 10 | { 11 | if (values.empty()) return nullptr; 12 | 13 | const T* optimum = &values[0]; 14 | for (size_t i = 1; i < values.size(); ++i) 15 | { 16 | if (compare(values[i], *optimum)) 17 | { 18 | optimum = &values[i]; 19 | } 20 | } 21 | return optimum; 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_05/Optimum.h: -------------------------------------------------------------------------------- 1 | // Optimum.h - a function template to determine the optimum element in a given vector 2 | #ifndef OPTIMUM_H 3 | #define OPTIMUM_H 4 | 5 | #include // For std::size_t (required here by some compilers) 6 | #include 7 | 8 | template 9 | const T* find_optimum(const std::vector& values, Comparison compare) 10 | { 11 | if (values.empty()) return nullptr; 12 | 13 | const T* optimum = &values[0]; 14 | for (size_t i = 1; i < values.size(); ++i) 15 | { 16 | if (compare(values[i], *optimum)) 17 | { 18 | optimum = &values[i]; 19 | } 20 | } 21 | return optimum; 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_06/Ex18_06.cpp: -------------------------------------------------------------------------------- 1 | // Using a default capture-by-value clause to access a local variable 2 | // from within the body of a lambda expression. 3 | #include 4 | #include "Optimum.h" 5 | 6 | int main() 7 | { 8 | std::vector numbers{ 91, 18, 92, 22, 13, 43 }; 9 | 10 | int number_to_search_for {}; 11 | std::cout << "Please enter a number: "; 12 | std::cin >> number_to_search_for; 13 | 14 | auto nearer { 15 | [=](int x, int y) { 16 | return std::abs(x - number_to_search_for) < std::abs(y - number_to_search_for); 17 | } 18 | }; 19 | std::cout << "The number nearest to " << number_to_search_for << " is " 20 | << *find_optimum(numbers, nearer) << std::endl; 21 | } 22 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_06/Optimum.h: -------------------------------------------------------------------------------- 1 | // Optimum.h - a function template to determine the optimum element in a given vector 2 | #ifndef OPTIMUM_H 3 | #define OPTIMUM_H 4 | 5 | #include // For std::size_t (required here by some compilers) 6 | #include 7 | 8 | template 9 | const T* find_optimum(const std::vector& values, Comparison compare) 10 | { 11 | if (values.empty()) return nullptr; 12 | 13 | const T* optimum = &values[0]; 14 | for (size_t i = 1; i < values.size(); ++i) 15 | { 16 | if (compare(values[i], *optimum)) 17 | { 18 | optimum = &values[i]; 19 | } 20 | } 21 | return optimum; 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_07/Ex18_07.cpp: -------------------------------------------------------------------------------- 1 | // Using a lambda expression from inside a member function (see Finder.cpp) 2 | #include 3 | #include "Optimum.h" 4 | #include "Finder.h" 5 | 6 | int main() 7 | { 8 | std::vector numbers{ 91, 18, 92, 22, 13, 43 }; 9 | 10 | int number_to_search_for {}; 11 | std::cout << "Please enter a number: "; 12 | std::cin >> number_to_search_for; 13 | 14 | Finder finder; 15 | finder.setNumberToSearchFor(number_to_search_for); 16 | std::cout << "The number nearest to " << finder.getNumberToSearchFor() << " is " 17 | << *finder.findNearest(numbers) << std::endl; 18 | } 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_07/Finder.cpp: -------------------------------------------------------------------------------- 1 | // Exercising capturing the this pointer 2 | #include "Finder.h" 3 | #include "Optimum.h" 4 | #include // For std::abs() 5 | 6 | int Finder::getNumberToSearchFor() const 7 | { 8 | return number_to_search_for; 9 | } 10 | 11 | void Finder::setNumberToSearchFor(int value) 12 | { 13 | number_to_search_for = value; 14 | } 15 | 16 | std::optional Finder::findNearest(const std::vector& values) const 17 | { 18 | if (values.empty()) 19 | return std::nullopt; 20 | else 21 | return *find_optimum(values, [this](int x, int y) { 22 | return std::abs(x - number_to_search_for) < std::abs(y - number_to_search_for); 23 | }); 24 | } 25 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_07/Finder.h: -------------------------------------------------------------------------------- 1 | // Finder.h - A small class to illustrate the use of lambda expression in member functions 2 | #ifndef FINDER_H 3 | #define FINDER_H 4 | 5 | #include 6 | #include 7 | 8 | class Finder 9 | { 10 | public: 11 | int getNumberToSearchFor() const; 12 | void setNumberToSearchFor(int n); 13 | 14 | std::optional findNearest(const std::vector& values) const; 15 | private: 16 | int number_to_search_for {}; 17 | }; 18 | 19 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_07/Optimum.h: -------------------------------------------------------------------------------- 1 | // Optimum.h - a function template to determine the optimum element in a given vector 2 | #ifndef OPTIMUM_H 3 | #define OPTIMUM_H 4 | 5 | #include // For std::size_t (required here by some compilers) 6 | #include 7 | 8 | template 9 | const T* find_optimum(const std::vector& values, Comparison compare) 10 | { 11 | if (values.empty()) return nullptr; 12 | 13 | const T* optimum = &values[0]; 14 | for (size_t i = 1; i < values.size(); ++i) 15 | { 16 | if (compare(values[i], *optimum)) 17 | { 18 | optimum = &values[i]; 19 | } 20 | } 21 | return optimum; 22 | } 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 18/Ex18_08/Ex18_08.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beg-cplusplus17/b86344273b1c4c51a597995b979a984e92dab022/Beginning C++ 17 source code/Examples/Chapter 18/Ex18_08/Ex18_08.cpp -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 19/Ex19_01/Ex19_01.cpp: -------------------------------------------------------------------------------- 1 | // Working with std::deque<> 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::deque my_deque; // A deque<> allows efficient insertions to 8 | my_deque.push_back(2); // both ends of the sequence 9 | my_deque.push_back(4); 10 | my_deque.push_front(1); 11 | 12 | my_deque[2] = 3; // A deque<> is a random-access sequence container 13 | 14 | std::cout << "There are " << my_deque.size() << " elements in my_deque: "; 15 | 16 | for (int element : my_deque) // A deque<>, like all containers, is a range 17 | std::cout << element << ' '; 18 | std::cout << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 19/Ex19_02/Ex19_02.cpp: -------------------------------------------------------------------------------- 1 | // Working with stacks and queues 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::stack stack; 9 | for (int i{}; i < 10; ++i) 10 | stack.push(i); 11 | 12 | std::cout << "The elements coming of the top of the stack: "; 13 | while (!stack.empty()) 14 | { 15 | std::cout << stack.top() << ' '; 16 | stack.pop(); // pop() is a void function! 17 | } 18 | std::cout << std::endl; 19 | 20 | std::queue queue; 21 | for (int i{}; i < 10; ++i) 22 | queue.push(i); 23 | 24 | std::cout << "The elements coming from the front of the queue: "; 25 | while (!queue.empty()) 26 | { 27 | std::cout << queue.front() << ' '; 28 | queue.pop(); // pop() is a void function! 29 | } 30 | std::cout << std::endl; 31 | } 32 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 19/Ex19_04/Ex19_04.cpp: -------------------------------------------------------------------------------- 1 | // Basic use of std::map<> 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::map phone_book; 9 | phone_book["Donald Trump"] = 202'456'1111; 10 | phone_book["Melania Trump"] = 202'456'1111; 11 | phone_book["Francis"] = 39'06'6982; 12 | phone_book["Elizabeth"] = 44'020'7930'4832; 13 | 14 | std::cout << "The president's number is " << phone_book["Donald Trump"] << std::endl; 15 | 16 | for (const auto&[name, number] : phone_book) 17 | std::cout << name << " can be reached at " << number << std::endl; 18 | } 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 19/Ex19_07/Ex19_07.cpp: -------------------------------------------------------------------------------- 1 | // Iterating over the elements of a list<> 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | std::cout << "Enter a sequence of positive numbers, " 8 | << "terminated by a negative number: "; 9 | 10 | std::list numbers; 11 | 12 | while (true) 13 | { 14 | signed number{-1}; 15 | std::cin >> number; 16 | if (number < 0) break; 17 | numbers.push_back(static_cast(number)); 18 | } 19 | 20 | std::cout << "You entered the following numbers: "; 21 | for (auto iter {numbers.begin()}; iter != numbers.end(); ++iter) 22 | { 23 | std::cout << *iter << ' '; 24 | } 25 | std::cout << std::endl; 26 | } 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 19/Ex19_08/Box.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Box.h" 3 | 4 | // Constructor definition 5 | Box::Box(double lv, double wv, double hv) 6 | : length{lv}, width{wv}, height{hv} 7 | { 8 | } 9 | 10 | // Function to calculate the volume of a box 11 | double Box::volume() 12 | { 13 | return length * width * height; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 19/Ex19_08/Box.h: -------------------------------------------------------------------------------- 1 | #ifndef BOX_H 2 | #define BOX_H 3 | 4 | class Box 5 | { 6 | private: 7 | double length{ 1.0 }; 8 | double width{ 1.0 }; 9 | double height{ 1.0 }; 10 | 11 | public: 12 | // Constructors 13 | Box() = default; 14 | Box(double length, double width, double height); 15 | 16 | double volume(); // Function to calculate the volume of a box 17 | 18 | // Functions to provide access to the values of member variables 19 | double getLength() { return length; } 20 | double getWidth() { return width; } 21 | double getHeight() { return height; } 22 | 23 | // Functions to set member variable values 24 | void setLength(double lv) { if (lv > 0) length = lv; } 25 | void setWidth(double wv) { if (wv > 0) width = wv; } 26 | void setHeight(double hv) { if (hv > 0) height = hv; } 27 | }; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Examples/Chapter 19/Ex19_08/Ex19_08.cpp: -------------------------------------------------------------------------------- 1 | // Altering elements through a mutable iterator 2 | #include 3 | #include 4 | #include "Box.h" // From Ex11_04 5 | 6 | int main() 7 | { 8 | std::vector boxes{ Box{ 1.0, 2.0, 3.0 } }; // A vector containing 1 Box 9 | 10 | auto iter{ boxes.begin() }; 11 | std::cout << iter->volume() << std::endl; // 6 == 1.0 * 2.0 * 3.0 12 | 13 | *iter = Box{ 2.0, 3.0, 4.0 }; 14 | std::cout << iter->volume() << std::endl; // 24 == 2.0 * 3.0 * 4.0 15 | 16 | iter->setHeight(7.0); 17 | std::cout << iter->volume() << std::endl; // 42 == 2.0 * 3.0 * 7.0 18 | } 19 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 01/Exer1_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 1-3. Spot the errors. 2 | // The correct version should closely resemble the answer to exercise 1.1. 3 | 4 | include // # character missing before include 5 | 6 | Int main() // Should be int, not Int 7 | { 8 | std:cout << "Hello World" << std:endl // A semicolon is missing from the end of this line 9 | // cout and endl must be prefixed with std::, not std: 10 | ) -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 01/Soln1_01.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 1-1 Writing the line "Hello World" to the screen. 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | std::cout << "Hello World" << std::endl; 8 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 01/Soln1_02.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 1-2. Write your name and age on successive lines. 2 | // There are several possibilities. You can do it in one statement for example. 3 | // You could also output '\n' to go to a new line. 4 | 5 | #include 6 | 7 | int main() 8 | { 9 | std::cout << "Phil McCavity" << std::endl; // Name 10 | std::cout << "Age: 88" << std::endl; // and age 11 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 02/Soln2_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 2-3. Converting a length in inches to feet-and-inches. 2 | #include 3 | 4 | int main() 5 | { 6 | const int inches_per_foot {12}; 7 | 8 | long length_inches {}; 9 | 10 | std::cout << "This program converts inches into feet-and-inches.\n"; 11 | std::cout << "Please enter the number of inches: "; 12 | std::cin >> length_inches; 13 | 14 | const long feet {length_inches / inches_per_foot}; 15 | const long inches {length_inches % inches_per_foot}; 16 | 17 | std::cout << "In " << length_inches << " inches there are " 18 | << feet << " feet and " << inches << " inch(es)." << std::endl; 19 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 03/Soln3_06.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 3-5. Swapping integers. 2 | 3 | #include 4 | 5 | int main() 6 | { 7 | int first {}, second {}; 8 | std::cout << "Enter two integers separated by a space: "; 9 | std::cin >> first >> second; 10 | 11 | first ^= second; 12 | second ^= first; 13 | first ^= second; 14 | std::cout << "In reverse order they are " << first << " and " << second << std::endl; 15 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 04/Soln4_01.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 4-1 Testing whether two integer values are equal. 2 | 3 | #include 4 | 5 | int main() { 6 | int value1 {}; 7 | int value2 {}; 8 | 9 | std::cout << "Please input two integers, separated by a space: "; 10 | std::cin >> value1 >> value2; 11 | std::cout << std::endl; 12 | 13 | if (value1 == value2) 14 | std::cout << "The values you entered are the same (two times " << value1 << ")." << std::endl; 15 | else 16 | std::cout << "The values you entered are not the same (" << value1 << " != " << value2 << ")." << std::endl; 17 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 04/Soln4_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 4-2 Using nested ifs and a logical && to check the value of a number. 2 | 3 | #include 4 | 5 | int main() { 6 | double value {}; 7 | 8 | std::cout << "Please enter a number between 1 and 100: "; 9 | std::cin >> value; 10 | std::cout << std::endl; 11 | 12 | if (value >= 1 && value <= 100) 13 | { 14 | std::cout << "The number you entered is "; 15 | 16 | if (value > 50) 17 | std::cout << "greater than 50"; 18 | else if (value < 50) 19 | std::cout << "less than 50" << std::endl; 20 | else 21 | std::cout << "50" << std::endl; 22 | 23 | std::cout << '.' << std::endl; 24 | } 25 | else 26 | { 27 | std::cout << "The number is not between 1 and 100." << std::endl; 28 | } 29 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 04/Soln4_04.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beg-cplusplus17/b86344273b1c4c51a597995b979a984e92dab022/Beginning C++ 17 source code/Exercises/Chapter 04/Soln4_04.cpp -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 04/Soln4_06.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 4-4 Finding the range for an integer. 2 | // This is just a question of bolting sufficent conditional operators together 3 | // in an expression. 4 | 5 | #include 6 | using std::cin; 7 | using std::cout; 8 | using std::endl; 9 | 10 | int main() 11 | { 12 | int n {}; 13 | std::cout << "Enter an integer: "; 14 | std::cin >> n; 15 | 16 | std::cout << "The value is " 17 | << (n <= 20 ? "not greater than 20" 18 | : n <= 30 ? "greater than 20 and not greater than 30" 19 | : n <= 100? "greater than 30 and not exceeding 100" 20 | : "greater than 100") 21 | << '.' << std::endl; 22 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 05/Soln5_01.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 5-1 Squaring odd numbers 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int limit {}; 8 | std::cout << "Enter the upper limit for squared odd numbers: "; 9 | std::cin >> limit; 10 | for (int i {1}; i <= limit; i += 2) 11 | { 12 | std::cout << std::setw(4) << i << " squared is " << std::setw(8) << i * i << std::endl; 13 | } 14 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 05/Soln5_02.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 5-2 Summing integers and calculating the average 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | unsigned int count {}; 9 | long long total {}; 10 | 11 | while (true) 12 | { 13 | std::cout << "Enter an integer: "; 14 | int n; 15 | std::cin >> n; 16 | total += n; 17 | ++count; 18 | 19 | char yesno {}; 20 | std::cout << "Do you want to enter another (y/n)?"; 21 | std::cin >> yesno; 22 | 23 | if (std::tolower(yesno) == 'n') 24 | break; 25 | } 26 | 27 | std::cout << "The total is " << total << std::endl 28 | << "The average is " << std::setw(10) << std::setprecision(2) 29 | << std::fixed << (static_cast(total) / count) << std::endl; 30 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 05/Soln5_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 5-3 Using a do-while loop to count characters 2 | #include 3 | 4 | int main() 5 | { 6 | unsigned count {}; 7 | char ch {}; 8 | 9 | std::cout << "Please enter a sequence of characters terminated by '#':" << std::endl; 10 | 11 | // We have to read at least one character so do-while is best 12 | do { 13 | std::cin >> ch; 14 | ++count; 15 | } while (ch != '#'); 16 | 17 | // We do not count '#' as a character, so count must be adjusted 18 | --count; 19 | std::cout << "You entered " << count << " characters (not counting spaces and the terminal #)." << std::endl; 20 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 05/Soln5_05.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 5-5 Print out characters entered by the user *after* reversing them 2 | #include 3 | 4 | int main() 5 | { 6 | const size_t max_num_characters {1'000}; 7 | char string[max_num_characters]; 8 | 9 | std::cout << "Please enter a string: "; 10 | std::cin.getline(string, max_num_characters); 11 | 12 | // Count the number of characters 13 | size_t count {}; 14 | while (count < max_num_characters && string[count] != '\0') 15 | ++count; 16 | 17 | // Reverse the characters of the string entered by the user 18 | for (size_t i = 0; i < count / 2; ++i) 19 | { 20 | char temp = string[i]; 21 | string[i] = string[count - i - 1]; 22 | string[count - i - 1] = temp; 23 | } 24 | 25 | // Print out all characters, one by one 26 | for (size_t i = 0; i < count; ++i) 27 | { 28 | std::cout << string[i]; 29 | } 30 | std::cout << std::endl; 31 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 05/Soln5_06.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 5-6. Working with a vector container 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | std::cout << "What is the largest number I should check? "; 9 | unsigned bound {}; 10 | std::cin >> bound; 11 | 12 | std::vector values; 13 | // Add element values 1 to bound 14 | for (unsigned i {1}; i <= bound; ++i) 15 | values.push_back(i); 16 | 17 | size_t count {}; // Number of output values 18 | size_t perline {10}; // Number output perline 19 | for (auto value : values) 20 | { 21 | if (value % 7 == 0 || value % 13 == 0) continue; 22 | std::cout << std::setw(6) << value; 23 | if (++count % perline == 0) std::cout << "\n"; 24 | } 25 | std::cout << std::endl; 26 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 05/Soln5_08.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 5-08. Generate 93 Fibonacci numbers stored in an array. 2 | // 93 was not an arbitrary choice for the number of Fibonacci numbers. 3 | // Fibonacci number grow fairly rapidly. 4 | // 93 is the most that are possible with type unsigned long long. 5 | 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | const size_t n {93}; 12 | std::array fib; 13 | fib[0] = fib[1] = 1UL; 14 | for (size_t i {2}; i < n; ++i) 15 | fib[i] = fib[i - 1] + fib[i - 2]; 16 | 17 | std::cout << "The first " << n << " Fibonacci numbers are:\n"; 18 | for (auto number : fib) 19 | { 20 | std::cout << number << std::endl; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 06/Soln6_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 6-3. Storing numbers in a dynamic array 2 | #include 3 | #include 4 | #include 5 | 6 | int main() 7 | { 8 | size_t n {}; 9 | std::cout << "Enter the number of array elements: "; 10 | std::cin >> n; 11 | auto* values = new double[n]; 12 | for (size_t i {}; i < n; ++i) 13 | *(values+i) = 1.0 / ((i + 1)*(i + 1)); 14 | 15 | double sum {}; 16 | for (size_t i {}; i < n; ++i) 17 | sum += values[i]; 18 | 19 | std::cout << "The result is " << std::setprecision(15) << std::sqrt(6.0 * sum) << std::endl; 20 | 21 | delete[] values; // Don't forget to free the memory! 22 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 06/Soln6_06.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 6-6. Storing a dynamically allocated vector in a smart pointer 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | size_t n {}; 12 | std::cout << "Enter the number of vector elements: "; 13 | std::cin >> n; 14 | auto values = std::make_unique>(n); 15 | 16 | for (size_t i {}; i < n; ++i) 17 | (*values)[i] = 1.0 / ((i + 1)*(i + 1)); 18 | 19 | double sum {}; 20 | for (auto value : *values) 21 | sum += value; 22 | 23 | std::cout << "Result is " << std::setprecision(15) << std::sqrt(6.0*sum) << std::endl; 24 | 25 | // No need to deallocate the memory yourself anymore: the smart pointer takes care of that for you! 26 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 07/Soln7_07.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 7-7 2 | // Relatively easy exercise to practice using string streams to parse given strings 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | std::string numbers; 11 | std::cout << "Enter a sequence of numbers terminated by #:\n"; 12 | std::getline(std::cin, numbers, '#'); 13 | 14 | long sum {}; 15 | 16 | std::stringstream ss{ numbers }; 17 | while (ss) // true as long as the stream is non-empty 18 | { 19 | int i {}; // Extract a number and add it to the sum 20 | ss >> i; 21 | sum += i; 22 | } 23 | 24 | std::cout << "The sum of the numbers you entered is: " << sum << std::endl; 25 | } 26 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 08/Soln8_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 8_3 Checking the number of arguments entered at the command line. 2 | #include 3 | 4 | int main(int numArguments, char* arguments[]) 5 | { 6 | switch (numArguments - 1) 7 | { 8 | case 2: case 3: case 4: 9 | for (size_t i {1}; i < numArguments; ++i) 10 | std::cout << "Argument " << i << " is " << arguments[i] << std::endl; 11 | break; 12 | default: 13 | std::cout << "You entered the incorrect number of arguments.\n" 14 | << "Please enter 2, 3 or 4 arguments. " << std::endl; 15 | } 16 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 08/Soln8_07.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 8-7 Computing Fibinacci numbers recursively. 2 | // Main thing to realise is that the recursion needs two base cases. 3 | // Key is also to use unsigned values (function would fail for negative numbers) 4 | // and not to forget about zero either (using n == 1 and n == 2 as base cases 5 | // would mean trouble if n == 0 is passed) 6 | #include 7 | 8 | unsigned long long fib(size_t n); 9 | 10 | int main() 11 | { 12 | size_t num{}; 13 | std::cout << "Good day, master. How many Fibonacci numbers shall I compute today?" << std::endl; 14 | std::cin >> num; 15 | 16 | for (size_t i{1}; i <= num; ++i) 17 | std::cout << "fib(" << i << ") = " << fib(i) << '\n'; 18 | } 19 | 20 | unsigned long long fib(size_t n) 21 | { 22 | switch (n) 23 | { 24 | case 0: return 0; 25 | case 1: return 1; 26 | default: return fib(n-2) + fib(n-1); 27 | } 28 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 08/Soln8_08.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 8_8 More efficient recursive version of function for x to the power n, n positive or negative 2 | // Based on Ex8_17.cpp 3 | #include 4 | #include 5 | 6 | long double power(double x, int n); 7 | 8 | int main() 9 | { 10 | for (int i {-3}; i <= 3; ++i) // Calculate powers of 8 from -3 to +3 11 | std::cout << std::setw(10) << power(8.0, i); 12 | 13 | std::cout << std::endl; 14 | } 15 | 16 | // Recursive function to calculate x to the power n 17 | long double power(double x, int n) 18 | { 19 | if (n == 0) return 1.0; 20 | else if (n < 0) return 1.0 / power(x, -n); 21 | else if (n % 2) return x * power(x, n-1); // x is odd 22 | 23 | // If we make it this far, x > 0 and even 24 | const auto y = power(x, n/2); 25 | return y * y; 26 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 08/Soln8_09.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 8_9 Count the number of multiplications performed by 2 | // the divide and conquer power() function of Soln8_09.cpp 3 | #include 4 | #include 5 | 6 | long double power(double x, int n); 7 | 8 | int main() 9 | { 10 | std::cout << power(1.5, 1000) << std::endl; 11 | } 12 | 13 | inline auto mult(long double l, long double r) 14 | { 15 | static size_t count{}; 16 | std::cout << ++count << " multiplications" << std::endl; 17 | return l * r; 18 | } 19 | 20 | // Recursive function to calculate x to the power n 21 | long double power(double x, int n) 22 | { 23 | if (n == 0) return 1.0; 24 | else if (n < 0) return 1.0 / power(x, -n); 25 | else if (n % 2) return mult(x, power(x, n-1)); // x is odd 26 | 27 | // If we make it this far, x > 0 and even 28 | const auto y = power(x, n/2); 29 | return mult(y, y); 30 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_01/main.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-1 Using multiple files and preprocessor directives. 2 | // We have seven files in all. 3 | // The file main.cpp only needs to #include two of our three new header files, 4 | // since all it does is call the functions print_this() and print_that(): 5 | 6 | #include "printthis.h" 7 | #include "printthat.h" 8 | 9 | int main() 10 | { 11 | print_this("This is a test string using print_this()."); 12 | print_that("This is a test string using print_that()."); 13 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_01/print.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-1 Using multiple files and preprocessor directives. 2 | // We have seven files in all. 3 | // The file print.cpp contains the definition for the function print(). 4 | #include "print.h" 5 | #include 6 | 7 | void print(std::string_view s) 8 | { 9 | std::cout << s << std::endl; 10 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_01/print.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-1 Using multiple files and preprocessor directives. 2 | // We have seven files in all. 3 | // The file print.h contains the prototype for the function print(). 4 | #ifndef PRINT_H 5 | #define PRINT_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_01/printthat.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-1 Using multiple files and preprocessor directives. 2 | // We have seven files in all. 3 | // The file printthat.cpp contains the definition for the function print_that(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthat.h" 7 | #include "print.h" 8 | 9 | void print_that(std::string_view s) 10 | { 11 | print(s); 12 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_01/printthat.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-1 Using multiple files and preprocessor directives. 2 | // We have seven files in all. 3 | // The file printthat.h contains the prototype for the function print_that(). 4 | #ifndef PRINTTHAT_H 5 | #define PRINTTHAT_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_that(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_01/printthis.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-1 Using multiple files and preprocessor directives. 2 | // We have seven files in all. 3 | // The file printthis.cpp contains the definition for the function print_this(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthis.h" 7 | #include "print.h" 8 | 9 | void print_this(std::string_view s) 10 | { 11 | print(s); 12 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_01/printthis.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-1 Using multiple files and preprocessor directives. 2 | // We have seven files in all. 3 | // The file printthis.h contains the prototype for the function print_this(). 4 | #ifndef PRINTTHIS_H 5 | #define PRINTTHIS_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_this(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_02/main.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-2 Using an external variable (one that counts the amount of times print() is called). 2 | // We have seven files in all. 3 | // The file main.cpp only needs to #include two of our three new header files, 4 | // since all it does is call the functions print_this() and print_that(): 5 | 6 | #include "printthis.h" 7 | #include "printthat.h" 8 | #include "print.h" 9 | #include 10 | 11 | int main() 12 | { 13 | print_this("This is a test string using print_this()."); 14 | print_that("This is a test string using print_that()."); 15 | std::cout << "print() has been called " << call_count 16 | << (call_count == 1? " time" : " times") << '.' << std::endl; 17 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_02/print.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-2 Using an external variable (one that counts the amount of times print() is called). 2 | // We have seven files in all. 3 | // The file print.cpp contains the definition for the function print(). 4 | #include "print.h" 5 | #include 6 | 7 | // The one definition of the variable call_count (initialized to zero). 8 | int call_count {}; // Note: the {} is optional for definitions of global variables; 9 | // if you omit the braces, the variable shall still be initialized to zero. 10 | // We highly recommend you include the braces though for clarity. 11 | 12 | void print(std::string_view s) 13 | { 14 | ++call_count; 15 | std::cout << s << std::endl; 16 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_02/print.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-2 Using an external variable (one that counts the amount of times print() is called). 2 | // We have seven files in all. 3 | // The file print.h contains the prototype for the function print(). 4 | #ifndef PRINT_H 5 | #define PRINT_H 6 | 7 | #include 8 | 9 | // Declaration of an external variable call_count. 10 | extern int call_count; 11 | 12 | // Function prototype 13 | void print(std::string_view s); 14 | 15 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_02/printthat.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-2 Using an external variable (one that counts the amount of times print() is called). 2 | // We have seven files in all. 3 | // The file printthat.cpp contains the definition for the function print_that(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthat.h" 7 | #include "print.h" 8 | 9 | void print_that(std::string_view s) 10 | { 11 | print(s); 12 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_02/printthat.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-2 Using an external variable (one that counts the amount of times print() is called). 2 | // We have seven files in all. 3 | // The file printthat.h contains the prototype for the function print_that(). 4 | #ifndef PRINTTHAT_H 5 | #define PRINTTHAT_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_that(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_02/printthis.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-2 Using an external variable (one that counts the amount of times print() is called). 2 | // We have seven files in all. 3 | // The file printthis.cpp contains the definition for the function print_this(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthis.h" 7 | #include "print.h" 8 | 9 | void print_this(std::string_view s) 10 | { 11 | print(s); 12 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_02/printthis.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-2 Using an external variable (one that counts the amount of times print() is called). 2 | // We have seven files in all. 3 | // The file printthis.h contains the prototype for the function print_this(). 4 | #ifndef PRINTTHIS_H 5 | #define PRINTTHIS_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_this(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_03/main.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file main.cpp only needs to #include two of our three new header files, 4 | // since all it does is call the functions print_this() and print_that(): 5 | 6 | #include "printthis.h" 7 | #include "printthat.h" 8 | #include "print.h" 9 | #include 10 | 11 | int main() 12 | { 13 | print_this("This is a test string using print_this()."); 14 | print_that("This is a test string using print_that()."); 15 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_03/print.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file print.cpp contains the definition for the function print(). 4 | #include "print.h" 5 | #include 6 | 7 | void print1::print(std::string_view s) 8 | { 9 | std::cout << "print1: " << s << std::endl; 10 | } 11 | 12 | void print2::print(std::string_view s) 13 | { 14 | std::cout << "print2: " << s << std::endl; 15 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_03/print.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file print.h contains the prototype for the function print(). 4 | #ifndef PRINT_H 5 | #define PRINT_H 6 | 7 | #include 8 | 9 | namespace print1 10 | { 11 | // Function prototype 12 | void print(std::string_view s); 13 | } 14 | 15 | namespace print2 16 | { 17 | // Function prototype 18 | void print(std::string_view s); 19 | } 20 | 21 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_03/printthat.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthat.cpp contains the definition for the function print_that(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthat.h" 7 | #include "print.h" 8 | 9 | using print2::print; 10 | 11 | void print_that(std::string_view s) 12 | { 13 | print(s); 14 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_03/printthat.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthat.h contains the prototype for the function print_that(). 4 | #ifndef PRINTTHAT_H 5 | #define PRINTTHAT_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_that(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_03/printthis.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthis.cpp contains the definition for the function print_this(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthis.h" 7 | #include "print.h" 8 | 9 | void print_this(std::string_view s) 10 | { 11 | print1::print(s); 12 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_03/printthis.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthis.h contains the prototype for the function print_this(). 4 | #ifndef PRINTTHIS_H 5 | #define PRINTTHIS_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_this(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_04/main.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file main.cpp only needs to #include two of our three new header files, 4 | // since all it does is call the functions print_this() and print_that(): 5 | 6 | #include "printthis.h" 7 | #include "printthat.h" 8 | #include "print.h" 9 | #include 10 | 11 | #define DO_THIS 12 | 13 | int main() 14 | { 15 | #ifdef DO_THIS 16 | print_this("This is a test string using print_this()."); 17 | #else 18 | print_that("This is a test string using print_that()."); 19 | #endif 20 | } 21 | 22 | // Alternative solution: 23 | /* 24 | #ifdef DO_THIS 25 | #define myprint(X) print_this(X) 26 | #else 27 | #define myprint(X) print_that(X) 28 | #endif 29 | 30 | int main() 31 | { 32 | my_print("This is a test string using print_this()."); 33 | } 34 | */ -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_04/print.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file print.cpp contains the definition for the function print(). 4 | #include "print.h" 5 | #include 6 | 7 | void print1::print(std::string_view s) 8 | { 9 | std::cout << "print1: " << s << std::endl; 10 | } 11 | 12 | void print2::print(std::string_view s) 13 | { 14 | std::cout << "print2: " << s << std::endl; 15 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_04/print.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file print.h contains the prototype for the function print(). 4 | #ifndef PRINT_H 5 | #define PRINT_H 6 | 7 | #include 8 | 9 | namespace print1 10 | { 11 | // Function prototype 12 | void print(std::string_view s); 13 | } 14 | 15 | namespace print2 16 | { 17 | // Function prototype 18 | void print(std::string_view s); 19 | } 20 | 21 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_04/printthat.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthat.cpp contains the definition for the function print_that(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthat.h" 7 | #include "print.h" 8 | 9 | using print2::print; 10 | 11 | void print_that(std::string_view s) 12 | { 13 | print(s); 14 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_04/printthat.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthat.h contains the prototype for the function print_that(). 4 | #ifndef PRINTTHAT_H 5 | #define PRINTTHAT_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_that(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_04/printthis.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthis.cpp contains the definition for the function print_this(). 4 | // Each of the files printthis.cpp and printthat.cpp must #include the file print.h, 5 | // as well as its own header file: 6 | #include "printthis.h" 7 | #include "print.h" 8 | 9 | void print_this(std::string_view s) 10 | { 11 | print1::print(s); 12 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 10/Soln10_04/printthis.h: -------------------------------------------------------------------------------- 1 | // Exercise 10-3 Using namespaces 2 | // We have seven files in all. 3 | // The file printthis.h contains the prototype for the function print_this(). 4 | #ifndef PRINTTHIS_H 5 | #define PRINTTHIS_H 6 | 7 | #include 8 | 9 | // Function prototype 10 | void print_this(std::string_view s); 11 | 12 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 11/Soln11_01/Integer.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 11-1 Integer.cpp 2 | 3 | #include 4 | #include "Integer.h" 5 | 6 | Integer::Integer(int m) : n{m} 7 | { 8 | std::cout << "Object created." << std::endl; 9 | } 10 | 11 | void Integer::show() const 12 | { 13 | std::cout << "Value is " << n << std::endl; 14 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 11/Soln11_01/Integer.h: -------------------------------------------------------------------------------- 1 | // Exercise 11-1 Integer.h 2 | 3 | #ifndef INTEGER_H 4 | #define INTEGER_H 5 | 6 | class Integer 7 | { 8 | private: 9 | int n; 10 | 11 | public: 12 | Integer(int m); 13 | int getValue() const { return n; } 14 | void setValue(int m) { n = m; } 15 | void show() const; 16 | }; 17 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 11/Soln11_03/Integer.h: -------------------------------------------------------------------------------- 1 | // Exercise 11-3 Integer.h 2 | #ifndef INTEGER_H 3 | #define INTEGER_H 4 | class Integer 5 | { 6 | private: 7 | int n; 8 | 9 | public: 10 | Integer(int m = 0); 11 | Integer(const Integer& obj); 12 | int compare(const Integer& obj) const; 13 | int getValue() const { return n; } 14 | void setValue(int m) { n = m; } 15 | void show() const; 16 | 17 | Integer& add(const Integer& obj); 18 | Integer& subtract(const Integer& obj); 19 | Integer& multiply(const Integer& obj); 20 | }; 21 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 11/Soln11_04/Integer.h: -------------------------------------------------------------------------------- 1 | // Exercise 11-4 Integer.h 2 | 3 | #ifndef INTEGER_H 4 | #define INTEGER_H 5 | 6 | class Integer 7 | { 8 | private: 9 | int n; 10 | 11 | public: 12 | Integer(int m = 0); 13 | Integer(const Integer& obj); 14 | friend int compare(const Integer& obj1, const Integer& obj2); // friend compare function 15 | int getValue() const { return n; } 16 | void setValue(int m) { n = m; } 17 | void show() const; 18 | }; 19 | 20 | // A non-friend function that implements the same function 21 | int nonFriendCompare(const Integer& obj1, const Integer& obj2); 22 | 23 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 11/Soln11_05/Integer.h: -------------------------------------------------------------------------------- 1 | // Exercise 11-5 Integer.h 2 | 3 | #ifndef INTEGER_H 4 | #define INTEGER_H 5 | 6 | class Integer 7 | { 8 | private: 9 | int n; 10 | static unsigned int count; 11 | 12 | public: 13 | Integer(int m = 0); // Contructor with given value 14 | Integer(const Integer& obj); // Copy constructor 15 | ~Integer(); // Destructor 16 | int compare(const Integer& obj) const; // Compare function with reference parameter 17 | int getValue() const { return n; } 18 | void setValue(int m) { n = m; } 19 | void show() const; 20 | 21 | static void printCount(); 22 | }; 23 | 24 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 12/Soln12_01/Soln12_01.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 12-1 2 | // Implementing the * operator for the Box class 3 | // to post-multiply by an integer 4 | #include 5 | #include "Box.h" 6 | 7 | int main() 8 | { 9 | Box box {2, 3, 4}; 10 | std::cout << "Box is " << box << std::endl; 11 | unsigned n {3}; 12 | Box newBox = box * n; 13 | std::cout << "After multiplying by " << n << " box is " << newBox << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 12/Soln12_02/Soln12_02.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 12-3 2 | // Implementing the * operator for the Box class 3 | // to pre-multiply by an integer 4 | #include 5 | #include "Box.h" 6 | 7 | int main() 8 | { 9 | Box box {2, 3, 4}; 10 | std::cout << "Box is " << box << std::endl; 11 | unsigned n {3}; 12 | Box newBox = n * box; 13 | std::cout << "After pre-multiplying by " << n << " box is " << newBox << std::endl; 14 | } 15 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 12/Soln12_03/Soln12_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 12-3 2 | // Implementing the missing /, *=, +=, and /= operators for the Box class 3 | #include 4 | #include "Box.h" 5 | 6 | int main() 7 | { 8 | Box box {2, 3, 4}; 9 | std::cout << "Box is " << box << std::endl; 10 | size_t n {3}; 11 | box *= 3; 12 | std::cout << "After multiplying by " << n << " box is " << box << std::endl; 13 | box /= 3; 14 | std::cout << "After dividing by " << n << ", the box is again " << box << std::endl; 15 | 16 | Box newBox {2 * box}; 17 | std::cout << "Twice " << box << " is " << newBox << std::endl; 18 | 19 | std::cout << "Half that is again " << (newBox / 2) << std::endl; 20 | 21 | std::cout << "Adding both boxes gives " << (box + newBox) << std::endl; 22 | 23 | box += newBox; 24 | 25 | std::cout << "The same can be obtained by usign += as well: " << box << std::endl; 26 | } 27 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 12/Soln12_04/Soln12_04.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 12-4 2 | // Implementing operator for the Box class to allow it to be used in if statements. 3 | // A Box is "true" if and only if its volume is non-zero. 4 | #include 5 | #include "Box.h" 6 | 7 | void testBox(const Box& box) 8 | { 9 | std::cout << "The box's volume is " << box.volume() << ".\n"; 10 | if (box) 11 | std::cout << "This volume is non-zero."; 12 | if (!box) 13 | std::cout << "This volume is zero."; 14 | std::cout << std::endl; 15 | } 16 | 17 | int main() 18 | { 19 | Box box1{2, 3, 4}; 20 | std::cout << "box1 is " << box1 << std::endl; 21 | testBox(box1); 22 | 23 | std::cout << std::endl;; 24 | 25 | Box box2{0, 0, 0}; 26 | std::cout << "box2 is " << box2 << std::endl; 27 | testBox(box2); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 13/Soln13_01/Animals.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 13-1 - Animals.cpp 2 | // Implementations of the Animal class and classes derived from Animal 3 | 4 | #include 5 | #include "Animals.h" 6 | 7 | // Constructor 8 | Animal::Animal(std::string_view _name, int _weight) 9 | : name{_name}, weight{_weight} {} 10 | 11 | // Identify the animal 12 | void Animal::who() const 13 | { 14 | std::cout << "My name is " << name << " and I weigh " << weight << "lbs." << std::endl; 15 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 13/Soln13_01/Soln13_01.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 13-1 Exercising the Animal classes 2 | #include "Animals.h" 3 | 4 | int main() 5 | { 6 | Lion myLion{"Leo", 400}; 7 | Aardvark myAardvark{"Algernon", 50}; 8 | myLion.who(); 9 | myAardvark.who(); 10 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 13/Soln13_02/Animals.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 13-2 - Animals.cpp 2 | // Implementations of the Animal class and classes derived from Animal 3 | 4 | #include 5 | #include "Animals.h" 6 | 7 | // Constructor 8 | Animal::Animal(std::string_view _name, int _weight) 9 | : name{_name}, weight{_weight} {} 10 | 11 | // Identify the animal 12 | void Animal::who() const 13 | { 14 | std::cout << "My name is " << name << " and I weigh " << weight << "lbs." << std::endl; 15 | } 16 | 17 | // Identify the Lion 18 | void Lion::who() const 19 | { 20 | std::cout << "Rrroarrrr... I am a lion. "; 21 | Animal::who(); // Call protected base function 22 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 13/Soln13_02/Soln13_02.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 13-2 The who() function for the base class has the protected access specifier, 2 | // so we ensure the derived classes allow public access to the who() function. 3 | 4 | #include "Animals.h" 5 | 6 | int main() 7 | { 8 | Lion myLion{"Leo", 400}; 9 | Aardvark myAardvark{"Algernon", 50}; 10 | myLion.who(); 11 | myAardvark.who(); 12 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 13/Soln13_03/Animals.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 13-3 - Animals.cpp 2 | // Implementations of the Animal class and classes derived from Animal 3 | 4 | #include 5 | #include "Animals.h" 6 | 7 | // Constructor 8 | Animal::Animal(std::string_view _name, int _weight) 9 | : name{_name}, weight{_weight} {} 10 | 11 | // Identify the animal 12 | void Animal::who() const 13 | { 14 | std::cout << "My name is " << name << " and I weigh " << weight << "lbs." << std::endl; 15 | } 16 | 17 | // Identify the Lion 18 | void Lion::who() const 19 | { 20 | std::cout << "Rrroarrrr... I am a lion. "; 21 | Animal::who(); // Call base function 22 | } 23 | 24 | // Identify the Aardvark 25 | void Aardvark::who() const 26 | { 27 | Animal::who(); // Call base function 28 | std::cout << "Oh. And I'm an Aardvark. " << std::endl; 29 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 13/Soln13_03/Soln13_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 13-3 By addng a few lines to the test program, we can see the difference 2 | // between the calls to the base class and derived class who() functions. 3 | 4 | #include 5 | #include "Animals.h" 6 | 7 | int main() 8 | { 9 | Lion myLion{"Leo", 400}; 10 | Aardvark myAardvark{"Algernon", 50}; 11 | std::cout << "Calling derived versions of who():\n"; 12 | myLion.who(); 13 | myAardvark.who(); 14 | 15 | std::cout << "\nCalling base versions of who():\n"; 16 | myLion.Animal::who(); 17 | myAardvark.Animal::who(); 18 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 14/Soln14_01/Animals.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 14-1 Animals.cpp 2 | // Implementations of the Animal class and classes derived from Animal 3 | 4 | #include "Animals.h" 5 | 6 | // Constructor 7 | Animal::Animal(std::string_view theName, unsigned wt) 8 | : name(theName), weight(wt) 9 | {} 10 | 11 | // Return string describing the animal 12 | std::string Animal::who() const 13 | { 14 | return "My name is " + name + ". My weight is " + std::to_string(weight) + " lbs."; 15 | } 16 | 17 | // Make like a sheep 18 | std::string_view Sheep::sound() const 19 | { 20 | return "Baaaa!!"; 21 | } 22 | 23 | // Make like a dog 24 | std::string_view Dog::sound() const 25 | { 26 | return "Woof woof!!"; 27 | } 28 | 29 | // Make like a cow 30 | std::string_view Cow::sound() const 31 | { 32 | return "Mooooo!!"; 33 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 14/Soln14_01/Zoo.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 14-1 Zoo.cpp 2 | // Implementations of the Zoo class that stores pointers to Animals 3 | #include "Zoo.h" 4 | #include "Animals.h" 5 | #include 6 | 7 | // Constructor from a vector of animals 8 | Zoo::Zoo(const std::vector& new_animals) : animals {new_animals} {} 9 | 10 | // Add an animal to the zoo 11 | void Zoo::addAnimal(AnimalPtr animal) 12 | { 13 | animals.push_back(animal); 14 | } 15 | 16 | // Output the animals and the sound they make 17 | void Zoo::showAnimals() const 18 | { 19 | for (auto animal : animals) 20 | { 21 | std::cout << animal->who() << ' ' << animal->sound() << std::endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 14/Soln14_02/Zoo.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 14-2 Zoo.cpp 2 | // Implementations of the Zoo class that stores pointers to Animals 3 | #include "Zoo.h" 4 | #include "Animals.h" 5 | #include 6 | 7 | // Constructor from a vector of animals 8 | Zoo::Zoo(const std::vector& new_animals) : animals {new_animals} {} 9 | 10 | // Add an animal to the zoo 11 | void Zoo::addAnimal(AnimalPtr animal) 12 | { 13 | animals.push_back(animal); 14 | } 15 | 16 | // Output the animals and the sound they make 17 | void Zoo::showAnimals() const 18 | { 19 | for (auto animal : animals) 20 | { 21 | std::cout << animal->who() << ' ' << animal->sound() << std::endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 14/Soln14_03/Zoo.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 14-3 Zoo.cpp 2 | // Implementations of the Zoo class that stores pointers to Animals 3 | #include "Zoo.h" 4 | #include "Animals.h" 5 | #include 6 | 7 | // Constructor from a vector of animals 8 | Zoo::Zoo(const std::vector& new_animals) : animals {new_animals} {} 9 | 10 | // Add an animal to the zoo 11 | void Zoo::addAnimal(AnimalPtr animal) 12 | { 13 | animals.push_back(animal); 14 | } 15 | 16 | // Output the animals and the sound they make 17 | void Zoo::showAnimals() const 18 | { 19 | for (auto animal : animals) 20 | { 21 | std::cout << animal->who() << ' ' << animal->sound() << std::endl; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 14/Soln14_06/Point.h: -------------------------------------------------------------------------------- 1 | // Exercise 14-6 Point.h 2 | // A simple class for 2-dimensional points 3 | #ifndef POINT_H 4 | #define POINT_H 5 | 6 | class Point final 7 | { 8 | public: 9 | Point(double inX, double inY) : x(inX), y(inY) {} 10 | 11 | double getX() const { return x; } 12 | double getY() const { return y; } 13 | 14 | void setX(double inX) { x = inX; } 15 | void setY(double inY) { y = inY; } 16 | 17 | private: 18 | double x; 19 | double y; 20 | }; 21 | 22 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Exer15_05/Customer.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.cpp 2 | // A simple C++ customer class 3 | 4 | #include "Customer.h" 5 | 6 | Customer::Customer( 7 | std::string_view aSurname, 8 | std::string_view aName, 9 | std::string_view aStreet, 10 | int aStreetNumber, 11 | std::string_view aCity) 12 | : surname(aSurname) 13 | , name(aName) 14 | , street(aStreet) 15 | , streetNumber(aStreetNumber) 16 | , city(aCity) 17 | {} 18 | 19 | std::string Customer::ToString() const 20 | { 21 | std::string result; 22 | result += surname; 23 | result += ' '; 24 | result += name; 25 | result += ", "; 26 | result += street; 27 | result += ' '; 28 | result += std::to_string(streetNumber); 29 | result += ", "; 30 | result += city; 31 | return result; 32 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Exer15_05/Customer.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.h 2 | // A simple C++ customer class 3 | 4 | #ifndef CUSTOMER_H 5 | #define CUSTOMER_H 6 | 7 | #include 8 | #include 9 | 10 | class Customer 11 | { 12 | public: 13 | Customer( 14 | std::string_view surname, 15 | std::string_view name, 16 | std::string_view street, 17 | int streetNumber, 18 | std::string_view city 19 | ); 20 | 21 | std::string ToString() const; 22 | 23 | private: 24 | std::string surname; 25 | std::string name; 26 | std::string street; 27 | int streetNumber; 28 | std::string city; 29 | }; 30 | 31 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Exer15_05/DBException.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 DBException.h 2 | // A simple C++ exception type 3 | 4 | #ifndef DB_EXCEPTION_H 5 | #define DB_EXCEPTION_H 6 | 7 | #include 8 | 9 | class DataBaseException : public std::runtime_error 10 | { 11 | public: 12 | using std::runtime_error::runtime_error; // Inherit constructor 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_01/CurveBall.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-1 CurveBall.cpp 2 | // Implementation of CurveBall exception class 3 | 4 | #include "CurveBall.h" 5 | 6 | const char* CurveBall::what() const noexcept 7 | { 8 | return "CurveBall exception"; 9 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_01/CurveBall.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-1 CurveBall.h 2 | // Definition of CurveBall exception class 3 | 4 | #ifndef CURVEBALL_H 5 | #define CURVEBALL_H 6 | #include 7 | 8 | class CurveBall : public std::exception 9 | { 10 | public: 11 | const char* what() const noexcept override; 12 | }; 13 | 14 | #endif //CURVEBALL_H -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_02/CurveBall.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-1 CurveBall.cpp 2 | // Implementation of CurveBall exception class 3 | 4 | #include "CurveBall.h" 5 | 6 | const char* CurveBall::what() const noexcept 7 | { 8 | return "CurveBall exception"; 9 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_02/CurveBall.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-1 CurveBall.h 2 | // Definition of CurveBall exception class 3 | 4 | #ifndef CURVEBALL_H 5 | #define CURVEBALL_H 6 | #include 7 | 8 | class CurveBall : public std::exception 9 | { 10 | public: 11 | const char* what() const noexcept override; 12 | }; 13 | 14 | #endif //CURVEBALL_H -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_02/TooManyExceptions.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-2 TooManyExceptions.cpp 2 | // Implementation of TooManyExceptions exception class 3 | 4 | #include "TooManyExceptions.h" 5 | 6 | const char* TooManyExceptions::what() const noexcept 7 | { 8 | return "TooManyExceptions exception"; 9 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_02/TooManyExceptions.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-2 TooManyExceptions.h 2 | // Definition of TooManyExceptions exception class 3 | 4 | #ifndef TOOMANYEXCEPTIONS_H 5 | #define TOOMANYEXCEPTIONS_H 6 | #include 7 | 8 | class TooManyExceptions : public std::exception 9 | { 10 | public: 11 | const char* what() const noexcept override; 12 | }; 13 | 14 | #endif //TOOMANYEXCEPTIONS_H -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_04/CurveBall.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-1 CurveBall.cpp 2 | // Implementation of CurveBall exception class 3 | 4 | #include "CurveBall.h" 5 | 6 | const char* CurveBall::what() const noexcept 7 | { 8 | return "CurveBall exception"; 9 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_04/CurveBall.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-1 CurveBall.h 2 | // Definition of CurveBall exception class 3 | 4 | #ifndef CURVEBALL_H 5 | #define CURVEBALL_H 6 | #include 7 | 8 | class CurveBall : public std::exception 9 | { 10 | public: 11 | const char* what() const noexcept override; 12 | }; 13 | 14 | #endif //CURVEBALL_H -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_05/Customer.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.cpp 2 | // A simple C++ customer class 3 | 4 | #include "Customer.h" 5 | 6 | Customer::Customer( 7 | std::string_view aSurname, 8 | std::string_view aName, 9 | std::string_view aStreet, 10 | int aStreetNumber, 11 | std::string_view aCity) 12 | : surname(aSurname) 13 | , name(aName) 14 | , street(aStreet) 15 | , streetNumber(aStreetNumber) 16 | , city(aCity) 17 | {} 18 | 19 | std::string Customer::ToString() const 20 | { 21 | std::string result; 22 | result += surname; 23 | result += ' '; 24 | result += name; 25 | result += ", "; 26 | result += street; 27 | result += ' '; 28 | result += std::to_string(streetNumber); 29 | result += ", "; 30 | result += city; 31 | return result; 32 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_05/Customer.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.h 2 | // A simple C++ customer class 3 | 4 | #ifndef CUSTOMER_H 5 | #define CUSTOMER_H 6 | 7 | #include 8 | #include 9 | 10 | class Customer 11 | { 12 | public: 13 | Customer( 14 | std::string_view surname, 15 | std::string_view name, 16 | std::string_view street, 17 | int streetNumber, 18 | std::string_view city 19 | ); 20 | 21 | std::string ToString() const; 22 | 23 | private: 24 | std::string surname; 25 | std::string name; 26 | std::string street; 27 | int streetNumber; 28 | std::string city; 29 | }; 30 | 31 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 15/Soln15_05/DBException.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 DBException.h 2 | // A simple C++ exception type 3 | 4 | #ifndef DB_EXCEPTION_H 5 | #define DB_EXCEPTION_H 6 | 7 | #include 8 | 9 | class DataBaseException : public std::runtime_error 10 | { 11 | public: 12 | using std::runtime_error::runtime_error; // Inherit constructor 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 16/Soln16_01/Soln16_01.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 16-1 2 | // Adding a push_back() member function and a default constructor to the Array<> 3 | // class template. Use copy-and-swap for a memory-safe push_back(). 4 | #include "Array.h" 5 | #include 6 | 7 | int main() 8 | { 9 | const unsigned numElements = 100; 10 | 11 | Array squares; // default constructor 12 | for (unsigned i = 0; i < numElements; ++i) 13 | squares.push_back(i * i); // push_back() 14 | 15 | std::cout << squares.getSize() << " squares were added." << std::endl; 16 | std::cout << "For instance: 13 squared equals " << squares[13] << std::endl; 17 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 16/Soln16_02/Soln16_02.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 16-2 2 | // Create a basic Pair template 3 | #include "Pair.h" 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | auto my_pair = Pair(122, "abc"); 10 | ++my_pair.first; 11 | std::cout << "my_pair equals (" << my_pair.first 12 | << ", " << my_pair.second << ')' << std::endl; 13 | 14 | auto pair1 = Pair( 0, "def"); 15 | auto pair2 = Pair(123, "abc"); 16 | auto pair3 = Pair(123, "def"); 17 | 18 | std::cout << (pair1 < pair2 && pair2 < pair3? "operator< seems to be working" : "oops") << std::endl; 19 | std::cout << (pair1 == pair2? "oops" : "operator== works as well") << std::endl; 20 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 16/Soln16_03/Soln16_03.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 16-3 2 | // Create a << operator template for Pairs 3 | #include "Pair.h" 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | auto my_pair = Pair(122, "abc"); 10 | ++my_pair.first; 11 | std::cout << "my_pair equals " << my_pair << std::endl; 12 | 13 | auto pair1 = Pair( 0, "def"); 14 | auto pair2 = Pair(123, "abc"); 15 | auto pair3 = Pair(123, "def"); 16 | 17 | std::cout << (pair1 < pair2 && pair2 < pair3? "operator< seems to be working" : "oops") << std::endl; 18 | std::cout << (pair1 == pair2? "oops" : "operator== works as well") << std::endl; 19 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 17/Soln17_03/Customer.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.cpp 2 | // A simple C++ customer class 3 | 4 | #include "Customer.h" 5 | 6 | Customer::Customer( 7 | std::string_view aSurname, 8 | std::string_view aName, 9 | std::string_view aStreet, 10 | int aStreetNumber, 11 | std::string_view aCity) 12 | : surname(aSurname) 13 | , name(aName) 14 | , street(aStreet) 15 | , streetNumber(aStreetNumber) 16 | , city(aCity) 17 | {} 18 | 19 | std::string Customer::ToString() const 20 | { 21 | std::string result; 22 | result += surname; 23 | result += ' '; 24 | result += name; 25 | result += ", "; 26 | result += street; 27 | result += ' '; 28 | result += std::to_string(streetNumber); 29 | result += ", "; 30 | result += city; 31 | return result; 32 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 17/Soln17_03/Customer.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.h 2 | // A simple C++ customer class 3 | 4 | #ifndef CUSTOMER_H 5 | #define CUSTOMER_H 6 | 7 | #include 8 | #include 9 | 10 | class Customer 11 | { 12 | public: 13 | Customer( 14 | std::string_view surname, 15 | std::string_view name, 16 | std::string_view street, 17 | int streetNumber, 18 | std::string_view city 19 | ); 20 | 21 | std::string ToString() const; 22 | 23 | private: 24 | std::string surname; 25 | std::string name; 26 | std::string street; 27 | int streetNumber; 28 | std::string city; 29 | }; 30 | 31 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 17/Soln17_03/DBException.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 DBException.h 2 | // A simple C++ exception type 3 | 4 | #ifndef DB_EXCEPTION_H 5 | #define DB_EXCEPTION_H 6 | 7 | #include 8 | 9 | class DataBaseException : public std::runtime_error 10 | { 11 | public: 12 | using std::runtime_error::runtime_error; // Inherit constructor 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 17/Soln17_04/Customer.cpp: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.cpp 2 | // A simple C++ customer class 3 | 4 | #include "Customer.h" 5 | 6 | Customer::Customer( 7 | std::string_view aSurname, 8 | std::string_view aName, 9 | std::string_view aStreet, 10 | int aStreetNumber, 11 | std::string_view aCity) 12 | : surname(aSurname) 13 | , name(aName) 14 | , street(aStreet) 15 | , streetNumber(aStreetNumber) 16 | , city(aCity) 17 | {} 18 | 19 | std::string Customer::ToString() const 20 | { 21 | std::string result; 22 | result += surname; 23 | result += ' '; 24 | result += name; 25 | result += ", "; 26 | result += street; 27 | result += ' '; 28 | result += std::to_string(streetNumber); 29 | result += ", "; 30 | result += city; 31 | return result; 32 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 17/Soln17_04/Customer.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 Customer.h 2 | // A simple C++ customer class 3 | 4 | #ifndef CUSTOMER_H 5 | #define CUSTOMER_H 6 | 7 | #include 8 | #include 9 | 10 | class Customer 11 | { 12 | public: 13 | Customer( 14 | std::string_view surname, 15 | std::string_view name, 16 | std::string_view street, 17 | int streetNumber, 18 | std::string_view city 19 | ); 20 | 21 | std::string ToString() const; 22 | 23 | private: 24 | std::string surname; 25 | std::string name; 26 | std::string street; 27 | int streetNumber; 28 | std::string city; 29 | }; 30 | 31 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 17/Soln17_04/DBException.h: -------------------------------------------------------------------------------- 1 | // Exercise 15-5 DBException.h 2 | // A simple C++ exception type 3 | 4 | #ifndef DB_EXCEPTION_H 5 | #define DB_EXCEPTION_H 6 | 7 | #include 8 | 9 | class DataBaseException : public std::runtime_error 10 | { 11 | public: 12 | using std::runtime_error::runtime_error; // Inherit constructor 13 | }; 14 | 15 | #endif -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 18/Soln18_04/Collect.h: -------------------------------------------------------------------------------- 1 | // Collect.h 2 | #ifndef COLLECT_H 3 | #define COLLECT_H 4 | 5 | #include 6 | 7 | template 8 | std::vector collect(const std::vector& values, Predicate predicate) 9 | { 10 | std::vector result; 11 | 12 | for (auto& value : values) 13 | if (predicate(value)) 14 | result.push_back(value); 15 | 16 | return result; 17 | } 18 | 19 | #endif // COLLECT_H -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 18/Soln18_05/DeliveryTruck.cpp: -------------------------------------------------------------------------------- 1 | #include "DeliveryTruck.h" 2 | 3 | // Constructor 4 | DeliveryTruck::DeliveryTruck(Truckload aTruckload) 5 | : truckload{ std::move(aTruckload) } // Do not copy! 6 | {} 7 | 8 | void DeliveryTruck::deliverBox(SharedBox box) 9 | { 10 | truckload.removeBox(box); 11 | 12 | // Notify all interested parties (aka "observers") that the Box was delivered 13 | for (auto& callback : callbacks) 14 | callback(box); 15 | } 16 | 17 | void DeliveryTruck::registerOnDelivered(Callback callback) 18 | { 19 | callbacks.push_back(std::move(callback)); // Do not copy! 20 | } -------------------------------------------------------------------------------- /Beginning C++ 17 source code/Exercises/Chapter 18/Soln18_05/DeliveryTruck.h: -------------------------------------------------------------------------------- 1 | #ifndef DELIVERY_TRUCK_H 2 | #define DELIVERY_TRUCK_H 3 | 4 | #include "Truckload.h" 5 | 6 | #include // For std::function<> 7 | #include // For std::vector<> 8 | 9 | class DeliveryTruck 10 | { 11 | public: 12 | using Callback = std::function; // Type alias for the type of the delivery callback functions 13 | 14 | DeliveryTruck(Truckload truckload); // Create a delivery truck (pass-by-value to allow a Truckload to be either copied or moved!) 15 | 16 | void deliverBox(SharedBox box); 17 | 18 | void registerOnDelivered(Callback callback); 19 | 20 | private: 21 | Truckload truckload; 22 | 23 | std::vector callbacks; 24 | }; 25 | 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Apress/beg-cplusplus17/b86344273b1c4c51a597995b979a984e92dab022/LICENSE.txt -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Apress Source Code 2 | 3 | This repository accompanies [*Beginning C++17*](http://www.apress.com/9781484233658) by Ivor Horton and Peter Van Weert (Apress, 2018). 4 | 5 | [comment]: #cover 6 | 7 | 8 | Download the files as a zip using the green button, or clone the repository to your machine using Git. 9 | 10 | ## Releases 11 | 12 | Release v1.0 corresponds to the code in the published book, without corrections or updates. 13 | 14 | ## Contributions 15 | 16 | See the file Contributing.md for more information on how you can contribute to this repository. 17 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | # Contributing to Apress Source Code 2 | 3 | Copyright for Apress source code belongs to the author(s). However, under fair use you are encouraged to fork and contribute minor corrections and updates for the benefit of the author(s) and other readers. 4 | 5 | ## How to Contribute 6 | 7 | 1. Make sure you have a GitHub account. 8 | 2. Fork the repository for the relevant book. 9 | 3. Create a new branch on which to make your change, e.g. 10 | `git checkout -b my_code_contribution` 11 | 4. Commit your change. Include a commit message describing the correction. Please note that if your commit message is not clear, the correction will not be accepted. 12 | 5. Submit a pull request. 13 | 14 | Thank you for your contribution! --------------------------------------------------------------------------------