├── README.md ├── Volume 1 ├── 10. Name control │ ├── NamespaceInt.h │ ├── NamespaceMath.h │ ├── NamespaceOverriding2.h │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10 │ │ ├── ex10.cbp │ │ ├── ex10.h │ │ ├── file1.cpp │ │ ├── file2.cpp │ │ └── main.cpp │ ├── ex11 │ │ ├── ex11.cbp │ │ ├── ex11.cpp │ │ ├── ex11.h │ │ └── main.cpp │ ├── ex12 │ │ ├── ex12.cbp │ │ ├── ex12.cpp │ │ ├── ex12.h │ │ └── main.cpp │ ├── ex13 │ │ ├── ex13.cbp │ │ ├── ex13.cpp │ │ ├── ex13.h │ │ └── main.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ ├── ex16 │ │ ├── ex16.cbp │ │ ├── header1.h │ │ ├── header2.h │ │ └── main.cpp │ ├── ex17-18 │ │ ├── ex17.cbp │ │ ├── ex17.h │ │ ├── file1.cpp │ │ ├── file2.cpp │ │ └── main.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex22.cpp │ ├── ex22 │ │ ├── ex22.cbp │ │ ├── header1.h │ │ ├── header2.h │ │ └── main.cpp │ ├── ex23 │ │ ├── ex23.cbp │ │ ├── header1.h │ │ ├── header2.h │ │ └── main.cpp │ ├── ex24 │ │ ├── combined.h │ │ ├── ex24.cbp │ │ ├── header1.h │ │ ├── header2.h │ │ └── main.cpp │ ├── ex25 │ │ ├── ex25.cbp │ │ ├── ex25.h │ │ └── main.cpp │ ├── ex26.cpp │ ├── ex27.cpp │ ├── ex28.cpp │ ├── ex29.cpp │ ├── ex30.cpp │ ├── ex32 │ │ ├── Mirror.h │ │ ├── ex32.cbp │ │ ├── file1.cpp │ │ ├── file2.cpp │ │ ├── file3.cpp │ │ ├── file4.cpp │ │ └── file5.cpp │ ├── ex33 │ │ ├── Mirror.h │ │ ├── ex33.cbp │ │ ├── file1.cpp │ │ ├── file2.cpp │ │ ├── file3.cpp │ │ ├── file4.cpp │ │ └── file5.cpp │ ├── ex35.cpp │ └── statdest.out ├── 11. References&Copy │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10.cpp │ ├── ex10.s │ ├── ex15.cpp │ ├── ex16.cpp │ ├── ex17.cpp │ ├── ex18.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex22.cpp │ ├── ex24.cpp │ ├── ex25.cpp │ ├── ex26.cpp │ ├── ex27.cpp │ └── ex28.cpp ├── 12. Overloading │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex09.txt │ ├── ex10.cpp │ ├── ex11-12.cpp │ ├── ex13.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ ├── ex16.cpp │ ├── ex17.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex22.cpp │ ├── ex23.cpp │ ├── ex24.cpp │ ├── ex25.cpp │ ├── ex26.cpp │ ├── ex27.cpp │ ├── ex28.cpp │ ├── ex29.cpp │ └── ex30.cpp ├── 13. Dynamic objects │ ├── Framis.out │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03 │ │ ├── PStash.cpp │ │ ├── PStash.h │ │ ├── ex03.cbp │ │ └── main.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06 │ │ ├── PStash.cpp │ │ ├── PStash.h │ │ ├── ex06.cbp │ │ └── ex06.cpp │ ├── ex07 │ │ ├── Stack4.h │ │ ├── ex07.cbp │ │ └── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10.cpp │ ├── ex11.cpp │ ├── ex12.cpp │ ├── ex13.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ └── ex15.out ├── 14. Inheritance&Composition │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex06.out │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10.cpp │ ├── ex11 │ │ ├── AStash.h │ │ ├── PStash.cpp │ │ ├── PStash.h │ │ ├── PStashTest.cpp │ │ ├── ex11.cbp │ │ └── require.h │ ├── ex12 │ │ ├── AStash.h │ │ ├── PStash.cpp │ │ ├── PStash.h │ │ ├── PStashTest.cpp │ │ ├── ex12.cbp │ │ └── require.h │ ├── ex13.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ ├── ex16.cpp │ ├── ex17.cpp │ ├── ex18.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex22.cpp │ ├── ex23.cpp │ ├── ex24.cpp │ ├── ex25.cpp │ ├── ex26.cpp │ ├── ex27.cpp │ ├── ex28.cpp │ └── ex28.out ├── 15. Polymorphism │ ├── _OStack.h │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10.cpp │ ├── ex11.cpp │ ├── ex12.cpp │ ├── ex13.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ ├── ex16.cpp │ ├── ex17.cpp │ ├── ex18.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex21.out │ ├── ex22.cpp │ ├── ex23.cpp │ ├── ex25.cpp │ ├── ex26.cpp │ ├── ex27.cpp │ ├── ex32.cpp │ ├── ex33.cpp │ └── ex35.cpp ├── 16. Templates intro │ ├── TStack2.h │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03-04 │ │ ├── TPStash.h │ │ ├── TPStashTest.cpp │ │ └── ex03-04.cbp │ ├── ex05.cpp │ ├── ex06.cpp │ └── ex22.cpp ├── 2. Objects │ ├── 03.txt │ ├── 04.txt │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ └── ex10.cpp ├── 3. C and CPP │ ├── ex01 │ │ ├── ex01.cbp │ │ ├── header.cpp │ │ ├── header.h │ │ └── main.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10 │ │ ├── FileStatic.cpp │ │ └── FileStatic2.cpp │ ├── ex11.cpp │ ├── ex12 │ │ ├── Bitwise.cpp │ │ └── Boolean.cpp │ ├── ex13.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ ├── ex16.cpp │ ├── ex17.cpp │ ├── ex18.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex22.cpp │ ├── ex23.cpp │ ├── ex24.cpp │ ├── ex25.cpp │ ├── ex26.cpp │ ├── ex27.cpp │ ├── ex28.cpp │ ├── ex29.cpp │ ├── ex30 │ │ ├── YourPets1.cpp │ │ ├── YourPets2.cpp │ │ └── makefile │ ├── ex31.cpp │ ├── ex32.cpp │ ├── ex33.cpp │ ├── ex34.cpp │ └── ex35 │ │ ├── ex35.cpp │ │ └── makefile ├── 4. Abstraction │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03 │ │ ├── ex03.cbp │ │ ├── ex03.cpp │ │ ├── ex03.h │ │ └── main.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07 │ │ ├── CLib.cpp │ │ ├── CLib.h │ │ ├── ex07.cbp │ │ └── main.cpp │ ├── ex08 │ │ ├── Stack.cpp │ │ ├── Stack.h │ │ ├── ex08.cbp │ │ └── main.cpp │ ├── ex09 │ │ ├── ex09.cbp │ │ ├── ex09.cpp │ │ └── ex09.h │ ├── ex10.cpp │ ├── ex10.s │ ├── ex11.cpp │ ├── ex12.cpp │ ├── ex13 │ │ ├── ex13.cbp │ │ ├── main.cpp │ │ ├── video.cpp │ │ └── video.h │ ├── ex14 │ │ ├── Stack.cpp │ │ ├── Stack.h │ │ ├── ex14.cbp │ │ ├── main.cpp │ │ ├── video.cpp │ │ └── video.h │ ├── ex15.cpp │ ├── ex16 │ │ ├── CLib.cpp │ │ ├── CLib.h │ │ ├── CLibTest.cpp │ │ └── ex16.cbp │ ├── ex17.cpp │ ├── ex18.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex22 │ │ ├── Stack.cpp │ │ ├── Stack.h │ │ ├── Stash.cpp │ │ ├── Stash.h │ │ ├── ex22.cbp │ │ └── main.cpp │ ├── ex23 │ │ ├── External.h │ │ ├── Stack.cpp │ │ ├── Stack.h │ │ ├── Stash.cpp │ │ ├── Stash.h │ │ ├── ex23.cbp │ │ └── main.cpp │ ├── ex24.cpp │ └── ex25.cpp ├── 5. Implementation │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09 │ │ ├── CppLibTest.cpp │ │ ├── Stash.cpp │ │ ├── Stash.h │ │ └── ex09.cbp │ ├── ex10 │ │ ├── Stash.cpp │ │ ├── Stash.h │ │ ├── ex10.cbp │ │ └── ex10.cpp │ ├── ex11 │ │ ├── Stack.cpp │ │ ├── Stack.h │ │ ├── StackTest.cpp │ │ └── ex11.cbp │ ├── ex12 │ │ ├── Stack.cpp │ │ ├── Stack.h │ │ ├── ex12.cbp │ │ └── main.cpp │ ├── ex13 │ │ ├── Handle.cpp │ │ ├── Handle.h │ │ ├── UseHandle.cpp │ │ ├── ex13.cbp │ │ └── obj │ │ │ └── Release │ │ │ ├── Handle.o │ │ │ └── UseHandle.o │ └── ex14 │ │ ├── StackImp.cpp │ │ ├── StackImp.h │ │ ├── StackOfInt.cpp │ │ ├── StackOfInt.h │ │ ├── ex14.cbp │ │ ├── ex14.layout │ │ └── main.cpp ├── 6. Init and Cleanup │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06 │ │ ├── Handle.cpp │ │ ├── Handle.h │ │ ├── UseHandle.cpp │ │ └── ex06.cbp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ └── ex10.cpp ├── 7. Overloading │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex03.s │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06 │ │ ├── Stash3.cpp │ │ ├── Stash3.h │ │ ├── Stash3Test.cpp │ │ └── ex06.cbp │ ├── ex07 │ │ ├── Stack3.cpp │ │ ├── Stack3.h │ │ ├── ex07.cbp │ │ └── main.cpp │ ├── ex08.cpp │ ├── ex09 │ │ ├── Mem2.cpp │ │ ├── Mem2.h │ │ ├── MemTest.cpp │ │ └── ex09.cbp │ ├── ex10 │ │ ├── Mem2.cpp │ │ ├── Mem2.h │ │ ├── Stash3.cpp │ │ ├── Stash3.h │ │ ├── Stash3Test.cpp │ │ └── ex10.cbp │ └── ex11 │ │ ├── Mem2.cpp │ │ ├── Mem2.h │ │ ├── MemTest.cpp │ │ └── ex11.cbp ├── 8. Constants │ ├── ex01.c │ ├── ex01.cpp │ ├── ex02.c │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04 with C │ │ ├── ex04.c │ │ ├── ex04.cbp │ │ ├── ex04.h │ │ └── main.c │ ├── ex04 │ │ ├── ex04.cbp │ │ ├── ex04.cpp │ │ ├── ex04.h │ │ └── main.cpp │ ├── ex05.cpp │ ├── ex06.cpp │ ├── ex07 │ │ ├── ex07.cbp │ │ ├── ex07.cpp │ │ └── ex07.h │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10.cpp │ ├── ex11.cpp │ ├── ex12.cpp │ ├── ex13.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ ├── ex16.cpp │ ├── ex17 - ConstReference.cpp │ ├── ex18.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex22.cpp │ ├── ex23.cpp │ ├── ex24.cpp │ ├── ex25.cpp │ ├── ex26.cpp │ ├── ex27.cpp │ └── ex28.cpp └── 9. Inline │ ├── Cpptime.h │ ├── ex01.cpp │ ├── ex02.cpp │ ├── ex03.cpp │ ├── ex04.cpp │ ├── ex05.cpp │ ├── ex06 │ ├── ex06.cbp │ ├── ex06.cpp │ └── main.cpp │ ├── ex07.cpp │ ├── ex08.cpp │ ├── ex09.cpp │ ├── ex10.cpp │ ├── ex11.cpp │ ├── ex12.cpp │ ├── ex13.cpp │ ├── ex14.cpp │ ├── ex15.cpp │ ├── ex16.cpp │ ├── ex17.cpp │ ├── ex18.cpp │ ├── ex19.cpp │ ├── ex20.cpp │ ├── ex21.cpp │ ├── ex22 - inline.s │ ├── ex22 - not inline.s │ ├── ex22.cpp │ └── require.h └── Volume 2 ├── 1. Exceptions ├── ex01.cpp ├── ex02.cpp ├── ex03.cpp ├── ex04.cpp ├── ex05.cpp ├── ex06.cpp ├── ex07.cpp ├── ex08.cpp ├── ex09.cpp ├── ex10.cpp └── ex11.cpp ├── 10. Patterns ├── TStack.h ├── ex01.cpp ├── ex02.cfg ├── ex02.cpp ├── ex03.cpp ├── ex04.cpp ├── ex05.cpp ├── ex06 │ ├── ex06.h │ ├── ex06_1.cpp │ ├── ex06_1.txt │ ├── ex06_2.cpp │ ├── ex06_2.txt │ └── ex06_3.txt ├── ex07 │ ├── ex07.cpp │ ├── ex07.h │ ├── ex07_1.txt │ ├── ex07_2.txt │ └── ex07_3.txt ├── ex08.cpp ├── ex09.cpp ├── ex10.cpp ├── ex11.cpp ├── ex12.cpp ├── ex13.cpp ├── ex14.cpp ├── ex15.cpp ├── ex16 │ ├── Observable.h │ ├── Observer.h │ └── ex16.cpp ├── ex17.cpp ├── ex18.cpp ├── ex19.cpp ├── ex20.cpp ├── ex21.cpp ├── ex22.cpp ├── ex23.cpp └── purge.h ├── 11. Concurrency └── ex13.cpp ├── 2. Defensive coding ├── Rational.cpp ├── Rational.h ├── TestSuite │ ├── Suite.cpp │ ├── Suite.h │ ├── Test.cpp │ └── Test.h ├── ex01.cpp └── ex02-04.cpp ├── 3. Strings ├── ex01.cpp ├── ex02.cpp ├── ex03.cpp ├── ex04.cpp ├── ex05.cpp ├── ex06.cpp ├── ex07.cpp ├── ex08.cpp ├── ex08.txt ├── ex09.html ├── ex09_HTMLStripper.cpp ├── ex09_ReplaceAll.cpp ├── ex09_ReplaceAll.h ├── ex10.cpp ├── ex10.txt ├── ex11.cpp ├── ex11.txt ├── ex12.cpp ├── ex12.txt ├── ex14.cpp ├── ex14.txt ├── ex15.cpp ├── ex16.cpp ├── ex16.txt ├── ex16_result.txt ├── ex17.cpp ├── ex17.txt ├── ex18.cpp ├── ichar_traits.h ├── result_06.txt ├── result_07.txt └── source.txt ├── 4. iostreams ├── ex01.cpp ├── ex01.txt ├── ex02.cpp ├── ex02.txt ├── ex03.cpp ├── ex05.cpp ├── ex06.cpp ├── ex07.cpp ├── ex08.bin ├── ex08.cpp ├── ex09.cpp ├── ex09.txt ├── ex11.cpp ├── ex12.cpp ├── ex13.cpp ├── ex13.txt ├── ex15.cpp └── ex15.txt ├── 5. Templates ├── ex01.cpp ├── ex02.cpp ├── ex03.cpp ├── ex05.cpp ├── ex06.cpp ├── ex09.cpp ├── ex10.cpp └── ex11.cpp ├── 6. Algorithms ├── Generators.h ├── Inventory.h ├── PrintSequence.h ├── ex01.cpp ├── ex02.cpp ├── ex03.cpp ├── ex04.cpp ├── ex05.cpp ├── ex06.cpp ├── ex07.cpp ├── ex09.cpp ├── ex10.cpp ├── ex10.txt ├── ex11.cpp ├── ex11_1.txt ├── ex11_2.txt ├── ex12.cpp ├── ex13.cpp ├── ex15.cpp ├── ex16.cpp ├── ex17.cpp ├── ex18.cpp ├── ex19.cpp ├── ex20.cpp ├── ex20_src.txt ├── ex21.cpp ├── ex22.cpp ├── ex23.cpp ├── ex24.cpp ├── ex25.cpp ├── ex26.cpp ├── ex26_towns_ireland.txt ├── ex27.cpp ├── ex27_towns_data.txt ├── ex28.cpp ├── ex29.cpp ├── ex30.cpp ├── ex31.cpp ├── ex32.cpp ├── ex32.txt ├── ex32_result.txt ├── ex33.cpp ├── ex33.txt ├── ex34.cpp ├── ex34.txt ├── ex35.cpp └── ex36.cpp ├── 7. Containers ├── Noisy.cpp ├── Noisy.h ├── TokenIterator.h ├── ex01.cpp ├── ex02.cpp ├── ex03.cpp ├── ex04.cpp ├── ex05.cpp ├── ex06.cpp ├── ex07.cpp ├── ex09.cpp ├── ex09.txt ├── ex10.cpp ├── ex11.cpp ├── ex12.cpp ├── ex13.cpp ├── ex15.cpp ├── ex15_dict.txt ├── ex16.cpp ├── ex17.cpp ├── ex18.cpp ├── ex19.cpp └── ex20.cpp ├── 8. RTTI ├── AutoCounter.h ├── ex01.cpp ├── ex02.cpp ├── ex03.cpp ├── ex04.cpp ├── ex05.cpp ├── ex06.cpp ├── ex07.cpp ├── ex08.cpp ├── ex09.cpp └── ex10.cpp └── 9. Multiple Inheritance ├── ex01.cpp ├── ex02.cpp ├── ex04.cpp ├── ex05.cpp ├── ex06.cpp ├── ex07.cpp ├── ex08.cpp ├── ex09.cpp ├── ex10.cpp ├── ex12.cpp ├── ex14.cpp ├── ex15.cpp ├── ex15_log.txt └── ex16.cpp /README.md: -------------------------------------------------------------------------------- 1 | Some exercise solutions for Bruce Eckel's "Thinking in C++, 2nd Edition". -------------------------------------------------------------------------------- /Volume 1/10. Name control/NamespaceInt.h: -------------------------------------------------------------------------------- 1 | //: C10:NamespaceInt.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #ifndef NAMESPACEINT_H 7 | #define NAMESPACEINT_H 8 | namespace Int { 9 | enum sign { positive, negative }; 10 | class Integer { 11 | int i; 12 | sign s; 13 | public: 14 | Integer(int ii = 0) 15 | : i(ii), 16 | s(i >= 0 ? positive : negative) 17 | {} 18 | sign getSign() const { return s; } 19 | void setSign(sign sgn) { s = sgn; } 20 | // ... 21 | }; 22 | } 23 | #endif // NAMESPACEINT_H ///:~ 24 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/NamespaceMath.h: -------------------------------------------------------------------------------- 1 | //: C10:NamespaceMath.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #ifndef NAMESPACEMATH_H 7 | #define NAMESPACEMATH_H 8 | #include "NamespaceInt.h" 9 | namespace Math { 10 | using namespace Int; 11 | Integer a, b; 12 | Integer divide(Integer, Integer); 13 | // ... 14 | } 15 | #endif // NAMESPACEMATH_H ///:~ 16 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/NamespaceOverriding2.h: -------------------------------------------------------------------------------- 1 | //: C10:NamespaceOverriding2.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #ifndef NAMESPACEOVERRIDING2_H 7 | #define NAMESPACEOVERRIDING2_H 8 | #include "NamespaceInt.h" 9 | namespace Calculation { 10 | using namespace Int; 11 | Integer divide(Integer, Integer); 12 | // ... 13 | } 14 | #endif // NAMESPACEOVERRIDING2_H ///:~ 15 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex07.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class with a destructor that prints a message and then calls exit( ). 3 | Create a global object of this class and see what happens. 4 | **/ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class MyClass { 12 | public: 13 | ~MyClass(); 14 | }; 15 | 16 | MyClass::~MyClass() { 17 | cout << "~MyClass() call..." << endl; 18 | exit(EXIT_FAILURE); 19 | } 20 | 21 | MyClass m; 22 | 23 | int main() { 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex08.cpp: -------------------------------------------------------------------------------- 1 | //: C10:StaticDestructors.cpp 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Static object destructors 7 | #include 8 | using namespace std; 9 | ofstream out("statdest.out"); // Trace file 10 | 11 | class Obj { 12 | char c; // Identifier 13 | public: 14 | Obj(char cc) : c(cc) { 15 | out << "Obj::Obj() for " << c << endl; 16 | } 17 | ~Obj() { 18 | out << "Obj::~Obj() for " << c << endl; 19 | } 20 | }; 21 | 22 | Obj a('a'); // Global (static storage) 23 | // Constructor & destructor always called 24 | 25 | void f() { 26 | static Obj b('b'); 27 | } 28 | 29 | void g() { 30 | static Obj c('c'); 31 | } 32 | 33 | int main() { 34 | out << "inside main()" << endl; 35 | g(); 36 | f(); 37 | out << "leaving main()" << endl; 38 | } ///:~ 39 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex09.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex09.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex10/ex10.h: -------------------------------------------------------------------------------- 1 | #ifndef EX10_H_INCLUDED 2 | #define EX10_H_INCLUDED 3 | 4 | // int x; 5 | static int x; 6 | 7 | #endif // EX10_H_INCLUDED 8 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex10/file1.cpp: -------------------------------------------------------------------------------- 1 | #include "ex10.h" 2 | 3 | void changeValue1(int v) { 4 | x = v; 5 | } 6 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex10/file2.cpp: -------------------------------------------------------------------------------- 1 | #include "ex10.h" 2 | 3 | void changeValue2(int v) { 4 | x = v; 5 | } 6 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex10/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex10/main.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex11/ex11.cpp: -------------------------------------------------------------------------------- 1 | #include "ex11.h" 2 | 3 | MyClass m(123); 4 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex11/ex11.h: -------------------------------------------------------------------------------- 1 | #ifndef EX11_H_INCLUDED 2 | #define EX11_H_INCLUDED 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MyClass { 9 | public: 10 | MyClass(int value) : x(value) {} 11 | void print() { cout << "x = " << x << endl; } 12 | private: 13 | int x; 14 | }; 15 | 16 | #endif // EX11_H_INCLUDED 17 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex11/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex11/main.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex12/ex12.cpp: -------------------------------------------------------------------------------- 1 | #include "ex12.h" 2 | 3 | //static 4 | MyClass m(123); 5 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex12/ex12.h: -------------------------------------------------------------------------------- 1 | #ifndef EX12_H_INCLUDED 2 | #define EX12_H_INCLUDED 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class MyClass { 9 | public: 10 | MyClass(int value) : x(value) {} 11 | void print() { cout << "x = " << x << endl; } 12 | private: 13 | int x; 14 | }; 15 | 16 | #endif // EX12_H_INCLUDED 17 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex12/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex12/main.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex13/ex13.cpp: -------------------------------------------------------------------------------- 1 | #include "ex13.h" 2 | #include 3 | 4 | //static 5 | void testFunc() { 6 | std::cout << "testFunc() called." << std::endl; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex13/ex13.h: -------------------------------------------------------------------------------- 1 | #ifndef EX13_H_INCLUDED 2 | #define EX13_H_INCLUDED 3 | 4 | void testFunc(); 5 | 6 | #endif // EX13_H_INCLUDED 7 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex13/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Declare a function in a header file. Define the function in one cpp file and 3 | call it inside main( ) in a second cpp file. Compile and verify that it works. 4 | Now change the function definition so that it is static and verify that the 5 | linker cannot find it. 6 | **/ 7 | 8 | #include 9 | #include "ex13.h" 10 | 11 | using namespace std; 12 | 13 | int main() { 14 | 15 | testFunc(); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex14.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex14.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex15.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write and compile a simple program that uses the auto and register keywords. 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | auto x = 'z'; 12 | cout << x << endl; 13 | 14 | register int y = 2; 15 | cout << y << endl; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex16/header1.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER1_H_INCLUDED 2 | #define HEADER1_H_INCLUDED 3 | 4 | #include 5 | 6 | namespace MyNamespace { 7 | 8 | void func1() { std::cout << "func1() called." << std::endl; } 9 | void func2(int x) { std::cout << "func2(" << x << ") called." << std::endl; } 10 | } 11 | 12 | 13 | #endif // HEADER1_H_INCLUDED 14 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex16/header2.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER2_H_INCLUDED 2 | #define HEADER2_H_INCLUDED 3 | 4 | #include 5 | #include "header1.h" 6 | 7 | namespace MyNamespace { 8 | 9 | void func3(char c) { std::cout << "func3(" << c << ") called." << std::endl; } 10 | void func4(int x, int y) { std::cout << "func4(" << x << "," << y << ") called." << std::endl; } 11 | } 12 | 13 | 14 | #endif // HEADER2_H_INCLUDED 15 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex16/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex16/main.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex17-18/ex17.h: -------------------------------------------------------------------------------- 1 | #ifndef EX17_H_INCLUDED 2 | #define EX17_H_INCLUDED 3 | 4 | namespace { 5 | 6 | int x; 7 | } 8 | 9 | 10 | #endif // EX17_H_INCLUDED 11 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex17-18/file1.cpp: -------------------------------------------------------------------------------- 1 | #include "ex17.h" 2 | 3 | void setX1(int y) { 4 | x = y; 5 | } 6 | 7 | int getX1() { 8 | return x; 9 | } 10 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex17-18/file2.cpp: -------------------------------------------------------------------------------- 1 | #include "ex17.h" 2 | 3 | void setX2(int y) { 4 | x = y; 5 | } 6 | 7 | int getX2() { 8 | return x; 9 | } 10 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex17-18/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a header file with an unnamed namespace. Include the header in two 3 | separate cpp files and show that an unnamed space is unique for each 4 | translation unit. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | extern void setX1(int ); 12 | extern void setX2(int ); 13 | extern int getX1(); 14 | extern int getX2(); 15 | 16 | int main() { 17 | 18 | setX1(11); 19 | setX2(22); 20 | 21 | cout << "x in first file: " << getX1() << endl; 22 | cout << "x in second file: " << getX2() << endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex19.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify FriendInjection.cpp to add a definition for the friend function and to call the function inside main( ). 3 | **/ 4 | 5 | #include 6 | 7 | namespace Me { 8 | class Us { 9 | friend void you(Us u); 10 | public: 11 | Us() : value (123) {} 12 | private: 13 | int value; 14 | }; 15 | 16 | void you(Us u) { 17 | std::cout << "Value from Us: " << u.value << std::endl; 18 | } 19 | } 20 | 21 | 22 | 23 | int main() { 24 | 25 | Me::Us us; 26 | 27 | you(us); 28 | 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex20.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | In Arithmetic.cpp, demonstrate that the using directive does not extend 3 | outside the function in which the directive was made. 4 | **/ 5 | #include "NamespaceInt.h" 6 | void arithmetic() { 7 | using namespace Int; 8 | Integer x; 9 | x.setSign(positive); 10 | } 11 | 12 | int main() { 13 | 14 | //Int:: 15 | Integer y; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex22.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Repair the problem in OverridingAmbiguity.cpp, first with scope resolution, 3 | then instead with a using declaration that forces the compiler to choose one 4 | of the identical function names. 5 | **/ 6 | #include "NamespaceMath.h" 7 | #include "NamespaceOverriding2.h" 8 | void s() { 9 | using namespace Math; 10 | using namespace Calculation; 11 | // Everything's ok until: 12 | //! divide(1, 2); // Ambiguity 13 | 14 | // Variant 1 15 | // Math::divide(1, 2); 16 | 17 | // Variant 2 18 | // using Calculation::divide; 19 | // divide(1, 2); 20 | 21 | } 22 | int main() {} ///:~ 23 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex22/header1.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER1_H_INCLUDED 2 | #define HEADER1_H_INCLUDED 3 | 4 | namespace First { 5 | 6 | class TestClass { 7 | public: 8 | TestClass() : i(1) {} 9 | private: 10 | int i; 11 | }; 12 | } 13 | 14 | 15 | #endif // HEADER1_H_INCLUDED 16 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex22/header2.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER2_H_INCLUDED 2 | #define HEADER2_H_INCLUDED 3 | 4 | namespace Second { 5 | 6 | class TestClass { 7 | public: 8 | TestClass() : f(2.2) {} 9 | private: 10 | float f; 11 | }; 12 | } 13 | 14 | #endif // HEADER2_H_INCLUDED 15 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex22/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | In two header files, create two namespaces, each containing a class 3 | (with all inline definitions) with a name identical to that in the other namespace. 4 | Create a cpp file that includes both header files. Create a function, and inside 5 | the function use the using directive to introduce both namespaces. Try creating 6 | an object of the class and see what happens. Make the using directives global 7 | (outside of the function) to see if it makes any difference. Repair the problem 8 | using scope resolution, and create objects of both classes. 9 | **/ 10 | 11 | #include "header1.h" 12 | #include "header2.h" 13 | 14 | void someFunc() { 15 | 16 | // using namespace First; 17 | // using namespace Second; 18 | 19 | // error: reference to 'TestClass' is ambiguous| 20 | // TestClass t; 21 | 22 | First::TestClass t1; 23 | Second::TestClass t2; 24 | } 25 | 26 | 27 | int main() {} 28 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex23/header1.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER1_H_INCLUDED 2 | #define HEADER1_H_INCLUDED 3 | 4 | namespace First { 5 | 6 | class TestClass { 7 | public: 8 | TestClass() : i(1) {} 9 | private: 10 | int i; 11 | }; 12 | } 13 | 14 | 15 | #endif // HEADER1_H_INCLUDED 16 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex23/header2.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER2_H_INCLUDED 2 | #define HEADER2_H_INCLUDED 3 | 4 | namespace Second { 5 | 6 | class TestClass { 7 | public: 8 | TestClass() : f(2.2) {} 9 | private: 10 | float f; 11 | }; 12 | } 13 | 14 | #endif // HEADER2_H_INCLUDED 15 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex23/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Repair the problem in Exercise 22 with a using declaration that forces the compiler to 3 | choose one of the identical class names. 4 | **/ 5 | 6 | #include "header1.h" 7 | #include "header2.h" 8 | 9 | void someFunc() { 10 | 11 | using namespace First; 12 | 13 | TestClass t1; 14 | } 15 | 16 | 17 | int main() {} 18 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex24/combined.h: -------------------------------------------------------------------------------- 1 | #ifndef COMBINED_H_INCLUDED 2 | #define COMBINED_H_INCLUDED 3 | 4 | #include "header1.h" 5 | #include "header2.h" 6 | 7 | namespace Combined { 8 | 9 | using namespace BobsSuperDuperLibrary; 10 | using namespace exUnnamed; 11 | } 12 | 13 | #endif // COMBINED_H_INCLUDED 14 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex24/header1.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER1_H_INCLUDED 2 | #define HEADER1_H_INCLUDED 3 | 4 | namespace BobsSuperDuperLibrary { 5 | class Widget { /* ... */ }; 6 | class Poppit { /* ... */ }; 7 | } 8 | 9 | #endif // HEADER1_H_INCLUDED 10 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex24/header2.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER2_H_INCLUDED 2 | #define HEADER2_H_INCLUDED 3 | 4 | namespace exUnnamed { 5 | class Arm { /* ... */ }; 6 | class Leg { /* ... */ }; 7 | class Head { /* ... */ }; 8 | class Robot { 9 | Arm arm[4]; 10 | Leg leg[16]; 11 | Head head[3]; 12 | // ... 13 | } xanthan; 14 | 15 | int i, j, k; 16 | } 17 | 18 | #endif // HEADER2_H_INCLUDED 19 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex24/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Extract the namespace declarations in BobsSuperDuperLibrary.cpp and UnnamedNamespaces.cpp 3 | and put them in separate header files, giving the unnamed namespace a name in the process. 4 | In a third header file create a new namespace that combines the elements of the other two 5 | namespaces with using declarations. In main( ), introduce your new namespace with a using 6 | directive and access all the elements of your namespace. 7 | **/ 8 | 9 | #include "combined.h" 10 | 11 | int main() { 12 | 13 | using namespace Combined; 14 | 15 | Widget w; 16 | Poppit p; 17 | 18 | Arm a; 19 | Leg l; 20 | Head h; 21 | 22 | Robot r; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex25/ex25.h: -------------------------------------------------------------------------------- 1 | #ifndef EX25_H_INCLUDED 2 | #define EX25_H_INCLUDED 3 | 4 | #include 5 | #include 6 | 7 | class MyClass { 8 | public: 9 | MyClass(std::string s) : str (s) {} 10 | void print() { std::cout << str << std::endl; } 11 | private: 12 | std::string str; 13 | }; 14 | 15 | #endif // EX25_H_INCLUDED 16 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex25/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex25/main.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex26.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class containing a static double and long. Write a static member function that prints out the values. 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class StaticMembersClass { 10 | public: 11 | StaticMembersClass() {} 12 | static void print() { cout << d << '\n' << l << endl; } 13 | private: 14 | static double d; 15 | static long l; 16 | }; 17 | 18 | double StaticMembersClass::d = 2.2d; 19 | long StaticMembersClass::l = 1000000000L; 20 | 21 | int main() { 22 | 23 | StaticMembersClass s; 24 | s.print(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex27.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex27.cpp -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex30.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a struct that contains an int and a default constructor that 3 | initializes the int to zero. Make this struct local to a function. 4 | Inside that function, create an array of objects of your struct and 5 | demonstrate that each int in the array has automatically been initialized 6 | to zero. 7 | **/ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | void f() { 14 | 15 | struct MyStruct { 16 | MyStruct() : x(0) {} 17 | int x; 18 | }; 19 | 20 | MyStruct m[5]; 21 | 22 | for (int i = 0; i < 5; i++) 23 | cout << m[i].x << ' '; 24 | } 25 | 26 | int main() { 27 | 28 | f(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex32/Mirror.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex32/Mirror.h -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex32/file1.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | Mirror m1; 4 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex32/file2.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | extern Mirror m1; 4 | Mirror m2(&m1); 5 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex32/file3.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | extern Mirror m2; 4 | Mirror m3(&m2); 5 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex32/file4.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | extern Mirror m3; 4 | Mirror m4(&m3); 5 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex32/file5.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | #include 3 | 4 | extern Mirror m4; 5 | Mirror m5(&m4); 6 | 7 | bool result = m5.test(); 8 | 9 | int main() { 10 | 11 | std::cout.setf(std::ios::boolalpha); 12 | std::cout << result << std::endl; 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex33/Mirror.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/10. Name control/ex33/Mirror.h -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex33/file1.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | Mirror& m1() { 4 | static Mirror m1; 5 | return m1; 6 | } 7 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex33/file2.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | extern Mirror& m1(); 4 | 5 | Mirror& m2() { 6 | static Mirror m2(m1()); 7 | return m2; 8 | } 9 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex33/file3.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | extern Mirror& m2(); 4 | 5 | Mirror& m3() { 6 | static Mirror m3(m2()); 7 | return m3; 8 | } 9 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex33/file4.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | 3 | extern Mirror& m3(); 4 | 5 | Mirror& m4() { 6 | static Mirror m4(m3()); 7 | return m4; 8 | } 9 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex33/file5.cpp: -------------------------------------------------------------------------------- 1 | #include "Mirror.h" 2 | #include 3 | 4 | extern Mirror& m4(); 5 | 6 | Mirror& m5() { 7 | static Mirror m5(m4()); 8 | return m5; 9 | } 10 | 11 | bool result = m5().test(); 12 | 13 | int main() { 14 | 15 | std::cout.setf(std::ios::boolalpha); 16 | std::cout << result << std::endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/ex35.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Without including a header file, declare the function puts( ) 3 | from the Standard C Library. Call this function from main( ). 4 | **/ 5 | 6 | extern "C" int puts(const char* ); 7 | 8 | int main() { 9 | 10 | puts("Hello!"); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Volume 1/10. Name control/statdest.out: -------------------------------------------------------------------------------- 1 | Obj::Obj() for a 2 | inside main() 3 | Obj::Obj() for c 4 | Obj::Obj() for b 5 | leaving main() 6 | Obj::~Obj() for b 7 | Obj::~Obj() for c 8 | Obj::~Obj() for a 9 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex01.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/11. References&Copy/ex01.cpp -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Take the code fragments in the beginning of the section titled 3 | "References in C++" and put them into a main( ). Add statements 4 | to print output so that you can prove to yourself that references 5 | are like pointers that are automatically dereferenced. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | int y; 14 | int& r = y; 15 | 16 | const int& q = 12; 17 | 18 | int x = 0; 19 | int& a = x; 20 | 21 | cout << "x = " << x << ", a = " << a << endl; 22 | a++; 23 | cout << "x = " << x << ", a = " << a << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program in which you try to 3 | (1) Create a reference that is not initialized when it is created. 4 | (2) Change a reference to refer to another object after it is initialized. 5 | (3) Create a NULL reference. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | int x = 10; 15 | int y = 20; 16 | int& ref2 = x; 17 | 18 | cout << ref2 << endl; 19 | 20 | // 1 21 | // int& ref1; 22 | // error: 'ref1' declared as reference but not initialized 23 | 24 | // 2 25 | ref2 = y; 26 | cout << ref2 << endl; 27 | 28 | // 3 29 | // int& ref3 = NULL; 30 | // error: invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'| 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex04.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a function that takes a pointer argument, modifies 3 | what the pointer points to, and then returns the destination 4 | of the pointer as a reference. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | int& refAndPointer(int* x) { 12 | int& y = *x; 13 | y++; 14 | return y; 15 | } 16 | 17 | int main() { 18 | 19 | int i = 10; 20 | cout << i << endl; 21 | cout << refAndPointer(&i) << endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex06.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Take the code fragments at the beginning of the section titled "Pointer references" and turn them into a program. 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | void increment(int*& i) { i++; } 10 | 11 | int main() { 12 | int* i = 0; 13 | cout << "i = " << i << endl; 14 | 15 | increment(i); 16 | cout << "i = " << i << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex07.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a function that takes an argument of a reference to a pointer 3 | to a pointer and modifies that argument. In main( ), call the function. 4 | **/ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | void modifyPtr(int**& refptr) { 11 | 12 | (*refptr)++; 13 | } 14 | 15 | int main() { 16 | 17 | int my_array[] = {1, 2, 3}; 18 | 19 | int* iPtr = my_array; // ptr to arrays head 20 | int** iPtrPtr = &iPtr; // ptr to ptr to arrays head 21 | 22 | cout << *(*iPtrPtr) << endl; 23 | // modifies ptr so it points to next element 24 | modifyPtr(iPtrPtr); 25 | cout << *(*iPtrPtr) << endl; 26 | 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex08.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a function that takes a char& argument and modifies that argument. 3 | In main( ), print out a char variable, call your function for that variable, 4 | and print it out again to prove to yourself that it has been changed. 5 | How does this affect program readability? 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | void changeChar(char& c) { 13 | 14 | c = 'z'; 15 | } 16 | 17 | int main() { 18 | 19 | char sym = 'a'; 20 | cout << sym << endl; 21 | changeChar(sym); 22 | cout << sym << endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a simple function that takes an int as an argument, increments the value, 3 | and returns it. In main( ), call your function. Now discover how your compiler 4 | generates assembly code and trace through the assembly statements so that you 5 | understand how arguments are passed and returned, and how local variables are 6 | indexed off the stack. 7 | **/ 8 | 9 | /// Compilation command: 10 | /// g++ -S -masm=intel -g ex10.cpp -o ex10.s 11 | 12 | using namespace std; 13 | 14 | int increment(int x) { 15 | x++; 16 | return x; 17 | } 18 | 19 | int main() { 20 | 21 | int y = 10; 22 | increment(y); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex15.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/11. References&Copy/ex15.cpp -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex17.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/11. References&Copy/ex17.cpp -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex18.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class with a constructor that looks like a copy-constructor, 3 | but that has an extra argument with a default value. Show that this is 4 | still used as the copy-constructor. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class MyClass { 12 | public: 13 | MyClass() { cout << "MyClass()" << endl; } 14 | MyClass(const MyClass& m, int x = 0) { cout << "MyClass() copy constructor with an argument." << endl; } 15 | }; 16 | 17 | void foo1(MyClass m) {} 18 | 19 | int main() { 20 | 21 | MyClass obj; 22 | foo1(obj); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex19.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class with a copy-constructor that announces itself. Make a 3 | second class containing a member object of the first class, but do not 4 | create a copy-constructor. Show that the synthesized copy-constructor 5 | in the second class automatically calls the copy-constructor of the first class. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | class A { 13 | public: 14 | A() {} 15 | A(const A& a) { cout << "A() copy constructor." << endl; } 16 | }; 17 | 18 | class B{ 19 | A a; 20 | }; 21 | 22 | void foo(B b) {} 23 | 24 | int main() { 25 | 26 | B b; 27 | foo(b); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex20.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a very simple class, and a function that returns an object of that class 3 | by value. Create a second function that takes a reference to an object of your 4 | class. Call the first function as the argument of the second function, and 5 | demonstrate that the second function must use a const reference as its argument. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | class X { 13 | public: 14 | X() : x(0) {} 15 | X(int xxx) : x(xxx) {} 16 | private: 17 | int x; 18 | }; 19 | 20 | X func1() { 21 | cout << "func1()" << endl; 22 | return X(); 23 | } 24 | 25 | //void func2(X& x) { 26 | //error: invalid initialization of non-const reference of type 'X&' from an rvalue of type 'X' 27 | void func2(const X& x) { 28 | cout << "func2()" << endl; 29 | } 30 | 31 | int main() { 32 | 33 | func2( func1() ); 34 | 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex21.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a simple class without a copy-constructor, and a simple function 3 | that takes an object of that class by value. Now change your class by 4 | adding a private declaration (only) for the copy-constructor. Explain 5 | what happens when your function is compiled. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | class SimpleClass { 13 | public: 14 | SimpleClass() {} 15 | private: 16 | ///SimpleClass(const SimpleClass& s); 17 | ///error: 'SimpleClass::SimpleClass(const SimpleClass&)' is private| 18 | 19 | }; 20 | 21 | void foo(SimpleClass s) {} 22 | 23 | int main() { 24 | 25 | SimpleClass s; 26 | foo(s); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex22.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/11. References&Copy/ex22.cpp -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex25.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class containing an array of int. 3 | Can you index through this array using a pointer to member? 4 | **/ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class SomeClass { 11 | public: 12 | SomeClass() : class_array{1,2,3,4,5} {} 13 | int class_array[5]; 14 | }; 15 | 16 | int main() { 17 | 18 | SomeClass s; 19 | SomeClass* sPtr = &s; 20 | 21 | int (SomeClass::*array_ptr)[5] = &SomeClass::class_array; 22 | 23 | cout << *( sPtr->*array_ptr + 2 ) << endl; 24 | /// YES, WE CAN 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Volume 1/11. References&Copy/ex26.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify PmemFunDefinition.cpp by adding an overloaded member function f( ) 3 | (you can determine the argument list that causes the overload). Now make a 4 | second pointer to member, assign it to the overloaded version of f( ), and 5 | call the function through that pointer. How does the overload resolution 6 | happen in this case? 7 | **/ 8 | class Simple2 { 9 | public: 10 | int f(float) const { return 1; } 11 | void f(char) const {} 12 | }; 13 | 14 | int (Simple2::*fp)(float) const; 15 | int (Simple2::*fp2)(float) const = &Simple2::f; 16 | 17 | void (Simple2::*fp3)(char) const = &Simple2::f; 18 | 19 | int main() { 20 | 21 | fp = &Simple2::f; 22 | 23 | Simple2 s; 24 | Simple2* sPtr = &s; 25 | 26 | (sPtr->*fp2)(1.2); 27 | (sPtr->*fp3)('z'); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a simple class with an overloaded operator++. Try calling this 3 | operator in both pre- and postfix form and see what kind of compiler 4 | warning you get. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Simple { 12 | public: 13 | Simple() : i(0) {} 14 | Simple(int iii) : i(iii) {} 15 | 16 | const Simple& operator++() { 17 | i++; 18 | return *this; 19 | } 20 | 21 | private: 22 | int i; 23 | 24 | }; 25 | 26 | int main() { 27 | 28 | Simple s; 29 | 30 | ++s; 31 | ///error: no 'operator++(int)' declared for postfix '++' 32 | ///s++; 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a simple class containing an int and overload the operator+ as a 3 | member function. Also provide a print( ) member function that takes an 4 | ostream& as an argument and prints to that ostream&. Test your class to 5 | show that it works correctly. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | class SimpleInt { 13 | public: 14 | SimpleInt() : i(0) {} 15 | SimpleInt(int iii) : i(iii) {} 16 | 17 | const SimpleInt operator+(const SimpleInt& right) const { return SimpleInt(i + right.i); } 18 | 19 | void print(ostream& out) { out << i << endl; } 20 | private: 21 | int i; 22 | }; 23 | 24 | int main() { 25 | 26 | SimpleInt a(1), b(2); 27 | SimpleInt c = a + b; 28 | c.print(cout); 29 | 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex03.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/12. Overloading/ex03.cpp -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex07.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/12. Overloading/ex07.cpp -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex09.txt: -------------------------------------------------------------------------------- 1 | d -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Determine the dummy constant value that your compiler passes for postfix operator++ and operator--. 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class SimpleInt { 10 | public: 11 | SimpleInt() : i(0) {} 12 | SimpleInt(int iii) : i(iii) {} 13 | 14 | const SimpleInt operator+(const SimpleInt& right) const { return SimpleInt(i + right.i); } 15 | 16 | //Postfix 17 | const SimpleInt operator++(int x) { 18 | SimpleInt s(i); 19 | i++; 20 | cout << "increment constant = " << x << endl; 21 | return s; 22 | } 23 | 24 | const SimpleInt operator--(int x) { 25 | SimpleInt s(i); 26 | i--; 27 | cout << "decrement constant = " << x << endl; 28 | return s; 29 | } 30 | private: 31 | int i; 32 | }; 33 | int main() { 34 | 35 | SimpleInt s; 36 | 37 | s++; 38 | s--; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex11-12.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/12. Overloading/ex11-12.cpp -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex19.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/12. Overloading/ex19.cpp -------------------------------------------------------------------------------- /Volume 1/12. Overloading/ex20.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Attempt to create a non-member operator= for a class and see what kind of compiler message you get. 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Simple { 10 | public: 11 | Simple(int x = 0) : data(x) {} 12 | private: 13 | int data; 14 | }; 15 | 16 | Simple& operator=(const Simple& left, const Simple& right) { 17 | 18 | if (&left == &right) 19 | return left; 20 | 21 | left.data = right.data; 22 | return left; 23 | } 24 | 25 | /// error: 'Simple& operator=(const Simple&, const Simple&)' must be a nonstatic member function 26 | 27 | int main() { 28 | 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex01.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex01.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex02.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex02.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex03/PStash.h: -------------------------------------------------------------------------------- 1 | //: C13:PStash.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Holds pointers instead of objects 7 | #ifndef PSTASH_H 8 | #define PSTASH_H 9 | 10 | class PStash { 11 | int quantity; // Number of storage spaces 12 | int next; // Next empty space 13 | // Pointer storage: 14 | void** storage; 15 | void inflate(int increase); 16 | public: 17 | PStash() : quantity(0), storage(0), next(0) {} 18 | ~PStash(); 19 | int add(void* element); 20 | void* operator[](int index) const; // Fetch 21 | // Remove the reference from this PStash: 22 | void* remove(int index); 23 | // Number of elements in Stash: 24 | int count() const { return next; } 25 | }; 26 | #endif // PSTASH_H ///:~ 27 | -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex03/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex03/main.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex04.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex04.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex05.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex05.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex06/PStash.h: -------------------------------------------------------------------------------- 1 | //: C13:PStash.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Holds pointers instead of objects 7 | #ifndef PSTASH_H 8 | #define PSTASH_H 9 | 10 | class PStash { 11 | int quantity; // Number of storage spaces 12 | int next; // Next empty space 13 | // Pointer storage: 14 | void** storage; 15 | void inflate(int increase); 16 | public: 17 | PStash() : quantity(0), storage(0), next(0) {} 18 | ~PStash(); 19 | int add(void* element); 20 | void* operator[](int index) const; // Fetch 21 | // Remove the reference from this PStash: 22 | void* remove(int index); 23 | // Number of elements in Stash: 24 | int count() const { return next; } 25 | }; 26 | #endif // PSTASH_H ///:~ 27 | -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex07/Stack4.h: -------------------------------------------------------------------------------- 1 | //: C09:Stack4.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // With inlines 7 | #ifndef STACK4_H 8 | #define STACK4_H 9 | 10 | class Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | Link(void* dat, Link* nxt): 15 | data(dat), next(nxt) {} 16 | }* head; 17 | public: 18 | Stack() : head(0) {} 19 | ~Stack() { } 20 | 21 | void push(void* dat) { 22 | head = new Link(dat, head); 23 | } 24 | void* peek() const { 25 | return head ? head->data : 0; 26 | } 27 | void* pop() { 28 | if(head == 0) return 0; 29 | void* result = head->data; 30 | Link* oldHead = head; 31 | head = head->next; 32 | delete oldHead; 33 | return result; 34 | } 35 | }; 36 | #endif // STACK4_H ///:~ 37 | -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex07/ex07.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex07/ex07.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex08.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex08.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex09.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex09.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Execute NewHandler.cpp on your machine to see the resulting count. 3 | Calculate the amount of free store available for your program. 4 | **/ 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int count = 0; 11 | 12 | void out_of_memory() { 13 | cerr << "memory exhausted after " << count 14 | << " allocations!" << endl; 15 | cerr << "Available memory: " << count * sizeof(int) * 1000 / 1024 / 1024 << " Mb" << endl; 16 | exit(1); 17 | } 18 | 19 | int main() { 20 | set_new_handler(out_of_memory); 21 | while(1) { 22 | count++; 23 | new int[1000]; // Exhausts memory 24 | } 25 | } ///:~ 26 | -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex14.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/13. Dynamic objects/ex14.cpp -------------------------------------------------------------------------------- /Volume 1/13. Dynamic objects/ex15.out: -------------------------------------------------------------------------------- 1 | new Widget 2 | Widget::new: 40 bytes 3 | * 4 | delete Widget 5 | ~Widget::delete 6 | 7 | new Widget[25] 8 | Widget::new[]: 1004 bytes 9 | ************************* 10 | delete []Widget 11 | ~~~~~~~~~~~~~~~~~~~~~~~~~Widget::delete[] 12 | >>>> Pointers in vector: 0 13 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex01.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/14. Inheritance&Composition/ex01.cpp -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create two classes, A and B, with default constructors that announce themselves. 3 | Inherit a new class called C from A, and create a member object of B in C, but do 4 | not create a constructor for C. Create an object of class C and observe the results. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class A { 12 | public: 13 | A() {cout << "A()" << endl; } 14 | }; 15 | 16 | class B { 17 | public: 18 | B() {cout << "B()" << endl; } 19 | }; 20 | 21 | class C : public A { 22 | B b; 23 | }; 24 | 25 | int main() { 26 | 27 | C c; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a three-level hierarchy of classes with default constructors, 3 | along with destructors, both of which announce themselves to cout. 4 | Verify that for an object of the most derived type, all three 5 | constructors and destructors are automatically called. Explain 6 | the order in which the calls are made. 7 | **/ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | class A { 14 | public: 15 | A() { cout << "A()" << endl; } 16 | ~A() { cout << "~A()" << endl; } 17 | }; 18 | 19 | class B : public A { 20 | public: 21 | B() : A() { cout << "B()" << endl; } 22 | ~B() { cout << "~B()" << endl; } 23 | }; 24 | 25 | class C : public B { 26 | public: 27 | C() : B() { cout << "C()" << endl; } 28 | ~C() { cout << "~C()" << endl; } 29 | }; 30 | 31 | int main() { 32 | 33 | C c; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex06.out: -------------------------------------------------------------------------------- 1 | Base1 constructor 2 | Member1 constructor 3 | Member2 constructor 4 | Derived1 constructor 5 | Member3 constructor 6 | Member4 constructor 7 | Derived2 constructor 8 | Member4 constructor 9 | Member5 constructor 10 | Derived3 constructor 11 | Derived3 destructor 12 | Member5 destructor 13 | Member4 destructor 14 | Derived2 destructor 15 | Member4 destructor 16 | Member3 destructor 17 | Derived1 destructor 18 | Member2 destructor 19 | Member1 destructor 20 | Base1 destructor 21 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex09.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Inherit a class StringVector from vector and redefine the push_back( ) 3 | and operator[] member functions to accept and produce string*. 4 | What happens if you try to push_back( ) a void*? 5 | **/ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | class StringVector : public vector { 14 | public: 15 | StringVector() : vs() {} 16 | void push_back(string* s) { vs.push_back(s); } 17 | string* operator[](size_t index) { return vs[index]; } 18 | private: 19 | vector vs; 20 | }; 21 | 22 | 23 | 24 | int main () { 25 | 26 | StringVector v; 27 | string str = "test"; 28 | void* vPtr = static_cast(&str); 29 | 30 | //!v.push_back(vPtr); 31 | //!error: invalid conversion from 'void*' to 'std::string* {aka std::basic_string*}' [-fpermissive]| 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a class containing a long and use the psuedo-constructor call syntax in the constructor to initialize the long. 3 | **/ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class SomeLong { 11 | public: 12 | SomeLong() : a(1234567890) {} 13 | void print() const { cout << "a = " << a << endl; } 14 | private: 15 | long a; 16 | }; 17 | 18 | int main() { 19 | 20 | SomeLong x; 21 | x.print(); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex11/PStash.h: -------------------------------------------------------------------------------- 1 | //: C13:PStash.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Holds pointers instead of objects 7 | #ifndef PSTASH_H 8 | #define PSTASH_H 9 | 10 | class PStash { 11 | int quantity; // Number of storage spaces 12 | int next; // Next empty space 13 | // Pointer storage: 14 | void** storage; 15 | void inflate(int increase); 16 | public: 17 | PStash() : quantity(0), storage(0), next(0) {} 18 | ~PStash(); 19 | int add(void* element); 20 | void* operator[](int index) const; // Fetch 21 | // Remove the reference from this PStash: 22 | void* remove(int index); 23 | // Number of elements in Stash: 24 | int count() const { return next; } 25 | }; 26 | #endif // PSTASH_H ///:~ 27 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex11/PStashTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class called Asteroid. Use inheritance to specialize the PStash 3 | class in Chapter 13 (PStash.h & PStash.cpp) so that it accepts and returns 4 | Asteroid pointers. Also modify PStashTest.cpp to test your classes. 5 | Change the class so PStash is a member object. 6 | **/ 7 | #include "AStash.h" 8 | #include "require.h" 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int main() { 15 | 16 | AStash AsteroidStash; 17 | 18 | for(int i = 0; i < 5; i++) 19 | AsteroidStash.add(new Asteroid(i)); 20 | 21 | for(int j = 0; j < AsteroidStash.count(); j++) 22 | cout << "AsteroidStash[" << j << "] = " 23 | << *((int*)AsteroidStash[j]) << endl; 24 | 25 | for(int k = 0; k < AsteroidStash.count(); k++) 26 | delete AsteroidStash.remove(k); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex12/PStash.h: -------------------------------------------------------------------------------- 1 | //: C13:PStash.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Holds pointers instead of objects 7 | #ifndef PSTASH_H 8 | #define PSTASH_H 9 | 10 | class PStash { 11 | int quantity; // Number of storage spaces 12 | int next; // Next empty space 13 | // Pointer storage: 14 | void** storage; 15 | void inflate(int increase); 16 | public: 17 | PStash() : quantity(0), storage(0), next(0) {} 18 | ~PStash(); 19 | int add(void* element); 20 | void* operator[](int index) const; // Fetch 21 | // Remove the reference from this PStash: 22 | void* remove(int index); 23 | // Number of elements in Stash: 24 | int count() const { return next; } 25 | }; 26 | #endif // PSTASH_H ///:~ 27 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex13.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/14. Inheritance&Composition/ex13.cpp -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex15.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class with two static member functions. Inherit from this class 3 | and redefine one of the member functions. Show that the other is hidden 4 | in the derived class. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class A { 12 | public: 13 | static void foo() { cout << "A::foo()" << endl; } 14 | static void foo(int) { cout << "A::foo(int)" << endl; } 15 | }; 16 | 17 | class B : public A { 18 | public: 19 | static void foo() { cout << "B::foo()" << endl; } 20 | }; 21 | 22 | using namespace std; 23 | 24 | int main() { 25 | 26 | B b; 27 | 28 | b.foo(); 29 | //!b.foo(1); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex17.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Use private and protected inheritance to create two new classes from a base class. 3 | Then attempt to upcast objects of the derived class to the base class. 4 | Explain what happens. 5 | **/ 6 | 7 | class Base { 8 | public: 9 | Base() {} 10 | }; 11 | 12 | class Derived1 : protected Base { 13 | public: 14 | Derived1() {} 15 | }; 16 | 17 | class Derived2 : private Base { 18 | public: 19 | Derived2() {} 20 | }; 21 | 22 | void f(Base b) {} 23 | 24 | int main() { 25 | 26 | Derived1 d1; 27 | Derived2 d2; 28 | 29 | //f(d1); 30 | //!error: 'Base' is an inaccessible base of 'Derived1'| 31 | //f(d2); 32 | //!error: 'Base' is an inaccessible base of 'Derived2'| 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex18.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | In Protected.cpp, add a member function in Derived that calls the protected Base member read( ). 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Base { 10 | int i; 11 | protected: 12 | int read() const { return i; } 13 | void set(int ii) { i = ii; } 14 | public: 15 | Base(int ii = 0) : i(ii) {} 16 | int value(int m) const { return m*i; } 17 | }; 18 | 19 | class Derived : public Base { 20 | int j; 21 | public: 22 | Derived(int jj = 0) : j(jj) {} 23 | void change(int x) { set(x); } 24 | int f() { return read(); } 25 | }; 26 | 27 | int main() { 28 | Derived d; 29 | d.change(10); 30 | cout << d.f() << endl; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex19.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Change Protected.cpp so that Derived is using protected inheritance. 3 | See if you can call value( ) for a Derived object. 4 | **/ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Base { 11 | int i; 12 | protected: 13 | int read() const { return i; } 14 | void set(int ii) { i = ii; } 15 | public: 16 | Base(int ii = 0) : i(ii) {} 17 | int value(int m) const { return m*i; } 18 | }; 19 | 20 | class Derived : protected Base { 21 | int j; 22 | public: 23 | Derived(int jj = 0) : j(jj) {} 24 | void change(int x) { set(x); } 25 | }; 26 | 27 | int main() { 28 | Derived d; 29 | d.change(10); 30 | //cout << d.value(5) << endl; 31 | //!error: 'int Base::value(int) const' is inaccessible| 32 | } ///:~ 33 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex20.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class called SpaceShip with a fly( ) method. Inherit Shuttle from 3 | SpaceShip and add a land( ) method. Create a new Shuttle, upcast by pointer 4 | or reference to a SpaceShip, and try to call the land( ) method. 5 | Explain the results. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | class SpaceShip { 13 | public: 14 | void fly() { cout << "SpaceShip::fly()" << endl; } 15 | }; 16 | 17 | class Shuttle : public SpaceShip { 18 | public: 19 | void land() { cout << "Shuttle::land()" << endl; } 20 | }; 21 | 22 | int main() { 23 | 24 | Shuttle sh; 25 | SpaceShip* sPtr = &sh; 26 | 27 | sPtr->fly(); 28 | //sPtr->land(); 29 | //!error: 'class SpaceShip' has no member named 'land'| 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex21.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Instrument.cpp to add a prepare( ) method to Instrument. Call prepare( ) inside tune( ). 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | enum note { middleC, Csharp, Cflat }; // Etc. 10 | 11 | class Instrument { 12 | public: 13 | void play(note) const {} 14 | void prepare(int i) const { cout << "prepare(" << i << ") called." << endl; } 15 | }; 16 | 17 | class Wind : public Instrument {}; 18 | 19 | void tune(Instrument& i) { 20 | i.play(middleC); 21 | i.prepare(1); 22 | } 23 | 24 | int main() { 25 | Wind flute; 26 | tune(flute); 27 | } 28 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex22.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/14. Inheritance&Composition/ex22.cpp -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex25.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify InheritStack2.cpp to use a vector instead of a Stack. 3 | **/ 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | class StringStack { 12 | vector stack; 13 | public: 14 | void push(string* str) { stack.push_back(str); } 15 | string* peek() const { return stack.back(); } 16 | 17 | string* pop() { 18 | string* temp = 0; 19 | if (stack.size() != 0 ) { 20 | temp = stack.back(); 21 | stack.pop_back(); 22 | } 23 | return temp; 24 | } 25 | }; 26 | 27 | int main() { 28 | ifstream in("ex25.cpp"); 29 | string line; 30 | StringStack textlines; 31 | while(getline(in, line)) 32 | textlines.push(new string(line)); 33 | string* s; 34 | while((s = textlines.pop()) != 0) 35 | cout << *s << endl; 36 | } 37 | -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex26.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/14. Inheritance&Composition/ex26.cpp -------------------------------------------------------------------------------- /Volume 1/14. Inheritance&Composition/ex28.out: -------------------------------------------------------------------------------- 1 | new Child 2 | Widget::new: 40 bytes 3 | * 4 | delete Child 5 | ~Widget::delete 6 | 7 | new Child[25] 8 | Widget::new[]: 1004 bytes 9 | ************************* 10 | delete []Child 11 | ~~~~~~~~~~~~~~~~~~~~~~~~~Widget::delete[] 12 | -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex04.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify C14:Combined.cpp so that f( ) is virtual in the base class. 3 | Change main( ) to perform an upcast and a virtual call. 4 | **/ 5 | 6 | class A { 7 | int i; 8 | public: 9 | A(int ii) : i(ii) {} 10 | ~A() {} 11 | void f() const {} 12 | }; 13 | 14 | class B { 15 | int i; 16 | public: 17 | B(int ii) : i(ii) {} 18 | ~B() {} 19 | virtual void f() const {} 20 | }; 21 | 22 | class C : public B { 23 | A a; 24 | public: 25 | C(int ii) : B(ii), a(ii) {} 26 | ~C() {} 27 | void f() const { 28 | a.f(); 29 | B::f(); 30 | } 31 | }; 32 | 33 | void foo(B b) { 34 | b.f(); 35 | } 36 | 37 | int main() { 38 | C c(47); 39 | foo(c); 40 | } 41 | -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Instrument3.cpp by adding a virtual prepare( ) function. Call prepare( ) inside tune( ). 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | enum note { middleC, Csharp, Cflat }; 10 | 11 | class Instrument { 12 | public: 13 | virtual void play(note) const { 14 | cout << "Instrument::play" << endl; 15 | } 16 | 17 | virtual void prepare() const { 18 | cout << "Instrument::prepare" << endl; 19 | } 20 | }; 21 | 22 | class Wind : public Instrument { 23 | public: 24 | void play(note) const { 25 | cout << "Wind::play" << endl; 26 | } 27 | }; 28 | 29 | void tune(Instrument& i) { 30 | i.prepare(); 31 | i.play(middleC); 32 | } 33 | 34 | int main() { 35 | Wind flute; 36 | tune(flute); 37 | } 38 | -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex08.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/15. Polymorphism/ex08.cpp -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex09.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/15. Polymorphism/ex09.cpp -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex13.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | In Early.cpp, make Pet a pure abstract base class. 3 | **/ 4 | 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Pet { 11 | public: 12 | virtual string speak() const = 0; 13 | }; 14 | 15 | class Dog : public Pet { 16 | public: 17 | string speak() const { return "Bark!"; } 18 | }; 19 | 20 | int main() { 21 | Dog ralph; 22 | Pet* p1 = &ralph; 23 | Pet& p2 = ralph; 24 | //Pet p3; 25 | // Late binding for both: 26 | cout << "p1->speak() = " << p1->speak() <speak(); 34 | 35 | delete p; 36 | } 37 | -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex15.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a small program to show the difference between calling a 3 | virtual function inside a normal member function and calling a 4 | virtual function inside a constructor. The program should prove 5 | that the two calls produce different results. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Base { 13 | public: 14 | Base() { cout << "f() within Base(): " << endl; f(); } 15 | virtual void f() { cout << "\tBase::f()" << endl; } 16 | }; 17 | 18 | class Child : public Base { 19 | public: 20 | Child() : Base() { cout << "f() within Child(): " << endl; f(); } 21 | void f() { cout << "\tChild::f()" << endl; } 22 | void g() { cout << "f() within g(): " << endl; f(); } 23 | }; 24 | 25 | int main() { 26 | 27 | Child* c = new Child; 28 | c->g(); 29 | delete c; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex16.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify VirtualsInDestructors.cpp by inheriting a class from Derived 3 | and overriding f( ) and the destructor. In main( ), create and upcast 4 | an object of your new type, then delete it. 5 | **/ 6 | #include 7 | using namespace std; 8 | 9 | class Base { 10 | public: 11 | virtual ~Base() { 12 | cout << "~Base()\n"; 13 | f(); 14 | } 15 | virtual void f() { cout << "Base::f()\n"; } 16 | }; 17 | 18 | class Derived : public Base { 19 | public: 20 | ~Derived() { cout << "~Derived()\n"; } 21 | void f() { cout << "Derived::f()\n"; } 22 | }; 23 | 24 | class Derived2 : public Derived { 25 | public: 26 | ~Derived2() { cout << "~Derived2()\n"; } 27 | void f() { cout << "Derived2::f()\n"; } 28 | }; 29 | 30 | int main() { 31 | Base* bp = new Derived2; 32 | delete bp; 33 | } ///:~ 34 | -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex18.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class that has a data member and a derived class that adds 3 | another data member. Write a non-member function that takes an object 4 | of the base class by value and prints out the size of that object using 5 | sizeof. In main( ) create an object of the derived class, print out its 6 | size, and then call your function. Explain what happens. 7 | **/ 8 | 9 | #include 10 | 11 | using namespace std; 12 | 13 | class Base { 14 | int b; 15 | }; 16 | 17 | class Child : public Base { 18 | int c; 19 | }; 20 | 21 | void printSize(Base b) { 22 | cout << "printSize(): " << sizeof(b) << endl; 23 | } 24 | 25 | int main() { 26 | 27 | Child c; 28 | 29 | cout << "sizeof Child: " << sizeof(c) << endl; 30 | printSize(c); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex20.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/15. Polymorphism/ex20.cpp -------------------------------------------------------------------------------- /Volume 1/15. Polymorphism/ex21.out: -------------------------------------------------------------------------------- 1 | Base1 constructor 2 | Member1 constructor 3 | Member2 constructor 4 | Derived1 constructor 5 | Member3 constructor 6 | Member4 constructor 7 | Derived2 constructor 8 | Derived2::f() 9 | Derived2 destructor 10 | Member4 destructor 11 | Member3 destructor 12 | Derived1 destructor 13 | Member2 destructor 14 | Member1 destructor 15 | Base1 destructor 16 | Derived2 destructor 17 | Member4 destructor 18 | Member3 destructor 19 | Derived1 destructor 20 | Member2 destructor 21 | Member1 destructor 22 | Base1 destructor 23 | -------------------------------------------------------------------------------- /Volume 1/16. Templates intro/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Implement the inheritance hierarchy in the OShape diagram in this chapter. 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Object { 10 | public: 11 | virtual ~Object() {} 12 | }; 13 | 14 | class Shape { 15 | public: 16 | virtual ~Shape() {} 17 | }; 18 | 19 | class Circle : public Shape { 20 | public: 21 | ~Circle() {} 22 | }; 23 | 24 | class Square : public Shape { 25 | public: 26 | ~Square() {} 27 | }; 28 | 29 | class Triangle : public Shape { 30 | public: 31 | ~Triangle() {} 32 | }; 33 | 34 | class OShape : public Object, public Shape { 35 | public: 36 | ~OShape() {} 37 | }; 38 | 39 | int main() { 40 | 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /Volume 1/16. Templates intro/ex03-04/TPStashTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify TPStash.h so that the increment value used by inflate( ) 3 | automatically resizes itself to reduce the number of times it 4 | needs to be called. For example, each time it is called it could 5 | double the increment value for use in the next call. Demonstrate 6 | this functionality by reporting whenever an inflate( ) is called, 7 | and write test code in main( ). 8 | **/ 9 | 10 | #include "TPStash.h" 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() { 16 | 17 | ifstream in("TPStashTest.cpp"); 18 | PStash stringStash; 19 | string line; 20 | while(getline(in, line)) 21 | stringStash.add(new string(line)); 22 | // Print out the strings: 23 | for(int u = 0; stringStash[u]; u++) 24 | cout << "stringStash[" << u << "] = " 25 | << *stringStash[u] << endl; 26 | } ///:~ 27 | -------------------------------------------------------------------------------- /Volume 1/16. Templates intro/ex05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Templatize the fibonacci( ) function on the type of value that it produces 3 | (so it can produce long, float, etc. instead of just int). 4 | **/ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | template 11 | T fibonacci(T n) { 12 | const int sz = 100; 13 | static T f[sz]; 14 | f[0] = f[1] = 1; 15 | 16 | int i; 17 | for(i = 0; i < sz; i++) 18 | if(f[i] == 0) break; 19 | while(i <= n) { 20 | f[i] = f[i-1] + f[i-2]; 21 | i++; 22 | } 23 | return f[n]; 24 | } 25 | 26 | 27 | int main() { 28 | 29 | cout << fibonacci(11) << endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/03.txt: -------------------------------------------------------------------------------- 1 | one two three 2 | four 3 | five six seven 4 | eight nine 5 | ten -------------------------------------------------------------------------------- /Volume 1/2. Objects/04.txt: -------------------------------------------------------------------------------- 1 | one two three two five 2 | four one 3 | five six seven six six 4 | eight nine three 5 | ten one eight eight 6 | seven ten eight 7 | nine -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Hello.cpp so that it prints out your name and age. 3 | Compile and run the program. 4 | **/ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | cout << "Hello, World! My name is Sergey and I am " 13 | << 29 << " years old!" << endl; 14 | } 15 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a program that asks for the radius of a circle and prints the area of that circle. 3 | You can just use the ‘*’ operator to square the radius. 4 | Do not try to print out the value as octal or hex 5 | (these only work with integral types). 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | const double pi = 3.14; 15 | 16 | int radius; 17 | 18 | cout << "Please enter the cicrle radius: "; 19 | cin >> radius; 20 | 21 | double area; 22 | area = pi * radius * radius; 23 | 24 | cout << "Circle area: " << area << endl; 25 | 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a program that opens a file and counts the whitespace-separated words in that file. 3 | **/ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | int counter = 0; 14 | ifstream inputFile("03.txt"); 15 | string buf; 16 | 17 | while (inputFile >> buf) 18 | ++counter; 19 | 20 | cout << "Whitespace-separated words: " << counter << endl; 21 | 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex04.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a program that counts the occurrence of a particular word in a file 3 | (use the string class’ operator ‘==’ to find the word). 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | int counter = 0; 15 | string key; 16 | 17 | cout << "Please enter the word for search: "; 18 | cin >> key; 19 | 20 | ifstream inputFile("04.txt"); 21 | string buf; 22 | 23 | while (inputFile >> buf) { 24 | 25 | if (key == buf) 26 | ++counter; 27 | } 28 | 29 | cout << "Word " << key << " occurs " << counter << " time(s)." << endl; 30 | 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Change Fillvector.cpp so that it prints the lines (backwards) from last to first. 3 | **/ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | vector v; 13 | ifstream in("ex05.cpp"); 14 | string line; 15 | while(getline(in, line)) 16 | v.push_back(line); 17 | 18 | for(int i = v.size()-1; i >= 0; i--) 19 | cout << i << ": " << v[i] << endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex06.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Change Fillvector.cpp so that it concatenates all the elements in the vector 3 | into a single string before printing it out, but don’t try to add line numbering. 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | vector v; 15 | ifstream in("ex06.cpp"); 16 | string line; 17 | string overall; 18 | 19 | while(getline(in, line)) 20 | v.push_back(line); 21 | 22 | for(int i = 0; i < v.size(); i++) 23 | overall = overall + v[i] + '\n'; 24 | 25 | cout << overall << endl; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex07.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Display a file a line at a time, waiting for the user to press the “Enter” key after each line. 3 | **/ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | ifstream inputFile("ex07.cpp"); 13 | string buf; 14 | 15 | while ( getline(inputFile, buf) ) { 16 | cout << buf; 17 | cin.get(); 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/2. Objects/ex08.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a vector and put 25 floating-point numbers into it using a for loop. Display the vector. 3 | **/ 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | vector vec; 12 | 13 | for (int i = 0; i < 25; i++) 14 | vec.push_back(i + 0.3); 15 | 16 | for (int index = 0; index < vec.size(); index++) 17 | cout << "vec[" << index << "] = " << vec[index] << endl; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex01/header.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "header.h" 3 | 4 | using namespace std; 5 | 6 | void func1(int x, double y) { 7 | cout << "Called func1(" << x << ", " << y << "), returns void." << endl; 8 | } 9 | 10 | 11 | char func2() { 12 | cout << "Called func2(), returns char." << endl; 13 | } 14 | 15 | int func3(int x) { 16 | cout << "Called func3(" << x << "), returns int." << endl; 17 | } 18 | 19 | float func4(double a, char b) { 20 | cout << "Called func4("<< a << ", " << b << "), returns float." << endl; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex01/header.h: -------------------------------------------------------------------------------- 1 | #ifndef HEADER_H_INCLUDED 2 | #define HEADER_H_INCLUDED 3 | 4 | void func1(int , double ); 5 | char func2(); 6 | int func3(int ); 7 | float func4(double , char); 8 | 9 | 10 | 11 | #endif // HEADER_H_INCLUDED 12 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex01/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a header file (with an extension of ‘.h’). In this file, 3 | declare a group of functions by varying the argument lists and 4 | return values from among the following: void, char, int, and float. 5 | Now create a .cpp file that includes your header file and creates 6 | definitions for all of these functions. Each definition should simply 7 | print out the function name, argument list, and return type so you know 8 | it’s been called. Create a second .cpp file that includes your header 9 | file and defines int main( ), containing calls to all of your functions. 10 | **/ 11 | 12 | #include 13 | #include "header.h" 14 | 15 | using namespace std; 16 | 17 | int main() { 18 | 19 | func1(2 , 2.2); 20 | func2(); 21 | func3(3); 22 | func4(4.6, 'z'); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that uses two nested for loops and the modulus 3 | operator (%) to detect and print prime numbers (integral numbers 4 | that are not evenly divisible by any other numbers except for themselves and 1). 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | const int limit = 100; 14 | int counter; 15 | 16 | for (int number = 1; number < limit; number++) { 17 | 18 | counter = 0; 19 | 20 | for (int i = 1; i <= number; i++) 21 | if (number % i == 0) 22 | ++counter; 23 | 24 | if (2 == counter) 25 | cout << number << " "; 26 | } 27 | 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that evaluates the two expressions in the section labeled "precedence.": 3 | 1) A = X + Y - 2/2 + Z; 4 | 2) A = X + (Y - 2)/(2 + Z); 5 | (Try evaluating the result with X = 1, Y = 2, and Z = 3.) 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | int X = 1, Y = 2, Z = 3; 15 | 16 | cout << "X = 1, Y = 2, and Z = 3\n" << endl; 17 | cout << "A = X + Y - 2/2 + Z = " << X + Y - 2/2 + Z << endl; 18 | cout << "A = X + (Y - 2)/(2 + Z) = " << X + (Y - 2)/(2 + Z) << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex06.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify YourPets2.cpp so that it uses various different data types 3 | (char, int, float, double, and their variants). Run the program 4 | and create a map of the resulting memory layout 5 | **/ 6 | 7 | #include 8 | using namespace std; 9 | 10 | char dog; 11 | int cat; 12 | float bird; 13 | double fish; 14 | 15 | void f(int pet) { 16 | cout << "pet id number: " << pet << endl; 17 | } 18 | 19 | int main() { 20 | long int i; 21 | short int j; 22 | long double k; 23 | cout << "f(): " << (long)&f << endl; 24 | cout << "dog: " << (long)&dog << endl; 25 | cout << "cat: " << (long)&cat << endl; 26 | cout << "bird: " << (long)&bird << endl; 27 | cout << "fish: " << (long)&fish << endl; 28 | cout << "i: " << (long)&i << endl; 29 | cout << "j: " << (long)&j << endl; 30 | cout << "k: " << (long)&k << endl; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex07.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create two functions, one that takes a string* and one that 3 | takes a string&. Each of these functions should modify the 4 | outside string object in its own unique way. In main( ), 5 | create and initialize a string object, print it, then pass 6 | it to each of the two functions, printing the results. 7 | **/ 8 | 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | void func1(string *str) { 15 | *str += " modified by func1()"; 16 | } 17 | 18 | void func2(string &str) { 19 | str += " and by func2()"; 20 | } 21 | 22 | int main() { 23 | 24 | string line = "New string"; 25 | 26 | cout << "Initial string: " << line << endl; 27 | func1(&line); 28 | cout << "After first function: " << line << endl; 29 | func2(line); 30 | cout << "After second function: " << line << endl; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex08.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that uses all the trigraphs to see if your compiler supports them. 3 | 4 | Trigraphs: 5 | ??= # 6 | ??/ \ 7 | ??' ^ 8 | ??( [ 9 | ??) ] 10 | ??! | 11 | ??< { 12 | ??> } 13 | ??- ~ 14 | 15 | 16 | Compilation command: g++ ex08.cpp -trigraphs -o ex08 17 | 18 | **/ 19 | ??=include 20 | 21 | using namespace std; 22 | 23 | int main() { 24 | 25 | int a??( 10 ??) ; 26 | 27 | cout << "Hello ??/ 28 | World!" << endl; 29 | 30 | if ( 1 > 2 ??!??! 2 > 3) ??< 31 | ??> 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex09.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Compile and run Static.cpp. Remove the static keyword from the code, 3 | compile and run it again, and explain what happens. 4 | **/ 5 | 6 | #include 7 | using namespace std; 8 | 9 | void func() { 10 | /*static*/ int i = 0; 11 | cout << "i = " << ++i << endl; 12 | } 13 | 14 | int main() { 15 | for(int x = 0; x < 10; x++) 16 | func(); 17 | } ///:~ 18 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex10/FileStatic.cpp: -------------------------------------------------------------------------------- 1 | //: C03:FileStatic.cpp 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // File scope demonstration. Compiling and 7 | // linking this file with FileStatic2.cpp 8 | // will cause a linker error 9 | 10 | // File scope means only available in this file: 11 | static int fs; 12 | 13 | int main() { 14 | fs = 1; 15 | } ///:~ 16 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex10/FileStatic2.cpp: -------------------------------------------------------------------------------- 1 | //: C03:FileStatic2.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Trying to reference fs 7 | extern int fs; 8 | void func() { 9 | fs = 100; 10 | } ///:~ 11 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex11.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Boolean.cpp so that it works with double values instead of ints. 3 | **/ 4 | #include 5 | using namespace std; 6 | 7 | int main() { 8 | double i,j; 9 | cout << "Enter a double: "; 10 | cin >> i; 11 | cout << "Enter another double: "; 12 | cin >> j; 13 | cout << "i > j is " << (i > j) << endl; 14 | cout << "i < j is " << (i < j) << endl; 15 | cout << "i >= j is " << (i >= j) << endl; 16 | cout << "i <= j is " << (i <= j) << endl; 17 | cout << "i == j is " << (i == j) << endl; 18 | cout << "i != j is " << (i != j) << endl; 19 | cout << "i && j is " << (i && j) << endl; 20 | cout << "i || j is " << (i || j) << endl; 21 | cout << " (i < 10) && (j < 10) is " 22 | << ((i < 10) && (j < 10)) << endl; 23 | } ///:~ 24 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex12/Boolean.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Boolean.cpp and Bitwise.cpp so they use the explicit operators 3 | (if your compiler is conformant to the C++ Standard it will support these). 4 | **/ 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int i,j; 10 | cout << "Enter an integer: "; 11 | cin >> i; 12 | cout << "Enter another integer: "; 13 | cin >> j; 14 | cout << "i > j is " << (i > j) << endl; 15 | cout << "i < j is " << (i < j) << endl; 16 | cout << "i >= j is " << (i >= j) << endl; 17 | cout << "i <= j is " << (i <= j) << endl; 18 | cout << "i == j is " << (i == j) << endl; 19 | cout << "i != j is " << (i != j) << endl; 20 | cout << "i and j is " << (i and j) << endl; 21 | cout << "i or j is " << (i or j) << endl; 22 | cout << " (i < 10) and (j < 10) is " 23 | << ((i < 10) and (j < 10)) << endl; 24 | } ///:~ 25 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex14.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Ifthen.cpp to use the ternary if-else operator (?:). 3 | **/ 4 | 5 | #include 6 | using namespace std; 7 | 8 | int main() { 9 | int i; 10 | cout << "type a number and 'Enter'" << endl; 11 | cin >> i; 12 | 13 | (i > 5) ? 14 | cout << "It's greater than 5" << endl 15 | : 16 | (i < 5) ? cout << "It's less than 5 " << endl : cout << "It's equal to 5 " << endl; 17 | 18 | cout << "type a number and 'Enter'" << endl; 19 | cin >> i; 20 | 21 | (i < 10) ? 22 | (i > 5) ? cout << "5 < i < 10" << endl : cout << "i <= 5" << endl 23 | : 24 | cout << "i >= 10" << endl; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex17.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Experiment with Union.cpp by removing various union elements to see the 3 | effects on the size of the resulting union. Try assigning to one element 4 | (thus one type) of the union and printing out a via a different element 5 | (thus a different type) to see what happens. 6 | **/ 7 | 8 | #include 9 | using namespace std; 10 | 11 | union Packed { 12 | char i; 13 | short j; 14 | int k; 15 | long l; 16 | //float f; 17 | double d; 18 | }; 19 | 20 | int main() { 21 | cout << "sizeof(Packed) = " 22 | << sizeof(Packed) << endl; 23 | Packed x; 24 | x.i = 'c'; 25 | cout << x.i << endl; 26 | x.d = 1.23; 27 | cout << x.d << endl; 28 | 29 | x.j = 2; 30 | cout << x.k << ' ' << x.i << endl; // Prints ubdefined data 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex21.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create an array of string objects and assign a string to each element. Print out the array using a for loop. 3 | **/ 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main() { 10 | 11 | const int SIZE = 3; 12 | 13 | string s[SIZE]; 14 | 15 | s[0] = "line one"; 16 | s[1] = "line two"; 17 | s[2] = "line three"; 18 | 19 | for (int i = 0; i < SIZE; i++) 20 | cout << s[i] << endl; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex22.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create two new programs starting from ArgsToInts.cpp so they use atol( ) and atof( ), respectively. 3 | **/ 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char* argv[]) { 10 | for(int i = 1; i < argc; i++) 11 | cout << atoi(argv[i]) << " " << atol(argv[i]) << " " << atof(argv[i]) << endl; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex23.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify PointerIncrement2.cpp so it uses a union instead of a struct. 3 | **/ 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | //typedef struct { 10 | typedef union { 11 | char c; 12 | short s; 13 | int i; 14 | long l; 15 | float f; 16 | double d; 17 | long double ld; 18 | } Primitives; 19 | 20 | int main() { 21 | Primitives p[10]; 22 | Primitives* pp = p; 23 | cout << "sizeof(Primitives) = " 24 | << sizeof(Primitives) << endl; 25 | cout << "pp = " << (long)pp << endl; 26 | pp++; 27 | cout << "pp = " << (long)pp << endl; 28 | } ///:~ 29 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex24.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify PointerArithmetic.cpp to work with long and long double. 3 | **/ 4 | 5 | #include 6 | using namespace std; 7 | 8 | #define P(EX) cout << #EX << ": " << EX << endl; 9 | 10 | int main() { 11 | int a[10]; 12 | //long double a[10]; 13 | for(int i = 0; i < 10; i++) 14 | a[i] = i; 15 | int* ip = a; 16 | //long double* ip = a; 17 | P(*ip); 18 | P(*++ip); 19 | P(*(ip + 5)); 20 | int* ip2 = ip + 5; 21 | //long double* ip2 = ip + 5; 22 | P(*ip2); 23 | P(*(ip2 - 4)); 24 | P(*--ip2); 25 | P(ip2 - ip); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex30/YourPets1.cpp: -------------------------------------------------------------------------------- 1 | //: C03:YourPets1.cpp 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #include 7 | using namespace std; 8 | 9 | int dog, cat, bird, fish; 10 | 11 | void f(int pet) { 12 | cout << "pet id number: " << pet << endl; 13 | } 14 | 15 | int main() { 16 | int i, j, k; 17 | } ///:~ 18 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex30/YourPets2.cpp: -------------------------------------------------------------------------------- 1 | //: C03:YourPets2.cpp 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #include 7 | using namespace std; 8 | 9 | int dog, cat, bird, fish; 10 | 11 | void f(int pet) { 12 | cout << "pet id number: " << pet << endl; 13 | } 14 | 15 | int main() { 16 | int i, j, k; 17 | cout << "f(): " << (long)&f << endl; 18 | cout << "dog: " << (long)&dog << endl; 19 | cout << "cat: " << (long)&cat << endl; 20 | cout << "bird: " << (long)&bird << endl; 21 | cout << "fish: " << (long)&fish << endl; 22 | cout << "i: " << (long)&i << endl; 23 | cout << "j: " << (long)&j << endl; 24 | cout << "k: " << (long)&k << endl; 25 | } ///:~ 26 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex30/makefile: -------------------------------------------------------------------------------- 1 | CPP = g++ 2 | OFLAG = -o 3 | 4 | .SUFFIXES: .o .cpp 5 | .cpp.o: 6 | $(CPP) $(CPPFLAGS) -c $< 7 | 8 | all: \ 9 | YourPets1 \ 10 | YourPets2 11 | 12 | YourPets1: YourPets1.o 13 | $(CPP) $(OFLAG) YourPets1 YourPets1.o 14 | 15 | YourPets2: YourPets2.o 16 | $(CPP) $(OFLAG) YourPets2 YourPets2.o 17 | 18 | YourPets1.o: YourPets1.cpp 19 | YourPets2.o: YourPets2.cpp -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex31.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify StringizingExpressions.cpp so that P(A) is conditionally #ifdefed to allow the 3 | debugging code to be automatically stripped out by setting a command-line flag. 4 | You will need to consult your compiler’s documentation to see how to define and undefine 5 | preprocessor values on the compiler command line. 6 | **/ 7 | 8 | /* 9 | Compile with: g++ -DDEBUG ex31.cpp -o ex31.exe 10 | */ 11 | 12 | 13 | #include 14 | 15 | using namespace std; 16 | 17 | #ifdef DEBUG 18 | #define P(A) cout << #A << " (debug version): " << (A) << endl; 19 | #else 20 | #define P(A) cout << #A << ": " << (A) << endl; 21 | #endif 22 | 23 | int main() { 24 | int a = 1, b = 2, c = 3; 25 | P(a); P(b); P(c); 26 | P(a + b); 27 | P((c - a)/b); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex32.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Define a function that takes a double argument and returns an int. 3 | Create and initialize a pointer to this function, and call the function through your pointer. 4 | **/ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int foo(double ); 11 | 12 | int main() { 13 | 14 | int (*fPtr)(double); 15 | fPtr = &foo; 16 | 17 | (*fPtr)(2.2); 18 | 19 | return 0; 20 | } 21 | 22 | 23 | int foo(double d) { 24 | 25 | cout << "Called foo(" << d << ")" << endl; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex33.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Declare a pointer to a function taking an int argument and returning 3 | a pointer to a function that takes a char argument and returns a float. 4 | **/ 5 | 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | float (*(*fPtr)(int))(char); 13 | 14 | // OR: 15 | 16 | typedef float (*fPtr1)(char); 17 | fPtr1 (*fPtr2)(int); 18 | 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex34.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify FunctionTable.cpp so that each function returns a string (instead of printing out a message) 3 | and so that this value is printed inside of main(). 4 | **/ 5 | 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | 11 | // A macro to define dummy functions: 12 | #define DF(N) string N() { \ 13 | return "function " #N " called..."; } 14 | 15 | DF(a) 16 | DF(b) 17 | DF(c) 18 | DF(d) 19 | 20 | string (*func_table[])() = { a, b, c, d }; 21 | 22 | int main() { 23 | 24 | while(1) { 25 | cout << "press a key from 'a' to 'd' " 26 | "or q to quit" << endl; 27 | char c, cr; 28 | cin.get(c); cin.get(cr); // second one for CR 29 | if ( c == 'q' ) 30 | break; // ... out of while(1) 31 | if ( c < 'a' || c > 'd' ) 32 | continue; 33 | cout << (*func_table[c - 'a'])() << endl; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Volume 1/3. C and CPP/ex35/makefile: -------------------------------------------------------------------------------- 1 | CPP = g++ 2 | 3 | all: 4 | $(CPP) ex35.cpp -o ex35.exe 5 | 6 | debug: 7 | $(CPP) ex35.cpp -DDEBUG -o ex35.exe -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | In the Standard C library, the function puts( ) prints a char array to the console 3 | (so you can say puts("hello")). Write a C program that uses puts( ) but does not 4 | include or otherwise declare the function. Compile this program with your 5 | C compiler. (Some C++ compilers are not distinct from their C compilers; 6 | in this case you may need to discover a command-line flag that forces a C compilation.) 7 | Now compile it with the C++ compiler and note the difference. 8 | **/ 9 | 10 | 11 | //!Compilation error: 12 | //!error: 'puts' was not declared in this scope 13 | 14 | 15 | #include 16 | 17 | using namespace std; 18 | 19 | int main() { 20 | 21 | puts("hello"); 22 | 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a struct declaration with a single member function, then create a 3 | definition for that member function. Create an object of your new data type, 4 | and call the member function. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | struct MyStruct { 12 | void someFunc(); 13 | }; 14 | 15 | void MyStruct::someFunc() { 16 | 17 | cout << "someFunc() called..." << endl; 18 | 19 | } 20 | 21 | int main() { 22 | 23 | MyStruct m; 24 | m.someFunc(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex03/ex03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ex03.h" 3 | 4 | void MyStruct::someFunc() { 5 | 6 | std::cout << "someFunc() called..." << std::endl; 7 | 8 | } 9 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex03/ex03.h: -------------------------------------------------------------------------------- 1 | #ifndef EX03_H_INCLUDED 2 | #define EX03_H_INCLUDED 3 | 4 | struct MyStruct { 5 | void someFunc(); 6 | }; 7 | 8 | #endif // EX03_H_INCLUDED 9 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex03/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Change your solution to Exercise 2 so the struct is declared in a 3 | properly "guarded" header file, with the definition in 4 | one cpp file and your main( ) in another. 5 | **/ 6 | 7 | #include "ex03.h" 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | MyStruct m; 14 | m.someFunc(); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex04.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a struct with a single int data member, and two global functions, 3 | each of which takes a pointer to that struct. The first function has 4 | a second int argument and sets the struct’s int to the argument value, 5 | the second displays the int from the struct. Test the functions. 6 | **/ 7 | #include 8 | 9 | using namespace std; 10 | 11 | struct MyStruct { 12 | int s_value; 13 | }; 14 | 15 | // Global functions 16 | 17 | void changeValue(MyStruct* sPtr, int x) { 18 | sPtr->s_value = x; 19 | } 20 | 21 | void printValue(MyStruct* sPtr) { 22 | cout << "MyStruct: s_value = " << sPtr->s_value << endl; 23 | } 24 | 25 | // Main 26 | 27 | int main() { 28 | 29 | MyStruct s; 30 | changeValue(&s, 123); 31 | printValue(&s); 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Repeat Exercise 4 but move the functions so they are member functions of the struct, and test again. 3 | **/ 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct MyStruct { 9 | int s_value; 10 | 11 | void changeValue(int x); 12 | void printValue(); 13 | }; 14 | 15 | void MyStruct::changeValue(int x) { 16 | s_value = x; 17 | } 18 | 19 | void MyStruct::printValue() { 20 | cout << "MyStruct: s_value = " << s_value << endl; 21 | } 22 | 23 | // Main 24 | 25 | int main() { 26 | 27 | MyStruct s; 28 | s.changeValue(123); 29 | s.printValue(); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex06.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class that (redundantly) performs data member selection 3 | and a member function call using the this keyword 4 | (which refers to the address of the current object). 5 | **/ 6 | #include 7 | 8 | using namespace std; 9 | 10 | class MyClass { 11 | public: 12 | void changeValue(int x); 13 | void notification(); 14 | void printValue(); 15 | private: 16 | int m_value; 17 | }; 18 | 19 | void MyClass::changeValue(int x) { 20 | this->notification(); 21 | this->m_value = x; 22 | } 23 | 24 | void MyClass::notification() { 25 | cout << "Changing value..." << endl; 26 | } 27 | 28 | void MyClass::printValue() { 29 | cout << "MyClass: m_value = " << this->m_value << endl; 30 | } 31 | 32 | 33 | // Main 34 | 35 | int main() { 36 | 37 | MyClass test; 38 | test.changeValue(123); 39 | test.printValue(); 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex07/CLib.h: -------------------------------------------------------------------------------- 1 | //: C04:CLib.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Header file for a C-like library 7 | // An array-like entity created at runtime 8 | 9 | typedef struct CStashTag { 10 | int size; // Size of each space 11 | int quantity; // Number of storage spaces 12 | int next; // Next empty space 13 | // Dynamically allocated array of bytes: 14 | unsigned char* storage; 15 | } CStash; 16 | 17 | void initialize(CStash* s, int size); 18 | void cleanup(CStash* s); 19 | int add(CStash* s, const void* element); 20 | void* fetch(CStash* s, int index); 21 | int count(CStash* s); 22 | void inflate(CStash* s, int increase); 23 | ///:~ 24 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex07/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Make a Stash that holds doubles. Fill it with 25 double values, 3 | then print them out to the console. 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "CLib.h" 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | 16 | CStash doubleStash; 17 | int i; 18 | char* cp; 19 | ifstream in; 20 | string line; 21 | const int bufsize = 80; 22 | 23 | initialize(&doubleStash, sizeof(double)); 24 | 25 | for(i = 0; i < 100; i++) { 26 | double d = i + 1; 27 | add(&doubleStash, &d); 28 | } 29 | 30 | for(i = 0; i < count(&doubleStash); i++) 31 | cout << "fetch(&doubleStash, " << i << ") = " 32 | << *(double*)fetch(&doubleStash, i) 33 | << endl; 34 | 35 | cleanup(&doubleStash); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex08/Stack.h: -------------------------------------------------------------------------------- 1 | //: C04:Stack.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Nested struct in linked list 7 | #ifndef STACK_H 8 | #define STACK_H 9 | 10 | struct Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | void initialize(void* dat, Link* nxt); 15 | }* head; 16 | void initialize(); 17 | void push(void* dat); 18 | void* peek(); 19 | void* pop(); 20 | void cleanup(); 21 | }; 22 | #endif // STACK_H ///:~ 23 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex08/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Repeat Exercise 7 with Stack: 3 | Make a Stach (Stack) that holds doubles. Fill it with 25 double values, 4 | then print them out to the console. 5 | **/ 6 | 7 | #include 8 | #include 9 | #include 10 | #include "Stack.h" 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | 16 | Stack doubleStack; 17 | doubleStack.initialize(); 18 | 19 | double modifier = 0.1; 20 | const int stack_size = 25; 21 | 22 | for (int i = 0; i < stack_size; i++) 23 | doubleStack.push(new double (i * modifier + modifier)); 24 | 25 | double *dPtr; 26 | 27 | while ( (dPtr = (double*) doubleStack.pop()) != nullptr ) { 28 | 29 | cout << *dPtr << endl; 30 | delete dPtr; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex09/ex09.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/4. Abstraction/ex09/ex09.cpp -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex09/ex09.h: -------------------------------------------------------------------------------- 1 | #ifndef EX09_H_INCLUDED 2 | #define EX09_H_INCLUDED 3 | 4 | #include 5 | 6 | void f(int x) { 7 | 8 | printf("%d\n", x); 9 | 10 | } 11 | 12 | #endif // EX09_H_INCLUDED 13 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Find out how to produce assembly language from your C and C++ 3 | compilers. Write a struct with a single member function in C++. 4 | Produce assembly language from each and find the function names 5 | that are produced by your C++ member function, so you can see 6 | what sort of name decoration occurs inside the compiler. 7 | **/ 8 | 9 | // Compilation params: g++ -S -masm=intel -g ex10.cpp -o ex10.s 10 | 11 | #include 12 | 13 | using namespace std; 14 | 15 | struct MyStruct { 16 | void someFunc(int x) {} 17 | }; 18 | 19 | int main() { 20 | 21 | MyStruct m; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex11.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program with conditionally-compiled code in main( ), so that when 3 | a preprocessor value is defined one message is printed, but when it is not 4 | defined another message is printed. Compile this code experimenting with a 5 | #define within the program, then discover the way your compiler takes preprocessor 6 | definitions on the command line and experiment with that. 7 | **/ 8 | 9 | /* 10 | Compile WITH or WITHOUT next params: g++ -DTEST ex11.cpp -o ex11.exe 11 | */ 12 | 13 | #include 14 | 15 | using namespace std; 16 | 17 | int main() { 18 | 19 | #ifdef TEST 20 | cout << "Constant was defined." << endl; 21 | #endif 22 | 23 | #ifndef TEST 24 | cout << "Constant wasn't defined." << endl; 25 | #endif 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex12.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that uses assert( ) with an argument that is always 3 | false (zero) to see what happens when you run it. Now compile it with 4 | #define NDEBUG and run it again to see the difference. 5 | **/ 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | assert(0); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex13/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "video.h" 3 | 4 | using namespace std; 5 | 6 | int main() { 7 | 8 | Video v; 9 | v.print(); 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex13/video.cpp: -------------------------------------------------------------------------------- 1 | #include "video.h" 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | void Video::print() { 7 | 8 | cout << "ID: " << getId() 9 | << "\nLength: " << getLength() 10 | << "\nPrice: " << getPrice() 11 | << "\nName: " << getName() 12 | << "\n----------" << endl; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex13/video.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_H_INCLUDED 2 | #define VIDEO_H_INCLUDED 3 | 4 | #include 5 | #include 6 | using std::string; 7 | 8 | struct Video { 9 | 10 | Video() : 11 | id(0), 12 | length(0), 13 | price(0), 14 | name("-") {} 15 | 16 | int id; 17 | int length; 18 | int price; 19 | string name; 20 | 21 | void print(); 22 | 23 | void setId(int x) { id = x; } 24 | int getId() { return id; } 25 | 26 | void setLength(int x) { length = x; } 27 | int getLength() { return length; } 28 | 29 | void setPrice(int x) { price = x; } 30 | int getPrice() { return price; } 31 | 32 | void setName(string x) { name = x; } 33 | string getName() { return name; } 34 | }; 35 | 36 | #endif // VIDEO_H_INCLUDED 37 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex14/Stack.h: -------------------------------------------------------------------------------- 1 | //: C04:Stack.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Nested struct in linked list 7 | #ifndef STACK_H 8 | #define STACK_H 9 | 10 | struct Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | void initialize(void* dat, Link* nxt); 15 | }* head; 16 | void initialize(); 17 | void push(void* dat); 18 | void* peek(); 19 | void* pop(); 20 | void cleanup(); 21 | }; 22 | #endif // STACK_H ///:~ 23 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex14/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a Stack object to hold the Video objects from Exercise 13. Create several Video objects, 3 | store them in the Stack, then display them using Video::print( ). 4 | **/ 5 | 6 | #include 7 | #include "video.h" 8 | #include "Stack.h" 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | Stack videoStack; 15 | videoStack.initialize(); 16 | 17 | videoStack.push(new Video(1, 75, 200, "Title #1")); 18 | videoStack.push(new Video(2, 100, 221, "Title #2")); 19 | videoStack.push(new Video(2, 100, 221, "Title #3")); 20 | 21 | Video *vPtr; 22 | 23 | while ( (vPtr = (Video*) videoStack.pop()) != nullptr) { 24 | vPtr->print(); 25 | delete vPtr; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex14/video.cpp: -------------------------------------------------------------------------------- 1 | #include "video.h" 2 | 3 | using std::cout; 4 | using std::endl; 5 | 6 | void Video::print() { 7 | 8 | cout << "ID: " << getId() 9 | << "\nLength: " << getLength() 10 | << "\nPrice: " << getPrice() 11 | << "\nName: " << getName() 12 | << "\n----------" << endl; 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex14/video.h: -------------------------------------------------------------------------------- 1 | #ifndef VIDEO_H_INCLUDED 2 | #define VIDEO_H_INCLUDED 3 | 4 | #include 5 | #include 6 | using std::string; 7 | 8 | struct Video { 9 | 10 | Video() : 11 | id(0), length(0), price(0), name("-") {} 12 | 13 | 14 | Video(int i, int len, int p, string str) : 15 | id(i), length(len), price(p), name(str) {} 16 | 17 | int id; 18 | int length; 19 | int price; 20 | string name; 21 | 22 | void print(); 23 | 24 | void setId(int x) { id = x; } 25 | int getId() { return id; } 26 | 27 | void setLength(int x) { length = x; } 28 | int getLength() { return length; } 29 | 30 | void setPrice(int x) { price = x; } 31 | int getPrice() { return price; } 32 | 33 | void setName(string x) { name = x; } 34 | string getName() { return name; } 35 | }; 36 | 37 | #endif // VIDEO_H_INCLUDED 38 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex16/CLib.h: -------------------------------------------------------------------------------- 1 | //: C04:CLib.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Header file for a C-like library 7 | // An array-like entity created at runtime 8 | 9 | #include 10 | 11 | typedef struct CStashTag { 12 | int size; // Size of each space 13 | int quantity; // Number of storage spaces 14 | int next; // Next empty space 15 | // Dynamically allocated array of bytes: 16 | std::vector storage; 17 | } CStash; 18 | 19 | void initialize(CStash* s, int size); 20 | void cleanup(CStash* s); 21 | int add(CStash* s, const void* element); 22 | void* fetch(CStash* s, int index); 23 | int count(CStash* s); 24 | void inflate(CStash* s, int increase); 25 | 26 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex17.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Dynamically create pieces of storage of the following types, using new: 3 | int, long, an array of 100 chars, an array of 100 floats. 4 | Print the addresses of these and then free the storage using delete. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | int *intPtr = new int(1); 14 | long *longPtr = new long(123); 15 | char *charPtr = new char[100]; 16 | float *floatPtr = new float[100]; 17 | 18 | cout << "Addresses: " 19 | << "\nInt pointer: \t\t" << intPtr 20 | << "\nLong pointer: \t\t" << longPtr 21 | << "\nChar[] pointer: \t" << (void*)charPtr // Address output 22 | << "\nFloat[] pointer: \t" << floatPtr << endl; 23 | 24 | delete intPtr; 25 | delete longPtr; 26 | delete []charPtr; 27 | delete []floatPtr; 28 | 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex19.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Show an example of a structure declared within another structure (a nested structure). 3 | Declare data members in both structs, and declare and define member functions in both structs. 4 | Write a main( ) that tests your new types. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | struct External { 12 | struct Internal { 13 | int x; 14 | void printInternalX() {cout << "Internal X: " << x << endl; } 15 | } data; 16 | int x; 17 | void printExternalX() {cout << "External X: " << x << endl; } 18 | }; 19 | 20 | int main() { 21 | 22 | External e; 23 | 24 | e.x = 6; 25 | e.data.x = 666; 26 | 27 | e.printExternalX(); 28 | e.data.printInternalX(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex21.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | C++ automatically creates the equivalent of a typedef for structs, as you’ve seen 3 | in this chapter. It also does this for enumerations and unions. 4 | Write a small program that demonstrates this. 5 | **/ 6 | #include 7 | 8 | using namespace std; 9 | 10 | int main() { 11 | 12 | struct MyStruct() { 13 | int x; 14 | void initialize() {cout << "MyStruct initialized." << endl; } 15 | } s; 16 | 17 | union MyUnion { 18 | int a; 19 | char b; 20 | } u; 21 | 22 | enum COLORS {RED = 1, GREEN = 2;} c; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex22/Stack.cpp: -------------------------------------------------------------------------------- 1 | //: C04:Stack.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Linked list with nesting 7 | #include "Stack.h" 8 | using namespace std; 9 | 10 | void Stack::Link::initialize(void* dat, Link* nxt) { 11 | data = dat; 12 | next = nxt; 13 | } 14 | 15 | void Stack::initialize() { head = 0; } 16 | 17 | void Stack::push(void* dat) { 18 | Link* newLink = new Link; 19 | newLink->initialize(dat, head); 20 | head = newLink; 21 | } 22 | 23 | void* Stack::peek() { 24 | return head->data; 25 | } 26 | 27 | void* Stack::pop() { 28 | if(head == 0) return 0; 29 | void* result = head->data; 30 | Link* oldHead = head; 31 | head = head->next; 32 | delete oldHead; 33 | return result; 34 | } 35 | 36 | void Stack::cleanup() { 37 | 38 | } ///:~ 39 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex22/Stack.h: -------------------------------------------------------------------------------- 1 | //: C04:Stack.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Nested struct in linked list 7 | #ifndef STACK_H 8 | #define STACK_H 9 | 10 | struct Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | void initialize(void* dat, Link* nxt); 15 | }* head; 16 | void initialize(); 17 | void push(void* dat); 18 | void* peek(); 19 | void* pop(); 20 | void cleanup(); 21 | }; 22 | #endif // STACK_H ///:~ 23 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex22/Stash.h: -------------------------------------------------------------------------------- 1 | #ifndef STASH_H 2 | #define STASH_H 3 | 4 | #include 5 | 6 | using std::vector; 7 | 8 | struct Stash { 9 | Stash() {} 10 | int size; 11 | int quantity; 12 | int next; 13 | 14 | vector storage; 15 | 16 | void initialize(int size); 17 | int add(const void* element); 18 | void* fetch(int index); 19 | int count(); 20 | void inflate(int increase); 21 | }; 22 | 23 | #endif // STASH_H -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex23/External.h: -------------------------------------------------------------------------------- 1 | #ifndef EXTERNAL_H_INCLUDED 2 | #define EXTERNAL_H_INCLUDED 3 | 4 | #include "Stack.h" 5 | #include "Stash.h" 6 | #include 7 | 8 | struct ExternalStruct { 9 | 10 | ExternalStruct() { stashStack.initialize(); reverseStack.initialize(); } 11 | 12 | Stack stashStack; 13 | Stack reverseStack; 14 | 15 | void add_stash(Stash* dat){ stashStack.push(dat); } 16 | Stash* remove_stash() { return (Stash*)reverseStack.pop(); } 17 | 18 | void reverse() { 19 | Stash *sPointer = 0; 20 | while((sPointer = (Stash*)stashStack.pop()) != 0) 21 | reverseStack.push(sPointer); 22 | std::cout << "Reversed." << std::endl; 23 | } 24 | }; 25 | 26 | 27 | #endif // EXTERNAL_H_INCLUDED 28 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex23/Stack.cpp: -------------------------------------------------------------------------------- 1 | //: C04:Stack.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Linked list with nesting 7 | #include "Stack.h" 8 | using namespace std; 9 | 10 | void Stack::Link::initialize(void* dat, Link* nxt) { 11 | data = dat; 12 | next = nxt; 13 | } 14 | 15 | void Stack::initialize() { head = 0; } 16 | 17 | void Stack::push(void* dat) { 18 | Link* newLink = new Link; 19 | newLink->initialize(dat, head); 20 | head = newLink; 21 | } 22 | 23 | void* Stack::peek() { 24 | return head->data; 25 | } 26 | 27 | void* Stack::pop() { 28 | if(head == 0) return 0; 29 | void* result = head->data; 30 | Link* oldHead = head; 31 | head = head->next; 32 | delete oldHead; 33 | return result; 34 | } 35 | 36 | void Stack::cleanup() { 37 | 38 | } ///:~ 39 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex23/Stack.h: -------------------------------------------------------------------------------- 1 | //: C04:Stack.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Nested struct in linked list 7 | #ifndef STACK_H 8 | #define STACK_H 9 | 10 | struct Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | void initialize(void* dat, Link* nxt); 15 | }* head; 16 | void initialize(); 17 | void push(void* dat); 18 | void* peek(); 19 | void* pop(); 20 | void cleanup(); 21 | }; 22 | #endif // STACK_H ///:~ 23 | -------------------------------------------------------------------------------- /Volume 1/4. Abstraction/ex23/Stash.h: -------------------------------------------------------------------------------- 1 | #ifndef STASH_H 2 | #define STASH_H 3 | 4 | #include 5 | 6 | using std::vector; 7 | 8 | struct Stash { 9 | Stash() {} 10 | int size; 11 | int quantity; 12 | int next; 13 | 14 | vector storage; 15 | 16 | void initialize(int size); 17 | int add(const void* element); 18 | void* fetch(int index); 19 | int count(); 20 | void inflate(int increase); 21 | }; 22 | 23 | #endif // STASH_H -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class with public, private, and protected data members and function members. 3 | Create an object of this class and see what kind of compiler messages you get when 4 | you try to access all the class members. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class MyClass { 12 | public: 13 | MyClass() : a(0), b(0), c(0) {} 14 | int a; 15 | protected: 16 | int b; 17 | private: 18 | int c; 19 | }; 20 | 21 | int main() { 22 | 23 | MyClass m; 24 | 25 | m.a = 1; 26 | //m.b = 2; // int MyClass::b is protected 27 | //m.c = 3; // int MyClass::c is private 28 | 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class and a global friend function that manipulates the private data in the class. 3 | **/ 4 | #include 5 | 6 | using namespace std; 7 | 8 | class SomeClass { 9 | friend void changePrivateValue (SomeClass &, int ); 10 | public: 11 | SomeClass() : x (123) {} 12 | int getX() { return x; } 13 | private: 14 | int x; 15 | }; 16 | 17 | void changePrivateValue (SomeClass &s, int y) { 18 | cout << "changePrivateValue ( ) called with y = " << y << endl; 19 | s.x = y; 20 | } 21 | 22 | int main() { 23 | 24 | SomeClass s; 25 | cout << "x = " << s.getX() << endl; 26 | changePrivateValue(s, 456); 27 | cout << "x = " << s.getX() << endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex04.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write two classes, each of which has a member function that takes a pointer 3 | to an object of the other class. Create instances of both objects in main( ) 4 | and call the aforementioned member function in each class. 5 | **/ 6 | #include 7 | 8 | using namespace std; 9 | 10 | class B; 11 | 12 | class A { 13 | public: 14 | void functionA(B *); 15 | void printName() { cout << "Class A" << endl; } 16 | }; 17 | 18 | class B { 19 | public: 20 | void functionB(A *); 21 | void printName() { cout << "Class B" << endl; } 22 | }; 23 | 24 | void A::functionA(B *bPtr) { bPtr->printName(); } 25 | void B::functionB(A *aPtr) { aPtr->printName(); } 26 | 27 | int main() { 28 | 29 | A a; 30 | B b; 31 | 32 | a.functionA(&b); 33 | b.functionB(&a); 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex09/Stash.h: -------------------------------------------------------------------------------- 1 | //: C05:Stash.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Converted to use access control 7 | #ifndef STASH_H 8 | #define STASH_H 9 | 10 | class Stash { 11 | int size; // Size of each space 12 | int quantity; // Number of storage spaces 13 | int next; // Next empty space 14 | // Dynamically allocated array of bytes: 15 | unsigned char* storage; 16 | void inflate(int increase); 17 | public: 18 | void initialize(int size); 19 | void cleanup(); 20 | int add(void* element); 21 | void* fetch(int index); 22 | int count(); 23 | }; 24 | #endif // STASH_H ///:~ 25 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex10/Stash.h: -------------------------------------------------------------------------------- 1 | //: C05:Stash.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Converted to use access control 7 | #ifndef STASH_H 8 | #define STASH_H 9 | 10 | class Stash { 11 | int size; // Size of each space 12 | int quantity; // Number of storage spaces 13 | int next; // Next empty space 14 | // Dynamically allocated array of bytes: 15 | unsigned char* storage; 16 | void inflate(int increase); 17 | public: 18 | void initialize(int size); 19 | void cleanup(); 20 | int add(void* element); 21 | void* fetch(int index); 22 | int count(); 23 | }; 24 | #endif // STASH_H ///:~ 25 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex11/Stack.cpp: -------------------------------------------------------------------------------- 1 | //: C04:Stack.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Linked list with nesting 7 | #include "Stack.h" 8 | using namespace std; 9 | 10 | void 11 | Stack::Link::initialize(void* dat, Link* nxt) { 12 | data = dat; 13 | next = nxt; 14 | } 15 | 16 | void Stack::initialize() { head = 0; } 17 | 18 | void Stack::push(void* dat) { 19 | Link* newLink = new Link; 20 | newLink->initialize(dat, head); 21 | head = newLink; 22 | } 23 | 24 | void* Stack::peek() { 25 | 26 | return head->data; 27 | } 28 | 29 | void* Stack::pop() { 30 | if(head == 0) return 0; 31 | void* result = head->data; 32 | Link* oldHead = head; 33 | head = head->next; 34 | delete oldHead; 35 | return result; 36 | } 37 | 38 | void Stack::cleanup() { 39 | 40 | } ///:~ 41 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex11/Stack.h: -------------------------------------------------------------------------------- 1 | //: C05:Stack2.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Nested structs via linked list 7 | #ifndef STACK2_H 8 | #define STACK2_H 9 | 10 | class Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | void initialize(void* dat, Link* nxt); 15 | }* head; 16 | public: 17 | void initialize(); 18 | void push(void* dat); 19 | void* peek(); 20 | void* pop(); 21 | void cleanup(); 22 | }; 23 | #endif // STACK2_H ///:~ 24 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex11/StackTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Copy the implementation and test files for Stack in Chapter 4 so that you can compile and test Stack2.h in this chapter. 3 | **/ 4 | #include "Stack.h" 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main(int argc, char* argv[]) { 11 | 12 | ifstream in(argv[1]); 13 | 14 | Stack textlines; 15 | textlines.initialize(); 16 | string line; 17 | // Read file and store lines in the Stack: 18 | while(getline(in, line)) 19 | textlines.push(new string(line)); 20 | // Pop the lines from the Stack and print them: 21 | string* s; 22 | while((s = (string*)textlines.pop()) != 0) { 23 | cout << *s << endl; 24 | delete s; 25 | } 26 | textlines.cleanup(); 27 | } ///:~ 28 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex12/Stack.cpp: -------------------------------------------------------------------------------- 1 | //: C04:Stack.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Linked list with nesting 7 | #include "Stack.h" 8 | using namespace std; 9 | 10 | void 11 | Stack::Link::initialize(void* dat, Link* nxt) { 12 | data = dat; 13 | next = nxt; 14 | } 15 | 16 | void Stack::initialize() { head = 0; } 17 | 18 | void Stack::push(void* dat) { 19 | Link* newLink = new Link; 20 | newLink->initialize(dat, head); 21 | head = newLink; 22 | } 23 | 24 | void* Stack::peek() { 25 | 26 | return head->data; 27 | } 28 | 29 | void* Stack::pop() { 30 | if(head == 0) return 0; 31 | void* result = head->data; 32 | Link* oldHead = head; 33 | head = head->next; 34 | delete oldHead; 35 | return result; 36 | } 37 | 38 | void Stack::cleanup() { 39 | 40 | } ///:~ 41 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex12/Stack.h: -------------------------------------------------------------------------------- 1 | //: C05:Stack2.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Nested structs via linked list 7 | #ifndef STACK2_H 8 | #define STACK2_H 9 | 10 | class Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | void initialize(void* dat, Link* nxt); 15 | }* head; 16 | public: 17 | void initialize(); 18 | void push(void* dat); 19 | void* peek(); 20 | void* pop(); 21 | void cleanup(); 22 | }; 23 | #endif // STACK2_H ///:~ 24 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex13/Handle.cpp: -------------------------------------------------------------------------------- 1 | //: C05:Handle.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Handle implementation 7 | #include "Handle.h" 8 | 9 | // Define Handle's implementation: 10 | struct Handle::Cheshire { 11 | int i; 12 | }; 13 | 14 | void Handle::initialize() { 15 | smile = new Cheshire; 16 | smile->i = 1; 17 | } 18 | 19 | void Handle::cleanup() { 20 | delete smile; 21 | } 22 | 23 | int Handle::read() { 24 | return smile->i; 25 | } 26 | 27 | void Handle::change(int x) { 28 | smile->i = x; 29 | } ///:~ 30 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex13/Handle.h: -------------------------------------------------------------------------------- 1 | //: C05:Handle.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Handle classes 7 | #ifndef HANDLE_H 8 | #define HANDLE_H 9 | 10 | class Handle { 11 | struct Cheshire; // Class declaration only 12 | Cheshire* smile; 13 | public: 14 | void initialize(); 15 | void cleanup(); 16 | int read(); 17 | void change(int); 18 | }; 19 | #endif // HANDLE_H ///:~ 20 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex13/UseHandle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Cheshire in Handle.cpp, and verify that your project manager recompiles 3 | and relinks only this file, but doesn’t recompile UseHandle.cpp. 4 | **/ 5 | #include "Handle.h" 6 | 7 | int main() { 8 | Handle u; 9 | u.initialize(); 10 | u.read(); 11 | u.change(1); 12 | u.cleanup(); 13 | } ///:~ 14 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex13/obj/Release/Handle.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/5. Implementation/ex13/obj/Release/Handle.o -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex13/obj/Release/UseHandle.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/5. Implementation/ex13/obj/Release/UseHandle.o -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex14/StackImp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using std::cout; 3 | using std::endl; 4 | 5 | #include "StackImp.h" 6 | 7 | void StackImp::print() { 8 | for (int i = head; i >= 0; i--) 9 | cout << data[i] << ' '; 10 | 11 | cout << endl; 12 | } 13 | 14 | void StackImp::push(int x) { 15 | // Variant with int[] 16 | //if (head >= array_size) { 17 | // cout << "Stack is full!" << endl; 18 | // return; 19 | //} 20 | cout << "Push(" << x << ")" << endl; 21 | data[++head] = x; 22 | } 23 | 24 | int StackImp::peek() { 25 | return (head >= 0) ? data[head] : 0; 26 | } 27 | 28 | int StackImp::pop() { 29 | if (head == -1) 30 | return 0; 31 | 32 | cout << "Pop(" << data[head] << ")" << endl; 33 | int element = data[head]; 34 | data[head--] = 0; 35 | return element; 36 | } 37 | 38 | bool StackImp::empty() { 39 | return (head == -1); 40 | } 41 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex14/StackImp.h: -------------------------------------------------------------------------------- 1 | #ifndef STACKIMP_H_INCLUDED 2 | #define STACKIMP_H_INCLUDED 3 | 4 | #include 5 | using std::vector; 6 | 7 | // const int array_size = 20; // Variant with int[] 8 | 9 | class StackImp { 10 | public: 11 | StackImp() : data{0}, head(-1) {} 12 | void print(); 13 | void push(int ); 14 | int peek(); 15 | int pop(); 16 | bool empty(); 17 | private: 18 | // int data[array_size]; // Variant with int[] 19 | vector data; 20 | int head; 21 | }; 22 | 23 | 24 | #endif // STACKIMP_H_INCLUDED 25 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex14/StackOfInt.cpp: -------------------------------------------------------------------------------- 1 | #include "StackOfInt.h" 2 | 3 | void StackOfInt::print_stack() { 4 | intStack.print(); 5 | } 6 | 7 | void StackOfInt::push_to_stack(int x) { 8 | intStack.push(x); 9 | } 10 | 11 | int StackOfInt::peek_from_stack() { 12 | return intStack.peek(); 13 | } 14 | 15 | int StackOfInt::pop_from_stack() { 16 | return intStack.pop(); 17 | } 18 | 19 | bool StackOfInt::isEmpty() { 20 | return intStack.empty(); 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex14/StackOfInt.h: -------------------------------------------------------------------------------- 1 | #ifndef STACKOFINT_H_INCLUDED 2 | #define STACKOFINT_H_INCLUDED 3 | 4 | #include "StackImp.h" 5 | 6 | class StackOfInt { 7 | public: 8 | StackOfInt() : intStack() {} 9 | void print_stack(); 10 | void push_to_stack(int ); 11 | int peek_from_stack(); 12 | int pop_from_stack(); 13 | bool isEmpty(); 14 | private: 15 | StackImp intStack; 16 | }; 17 | 18 | 19 | #endif // STACKOFINT_H_INCLUDED 20 | -------------------------------------------------------------------------------- /Volume 1/5. Implementation/ex14/ex14.layout: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a simple class called Simple with a constructor that prints 3 | something to tell you that it's been called. 4 | In main( ) make an object of your class. 5 | **/ 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Simple { 11 | public: 12 | Simple() { cout << "Simple() called." << endl; } 13 | }; 14 | 15 | int main() { 16 | 17 | Simple s; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Ex 1: 3 | Write a simple class called Simple with a constructor that prints 4 | something to tell you that it's been called. 5 | In main( ) make an object of your class. 6 | 7 | Ex 2: 8 | Add a destructor to Exercise 1 that prints out a message to tell you that it's been called. 9 | **/ 10 | #include 11 | 12 | using namespace std; 13 | 14 | class Simple { 15 | public: 16 | Simple() { cout << "Simple() called." << endl; } 17 | ~Simple() { cout << "~Simple() called." << endl; } 18 | }; 19 | 20 | int main() { 21 | 22 | Simple s; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex04.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Demonstrate that destructors are still called even when goto is used to jump out of a loop. 3 | **/ 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Simple { 9 | public: 10 | Simple(int n) : x(n) { cout << "Simple(" << x << ") called." << endl; } 11 | ~Simple() { cout << "~Simple(" << x << ") called." << endl; } 12 | private: 13 | int x; 14 | }; 15 | 16 | 17 | int main() { 18 | 19 | for (int i = 0; i < 4; i++) { 20 | Simple s(i); 21 | 22 | if (i == 2) 23 | goto end; 24 | } 25 | 26 | end: 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write two for loops that print out values from zero to 10. 3 | In the first, define the loop counter before the for loop, 4 | and in the second, define the loop counter in the control 5 | expression of the for loop. For the second part of this 6 | exercise, modify the identifier in the second for loop 7 | so that it as the same name as the loop counter for the 8 | first and see what your compiler does. 9 | **/ 10 | #include 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | 16 | int i; 17 | 18 | for (i = 0; i <= 10; i++) 19 | cout << i << " "; 20 | 21 | cout << '\n'; 22 | 23 | for (int i = 0; i <= 10; i++) 24 | cout << i << " "; 25 | 26 | cout << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex06/Handle.cpp: -------------------------------------------------------------------------------- 1 | //: C05:Handle.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Handle implementation 7 | #include "Handle.h" 8 | 9 | // Define Handle's implementation: 10 | struct Handle::Cheshire { 11 | int i; 12 | }; 13 | 14 | Handle::Handle() { 15 | smile = new Cheshire; 16 | smile->i = 0; 17 | } 18 | 19 | Handle::~Handle() { 20 | delete smile; 21 | } 22 | 23 | int Handle::read() { 24 | return smile->i; 25 | } 26 | 27 | void Handle::change(int x) { 28 | smile->i = x; 29 | } ///:~ 30 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex06/Handle.h: -------------------------------------------------------------------------------- 1 | //: C05:Handle.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Handle classes 7 | #ifndef HANDLE_H 8 | #define HANDLE_H 9 | 10 | class Handle { 11 | struct Cheshire; // Class declaration only 12 | Cheshire* smile; 13 | public: 14 | Handle(); 15 | ~Handle(); 16 | int read(); 17 | void change(int); 18 | }; 19 | #endif // HANDLE_H ///:~ 20 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex06/UseHandle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify the Handle.h, Handle.cpp, and UseHandle.cpp files at the end 3 | of Chapter 5 to use constructors and destructors. 4 | **/ 5 | 6 | //: C05:UseHandle.cpp 7 | // From Thinking in C++, 2nd Edition 8 | // Available at http://www.BruceEckel.com 9 | // (c) Bruce Eckel 2000 10 | // Copyright notice in Copyright.txt 11 | //{L} Handle 12 | // Use the Handle class 13 | #include "Handle.h" 14 | 15 | int main() { 16 | Handle u; 17 | //u.initialize(); 18 | u.read(); 19 | u.change(1); 20 | //u.cleanup(); 21 | } ///:~ 22 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex07.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Use aggregate initialization to create an array of double in 3 | which you specify the size of the array but do not provide 4 | enough elements. Print out this array using sizeof to determine 5 | the size of the array. Now create an array of double using 6 | aggregate initialization and automatic counting. Print out the array. 7 | **/ 8 | #include 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | double a[5] = {1.1, 2.2, 3.3}; 15 | int array_size_a = sizeof(a) / sizeof(*a); 16 | 17 | for (int i = 0; i < array_size_a; i++) 18 | cout << a[i] << '\t'; 19 | cout << endl; 20 | 21 | double b[] = {4.4, 5.5, 6.6, 7.7}; 22 | int array_size_b = sizeof(b) / sizeof(*b); 23 | 24 | for (int i = 0; i < array_size_b; i++) 25 | cout << b[i] << '\t'; 26 | cout << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/6. Init and Cleanup/ex10.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class without any constructors, and show that you can 3 | create objects with the default constructor. Now create a 4 | nondefault constructor (one with an argument) for the class, 5 | and try compiling again. Explain what happened. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | class Test { 13 | //!Test(int i) : x(i) {} 14 | //!error: no matching function for call to 'Test::Test()' 15 | private: 16 | int x; 17 | }; 18 | 19 | int main() { 20 | 21 | Test t; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Determine how to generate assembly output with your compiler, 3 | and run experiments to deduce the name-decoration scheme. 4 | **/ 5 | /* 6 | Compile with: g++ -S -masm=intel -g ex03.cpp -o ex03.s 7 | 8 | Name-decoration scheme: 9 | __ZN9SomeClassC2Ev 10 | __ZN9SomeClass8someFuncEi 11 | __ZN9SomeClass8someFuncEc 12 | 13 | */ 14 | 15 | #include 16 | 17 | using namespace std; 18 | 19 | class SomeClass { 20 | public: 21 | SomeClass(); 22 | void someFunc(int x); 23 | void someFunc(char y); 24 | private: 25 | int someData; 26 | }; 27 | 28 | SomeClass::SomeClass() : someData(1) {} 29 | 30 | void SomeClass::someFunc(int x) { cout << x << '\n' << someData << endl; } 31 | 32 | void SomeClass::someFunc(char y) { cout << y << '\n' << someData << endl; } 33 | 34 | int main() { 35 | 36 | SomeClass s; 37 | s.someFunc(1); 38 | s.someFunc('2'); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex05.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a function with two arguments and call it from main( ). 3 | Now make one of the arguments a "placeholder" (no identifier) 4 | and see if your call in main( ) changes. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | void testFunction(int , double ); 12 | 13 | int main() { 14 | 15 | testFunction(1, 2.2); 16 | 17 | return 0; 18 | } 19 | 20 | //void testFunction(int a, double b) { 21 | void testFunction(int a, double ) { 22 | cout << "testFunction called." << endl; 23 | } 24 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex06/Stash3.h: -------------------------------------------------------------------------------- 1 | 2 | #ifndef STASH3_H 3 | #define STASH3_H 4 | 5 | class Stash { 6 | int size; // Size of each space 7 | int quantity; // Number of storage spaces 8 | int next; // Next empty space 9 | // Dynamically allocated array of bytes: 10 | unsigned char* storage; 11 | void inflate(int increase); 12 | public: 13 | Stash(int size, int initQuantity = 100); 14 | ~Stash(); 15 | int add(void* element); 16 | void* fetch(int index); 17 | int count(); 18 | }; 19 | #endif // STASH3_H ///:~ 20 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex06/Stash3Test.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "Stash3.h" 3 | 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | Stash intStash(sizeof(int)); 11 | for(int i = 0; i < 100; i++) 12 | intStash.add(&i); 13 | for(int j = 0; j < intStash.count(); j++) 14 | cout << "intStash.fetch(" << j << ") = " 15 | << *(int*)intStash.fetch(j) 16 | << endl; 17 | const int bufsize = 80; 18 | Stash stringStash(sizeof(char) * bufsize); 19 | ifstream in("Stash3Test.cpp"); 20 | 21 | string line; 22 | while(getline(in, line)) 23 | stringStash.add((char*)line.c_str()); 24 | int k = 0; 25 | char* cp; 26 | while((cp = (char*)stringStash.fetch(k++))!=0) 27 | cout << "stringStash.fetch(" << k << ") = " 28 | << cp << endl; 29 | } ///:~ 30 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex07/Stack3.h: -------------------------------------------------------------------------------- 1 | //: C06:Stack3.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // With constructors/destructors 7 | #ifndef STACK3_H 8 | #define STACK3_H 9 | 10 | class Stack { 11 | struct Link { 12 | void* data; 13 | Link* next; 14 | Link(void* dat, Link* nxt); 15 | ~Link(); 16 | }* head; 17 | public: 18 | Stack(); 19 | Stack(void* element, int sz); 20 | ~Stack(); 21 | void push(void* dat); 22 | void* peek(); 23 | void* pop(); 24 | }; 25 | #endif // STACK3_H ///:~ 26 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex07/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a new version of the Stack class (from Chapter 6) that contains the 3 | default constructor as before, and a second constructor that takes as its 4 | arguments an array of pointers to objects and the size of that array. 5 | This constructor should move through the array and push each pointer onto 6 | the Stack. Test your class with an array of string. 7 | **/ 8 | 9 | 10 | #include "Stack3.h" 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() { 17 | 18 | string lines[] = {"one", "two", "three", "four", "five"}; 19 | 20 | Stack textlines(lines, sizeof(lines) / sizeof(*lines)); 21 | 22 | string* s; 23 | while((s = (string*)textlines.pop()) != 0) { 24 | cout << *s << endl; 25 | delete s; 26 | } 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex09/Mem2.cpp: -------------------------------------------------------------------------------- 1 | //: C07:Mem.cpp {O} 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #include "Mem2.h" 7 | #include 8 | using namespace std; 9 | 10 | Mem::Mem(int sz) { 11 | 12 | mem = 0; size = 0; 13 | 14 | if (sz != 0) 15 | ensureMinSize(sz); 16 | } 17 | 18 | Mem::~Mem() { delete []mem; } 19 | 20 | int Mem::msize() { return size; } 21 | 22 | void Mem::ensureMinSize(int minSize) { 23 | 24 | if(size < minSize) { 25 | byte* newmem = new byte[minSize]; 26 | memset(newmem + size, 0, minSize - size); 27 | memcpy(newmem, mem, size); 28 | delete []mem; 29 | mem = newmem; 30 | size = minSize; 31 | } 32 | } 33 | 34 | byte* Mem::pointer(int minSize) { 35 | if (minSize != 0) 36 | ensureMinSize(minSize); 37 | 38 | return mem; 39 | } 40 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex09/Mem2.h: -------------------------------------------------------------------------------- 1 | //: C07:Mem2.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #ifndef MEM2_H 7 | #define MEM2_H 8 | typedef unsigned char byte; 9 | 10 | class Mem { 11 | byte* mem; 12 | int size; 13 | void ensureMinSize(int minSize); 14 | public: 15 | Mem(int sz = 0); 16 | ~Mem(); 17 | int msize(); 18 | byte* pointer(int minSize = 0); 19 | }; 20 | #endif // MEM2_H ///:~ 21 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex10/Mem2.cpp: -------------------------------------------------------------------------------- 1 | #include "Mem2.h" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | Mem::Mem(int sz) { 7 | 8 | memory = 0; memory_size = 0; 9 | 10 | if (sz != 0) 11 | ensureMinSize(sz); 12 | } 13 | 14 | Mem::~Mem() { delete []memory; } 15 | 16 | int Mem::msize() { return memory_size; } 17 | 18 | 19 | void Mem::ensureMinSize(int minSize) { 20 | 21 | if(memory_size < minSize) { 22 | byte* newmem = new byte[minSize]; 23 | memset(newmem + memory_size, 0, minSize - memory_size); 24 | memcpy(newmem, memory, memory_size); 25 | delete []memory; 26 | memory = newmem; 27 | memory_size = minSize; 28 | } 29 | } 30 | 31 | 32 | byte* Mem::pointer(int minSize) { 33 | 34 | if (minSize != 0) 35 | ensureMinSize(minSize); 36 | 37 | return memory; 38 | } 39 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex10/Mem2.h: -------------------------------------------------------------------------------- 1 | #ifndef MEM2_H 2 | #define MEM2_H 3 | typedef unsigned char byte; 4 | 5 | class Mem { 6 | byte* memory; // Storage 7 | int memory_size; // Storage size 8 | public: 9 | Mem(int sz = 0); 10 | ~Mem(); 11 | int msize(); 12 | void ensureMinSize(int minSize); // Checking size 13 | byte* pointer(int minSize = 0); // Returns ptr to storage 14 | }; 15 | #endif 16 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex10/Stash3.h: -------------------------------------------------------------------------------- 1 | //: C07:Stash3.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | // Function overloading 7 | #ifndef STASH3_H 8 | #define STASH3_H 9 | 10 | #include "Mem2.h" 11 | 12 | class Stash { 13 | int block_size; // Block size 14 | int blocks_quantity; // Blocks quantity 15 | int next; // Next empty space 16 | 17 | Mem* storage; // Storage, instead of unsigned char* 18 | 19 | void inflate(int increase); 20 | public: 21 | Stash(int block_size); // Zero quantity 22 | Stash(int block_size, int initQuantity); 23 | ~Stash(); 24 | int add(void* element); // Adds element 25 | void* fetch(int index); // Returns element[index] 26 | int count(); // Returns size 27 | }; 28 | #endif 29 | -------------------------------------------------------------------------------- /Volume 1/7. Overloading/ex11/Mem2.h: -------------------------------------------------------------------------------- 1 | //: C07:Mem2.h 2 | // From Thinking in C++, 2nd Edition 3 | // Available at http://www.BruceEckel.com 4 | // (c) Bruce Eckel 2000 5 | // Copyright notice in Copyright.txt 6 | #ifndef MEM2_H 7 | #define MEM2_H 8 | typedef unsigned char byte; 9 | 10 | class Mem { 11 | byte* mem; 12 | byte* initial_ptr; 13 | bool not_initialized; 14 | int size; 15 | 16 | public: 17 | Mem(int sz = 0); 18 | ~Mem(); 19 | 20 | int msize(); 21 | void ensureMinSize(int minSize); 22 | byte* pointer(int minSize = 0); 23 | byte* init_ptr() {return initial_ptr;} 24 | bool moved(byte* ptr); 25 | }; 26 | #endif // MEM2_H ///:~ 27 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex01.c: -------------------------------------------------------------------------------- 1 | /** 2 | Create three const int values, then add them together to produce 3 | a value that determines the size of an array in an array definition. 4 | Try to compile the same code in C and see what happens 5 | (you can generally force your C++ compiler to run as a C compiler 6 | by using a command-line flag). 7 | **/ 8 | 9 | //!In C-mode we've got an error: 10 | //!error: variable-sized object may not be initialized 11 | 12 | int main() { 13 | 14 | const int a = 1; 15 | const int b = 2; 16 | const int c = 3; 17 | 18 | int array[a+b+c] = {0}; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create three const int values, then add them together to produce 3 | a value that determines the size of an array in an array definition. 4 | Try to compile the same code in C and see what happens 5 | (you can generally force your C++ compiler to run as a C compiler 6 | by using a command-line flag). 7 | **/ 8 | 9 | //!In C-mode we've got an error: 10 | //!error: variable-sized object may not be initialized 11 | 12 | int main() { 13 | 14 | const int a = 1; 15 | const int b = 2; 16 | const int c = 3; 17 | 18 | int array[a+b+c] = {0}; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex02.c: -------------------------------------------------------------------------------- 1 | /** 2 | Prove to yourself that the C and C++ compilers really do treat constants differently. 3 | Create a global const and use it in a global constant expression; 4 | then compile it under both C and C++. 5 | **/ 6 | 7 | //!In C-mode we've got an error: 8 | //!error: initializer element is not constant 9 | 10 | const int a = 10; 11 | const int b = a + a; 12 | 13 | int main() { 14 | 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Prove to yourself that the C and C++ compilers really do treat constants differently. 3 | Create a global const and use it in a global constant expression; 4 | then compile it under both C and C++. 5 | **/ 6 | 7 | //!C-mode thwors an error: 8 | //!error: initializer element is not constant 9 | 10 | const int a = 10; 11 | const int b = a + a; 12 | 13 | int main() { 14 | 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create example const definitions for all the built-in types and their variants. 3 | Use these in expressions with other consts to make new const definitions. 4 | Make sure they compile successfully. 5 | **/ 6 | 7 | #include 8 | 9 | int main() { 10 | 11 | const int a = 1; 12 | const short b = 2; 13 | const long c = 3; 14 | const long long d = 4; 15 | const double e = 4.4; 16 | const float f = 5.5; 17 | const long double g = 6.6; 18 | const bool h = true; 19 | const char i = '@'; 20 | const unsigned char j = 0; 21 | const int k[5] = {0}; 22 | const double l[2] = {0}; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex04 with C/ex04.c: -------------------------------------------------------------------------------- 1 | #include "ex04.h" 2 | 3 | int c = a; 4 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex04 with C/ex04.h: -------------------------------------------------------------------------------- 1 | /** 2 | Create a const definition in a header file, include that header file in two .cpp files, 3 | then compile those files and link them. You should not get any errors. 4 | Now try the same experiment with C. 5 | **/ 6 | 7 | #ifndef EX04_H_INCLUDED 8 | #define EX04_H_INCLUDED 9 | 10 | const int a = 10; 11 | 12 | 13 | #endif // EX04_H_INCLUDED 14 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex04 with C/main.c: -------------------------------------------------------------------------------- 1 | #include "ex04.h" 2 | 3 | int main() { 4 | 5 | int b = a; 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex04/ex04.cpp: -------------------------------------------------------------------------------- 1 | #include "ex04.h" 2 | 3 | int c = a; 4 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex04/ex04.h: -------------------------------------------------------------------------------- 1 | /** 2 | Create a const definition in a header file, include that header file in two .cpp files, 3 | then compile those files and link them. You should not get any errors. 4 | Now try the same experiment with C. 5 | **/ 6 | 7 | #ifndef EX04_H_INCLUDED 8 | #define EX04_H_INCLUDED 9 | 10 | const int a = 10; 11 | 12 | 13 | #endif // EX04_H_INCLUDED 14 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex04/main.cpp: -------------------------------------------------------------------------------- 1 | #include "ex04.h" 2 | 3 | int main() { 4 | 5 | int b = a; 6 | 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex05.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/8. Constants/ex05.cpp -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex06.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a const array of char, then try to change one of the chars. 3 | **/ 4 | 5 | #include 6 | 7 | int main() { 8 | 9 | const char array[] = "Test"; 10 | array[0] = 't'; 11 | //!error: assignment of read-only location 'array[0]' 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex07/ex07.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create an extern const declaration in one file, 3 | and put a main( ) in that file that prints the 4 | value of the extern const. Provide an extern const 5 | definition in a second file, then compile and link 6 | the two files together. 7 | **/ 8 | 9 | #include 10 | #include "ex07.h" 11 | 12 | using namespace std; 13 | 14 | extern const int a; 15 | 16 | int main() { 17 | 18 | cout << "a = " << a << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex07/ex07.h: -------------------------------------------------------------------------------- 1 | #ifndef EX07_H_INCLUDED 2 | #define EX07_H_INCLUDED 3 | 4 | const int a = 123; 5 | 6 | #endif // EX07_H_INCLUDED 7 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex08.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/8. Constants/ex08.cpp -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex09.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/8. Constants/ex09.cpp -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex10.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/8. Constants/ex10.cpp -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex11.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Remove the comment on the error-generating line of code in 3 | PointerAssignment.cpp to see the error that your compiler generates. 4 | **/ 5 | 6 | int d = 1; 7 | const int e = 2; 8 | int* u = &d; // OK -- d not const 9 | 10 | //int* v = &e; // Illegal -- e const 11 | //!error: invalid conversion from 'const int*' to 'int*' 12 | 13 | int* w = (int*)&e; // Legal but bad practice 14 | 15 | int main() {} 16 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex12.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/8. Constants/ex12.cpp -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex13.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a function that takes an argument by value as a const; 3 | then try to change that argument in the function body. 4 | **/ 5 | 6 | #include 7 | 8 | class SomeClass { 9 | public: 10 | SomeClass(int i) : x(i) {} 11 | void increment() {x++;} 12 | private: 13 | int x; 14 | }; 15 | 16 | void someFunc(const SomeClass ); 17 | 18 | int main() { 19 | 20 | SomeClass a(2); 21 | someFunc(a); 22 | 23 | return 0; 24 | } 25 | 26 | //!error: passing 'const SomeClass' as 'this' argument of 'void SomeClass::increment()' discards qualifiers 27 | void someFunc(const SomeClass s) { 28 | s.increment(); 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex14.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a function that takes a float by value. Inside the function, 3 | bind a const float& to the argument, and only use the reference from 4 | then on to ensure that the argument is not changed. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | void foo(float ); 12 | 13 | int main() { 14 | 15 | float f = 2.2; 16 | 17 | cout << "f: " << f << endl; 18 | foo(f); 19 | cout << "f after foo(): " << f << endl; 20 | 21 | return 0; 22 | } 23 | 24 | void foo(float f) { 25 | 26 | //const 27 | //!error: increment of read-only reference 'flink' 28 | float& flink = f; 29 | flink++; 30 | } 31 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex17 - ConstReference.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Make a new version of ConstPointer.cpp called ConstReference.cpp which demonstrates references instead of pointers 3 | **/ 4 | void t(int&) {} 5 | 6 | void u(const int& cip) { 7 | int i = cip; // OK -- copies value 8 | } 9 | 10 | const int& w() { 11 | static int i; 12 | const int& ir = i; 13 | return ir; 14 | } 15 | 16 | int main() { 17 | int x = 0; 18 | int& ip = x; 19 | const int& cip = x; 20 | t(ip); 21 | u(ip); 22 | u(cip); 23 | const int& cip2 = w(); 24 | } 25 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex18.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify ConstTemporary.cpp removing the comment on the error-causing 3 | line to see what error messages your compiler generates. 4 | **/ 5 | 6 | class X {}; 7 | 8 | X f() { return X(); } 9 | 10 | void g1(X&) {} 11 | void g2(const X&) {} 12 | 13 | int main() { 14 | //! error: invalid initialization of non-const reference of type 'X&' from an rvalue of type 'X' 15 | g1(f()); 16 | 17 | // OK: g2 takes a const reference: 18 | g2(f()); 19 | } 20 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex19.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class containing both a const and a non-const float. 3 | Initialize these using the constructor initializer list. 4 | **/ 5 | 6 | #include 7 | 8 | class S { 9 | public: 10 | S() : a(0), b(0) {} 11 | S(float aa, float bb) : a(aa), b(bb) {} 12 | private: 13 | float a; 14 | const float b; 15 | }; 16 | 17 | int main() { 18 | 19 | S(1.1, 2.2); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex21.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class containing a const member that you initialize in the 3 | constructor initializer list and an untagged enumeration that you 4 | use to determine an array size. 5 | **/ 6 | 7 | #include 8 | 9 | class SomeClass { 10 | public: 11 | SomeClass() : x(0), i({0}) {} 12 | SomeClass(int y) : x(y), i({0}) {} 13 | private: 14 | enum { size = 10 }; 15 | int i[size]; 16 | const int x; 17 | }; 18 | 19 | int main() { 20 | 21 | SomeClass(123); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex22.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | In ConstMember.cpp, remove the const specifier on the member function 3 | definition, but leave it on the declaration, to see what kind of 4 | compiler error message you get. 5 | **/ 6 | class X { 7 | int i; 8 | public: 9 | X(int ii); 10 | int f(); 11 | }; 12 | 13 | X::X(int ii) : i(ii) {} 14 | 15 | int X::f() const { 16 | return i; 17 | } 18 | 19 | int main() { 20 | X x1(10); 21 | const X x2(20); 22 | x1.f(); 23 | x2.f(); 24 | //! error: prototype for 'int X::f() const' does not match any in class 'X'| 25 | //! error: no matching function for call to 'X::f() const'| 26 | } 27 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex24.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class with both const and non-const member functions. 3 | Try to call a non-const member function from a const member 4 | function to see what kind of compiler error message you get. 5 | **/ 6 | 7 | #include 8 | 9 | class M { 10 | public: 11 | void nonConstFunc() {} 12 | void constFunc() const { nonConstFunc(); } 13 | //! error: no matching function for call to 'M::nonConstFunc() const' 14 | //! note: no known conversion for implicit 'this' parameter from 'const M* const' to 'M*' 15 | 16 | }; 17 | 18 | int main() { 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex25.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | In Mutable.cpp, remove the comment on the error-causing line to see 3 | what sort of error message your compiler produces. 4 | **/ 5 | 6 | class Z { 7 | int i; 8 | mutable int j; 9 | public: 10 | Z(); 11 | void f() const; 12 | }; 13 | 14 | Z::Z() : i(0), j(0) {} 15 | 16 | void Z::f() const { 17 | i++; //! error: increment of member 'Z::i' in read-only object| 18 | j++; // OK: mutable 19 | } 20 | 21 | int main() { 22 | const Z zz; 23 | zz.f(); // Actually changes it! 24 | } ///:~ 25 | -------------------------------------------------------------------------------- /Volume 1/8. Constants/ex28.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/8. Constants/ex28.cpp -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex01.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that uses the F( ) macro shown at the beginning of 3 | the chapter and demonstrates that it does not expand properly, as 4 | described in the text. Repair the macro and show that it works correctly. 5 | **/ 6 | 7 | #include 8 | 9 | #define F (x) (x + 1) 10 | //#define F(x) (x + 1) 11 | 12 | using namespace std; 13 | 14 | int main() { 15 | 16 | cout << F(1) << endl; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex02.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that uses the FLOOR( ) macro shown at 3 | the beginning of the chapter. Show the conditions under 4 | which it does not work properly. 5 | **/ 6 | 7 | #include 8 | 9 | #define FLOOR(x,b) x>=b?0:1 10 | 11 | using namespace std; 12 | 13 | int main() { 14 | 15 | int a = 1; 16 | 17 | if (FLOOR(1,2)) 18 | cout << "True" << endl; 19 | else 20 | cout << "False" << endl; 21 | 22 | 23 | if (FLOOR(a&0x0f,0x07)) 24 | cout << "True" << endl; 25 | else 26 | cout << "False" << endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex03.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify MacroSideEffects.cpp so that BAND( ) works properly. 3 | **/ 4 | #include 5 | using namespace std; 6 | 7 | //#define BAND(x) (((x)>5 && (x)<10) ? (x) : 0) 8 | int y; 9 | #define BAND(x) (((y)=x, (y)>5 && (y)<10) ? (y) : 0) 10 | 11 | int main() { 12 | ofstream out("macro.out"); 13 | for(int i = 4; i < 11; i++) { 14 | int a = i; 15 | out << "a = " << a << endl << '\t'; 16 | out << "BAND(++a)=" << BAND(++a) << endl; 17 | out << "\t a = " << a << endl; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex06/ex06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | inline void f() { 6 | cout << "f()"; 7 | } 8 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex06/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Prove that inline functions default to internal linkage. 3 | **/ 4 | 5 | //!Standart was changed, nowadays they have external linkage. 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | int main() { 12 | 13 | extern void f(); 14 | f(); 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex07.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/9. Inline/ex07.cpp -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex12.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Modify Cpptime.cpp so that it measures the time from the time that the 3 | program begins running to the time when the user presses the "Enter" or "Return" key. 4 | **/ 5 | #include "Cpptime.h" 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | Time start; 11 | 12 | cout << "Press enter after few seconds..." << endl; 13 | cin.get(); 14 | 15 | Time end; 16 | cout << endl; 17 | cout << "start = " << start.ascii(); 18 | cout << "end = " << end.ascii(); 19 | cout << "delta = " << end.delta(&start); 20 | } ///:~ 21 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex13.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 1/9. Inline/ex13.cpp -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex14.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Create a class A with an inline default constructor that announces itself. 3 | Now make a new class B and put an object of A as a member of B, and give B 4 | an inline constructor. Create an array of B objects and see what happens. 5 | **/ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class A { 12 | public: 13 | A() { cout << "A()" << endl; } 14 | }; 15 | 16 | class B { 17 | public: 18 | B() : a() { cout << "B()" << endl; } 19 | private: 20 | A a; 21 | }; 22 | 23 | int main() { 24 | 25 | B b_array[3]; 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex16.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that takes a string as the command-line argument. 3 | Write a for loop that removes one character from the string with 4 | each pass, and use the DEBUG( ) macro from this chapter to print 5 | the string each time. 6 | **/ 7 | 8 | #include 9 | 10 | using namespace std; 11 | 12 | #define DEBUG(x) cout << #x " = " << x << endl 13 | 14 | int main(int argc, char* argv[]) { 15 | 16 | if (argc > 1) { 17 | 18 | string s(argv[1]); 19 | size_t sz = s.size(); 20 | 21 | for (size_t n = 0; n < sz; ++n) { 22 | s = s.erase(sz - n - 1, 1); 23 | DEBUG(s); 24 | } 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex17.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Correct the TRACE( ) macro as specified in this chapter, and prove that it works correctly. 3 | **/ 4 | 5 | #include 6 | 7 | // #define TRACE(s) cerr << #s << endl; s 8 | #define TRACE(s) cerr << #s << endl, s 9 | 10 | using namespace std; 11 | 12 | int main() { 13 | 14 | int x; 15 | TRACE(x = 2); 16 | cout << x << endl; 17 | TRACE(++x); 18 | cout << x << endl; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex20.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that takes two command-line arguments: the first is an int 3 | and the second is a file name. Use require.h to ensure that you have the 4 | right number of arguments, that the int is between 5 and 10, and that the 5 | file can successfully be opened. 6 | **/ 7 | 8 | #include 9 | #include "require.h" 10 | 11 | using namespace std; 12 | 13 | int main(int argc, char* argv[]) { 14 | 15 | requireArgs(argc, 2); 16 | require(atoi(argv[1]) >=5 && atoi(argv[1]) <= 10, "int value must be between 5 and 10"); 17 | 18 | ifstream in(argv[2]); 19 | assure(in, argv[2]); 20 | 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex21.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Write a program that uses the IFOPEN( ) macro to open a file as an input stream. Note the creation of the ifstream object and its scope. 3 | **/ 4 | 5 | #include 6 | #include "require.h" 7 | 8 | #define IFOPEN(VAR, NAME) \ 9 | ifstream VAR(NAME); \ 10 | assure(VAR, NAME); 11 | 12 | using namespace std; 13 | 14 | int main(int argc, char* argv[]) { 15 | 16 | IFOPEN(in, argv[1]); 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Volume 1/9. Inline/ex22.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | Determine how to get your compiler to generate assembly code. 3 | Create a file containing a very small function and a main( ) 4 | that calls the function. Generate assembly code when the function 5 | is inlined and not inlined, and demonstrate that the inlined 6 | version does not have the function call overhead. 7 | **/ 8 | /* 9 | Compile with: g++ -S -masm=intel -g ex22.cpp -o ex22.s 10 | */ 11 | 12 | #include 13 | 14 | using namespace std; 15 | 16 | //inline void MySmallFunction() { 17 | // cout << "Function call..." << endl; 18 | //} 19 | 20 | void MySmallFunction(); 21 | 22 | int main() { 23 | 24 | MySmallFunction(); 25 | 26 | return 0; 27 | } 28 | 29 | void MySmallFunction() { 30 | cout << "Function call..." << endl; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 2/1. Exceptions/ex04.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a generic main( ) that takes all exceptions and 3 | reports them as errors. 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | int main() { 10 | 11 | try { 12 | std::cout << "Throwing some exception...\n"; 13 | throw std::runtime_error("Dumb exception"); 14 | } 15 | catch(...) { 16 | std::cout << "Some exception detected!\n"; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Volume 2/1. Exceptions/ex06.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Create a destructor that throws an exception, and write 3 | code to prove to yourself that this is a bad idea by showing 4 | that if a new exception is thrown before the handler for the 5 | existing one is reached, terminate( ) is called. 6 | */ 7 | 8 | #include 9 | 10 | class Exception1 {}; 11 | class Exception2 {}; 12 | 13 | class MyClass{ 14 | public: 15 | MyClass() { std::cout << "MyClass()" << '\n'; } 16 | 17 | ~MyClass() { 18 | std::cout << "~MyClass()" << '\n'; 19 | throw Exception2(); 20 | } 21 | }; 22 | 23 | int main() { 24 | 25 | try { 26 | throw Exception1(); 27 | } 28 | 29 | catch (Exception1&) { 30 | MyClass m; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Volume 2/1. Exceptions/ex07.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Prove to yourself that all exception objects (the ones 3 | that are thrown) are properly destroyed. 4 | */ 5 | 6 | #include 7 | 8 | class Exception { 9 | public: 10 | Exception() { std::cout << "Exception()\n"; } 11 | ~Exception() { std::cout << "~Exception()\n"; } 12 | }; 13 | 14 | int main() { 15 | 16 | try { 17 | throw Exception(); 18 | } 19 | catch (Exception& e){ 20 | std::cout << "Exception detected!\n"; 21 | } 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Volume 2/1. Exceptions/ex08.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Prove to yourself that if you create an exception object 3 | on the heap and throw the pointer to that object, it will 4 | not be cleaned up. 5 | */ 6 | 7 | #include 8 | 9 | class Exception { 10 | public: 11 | Exception() { std::cout << "Exception()\n"; } 12 | ~Exception() { std::cout << "~Exception()\n"; } 13 | }; 14 | 15 | int main() { 16 | 17 | try { 18 | Exception* exc = new Exception(); 19 | throw exc; 20 | } 21 | catch (Exception* e){ 22 | std::cout << "Exception detected!\n"; 23 | } 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Create a variation of SingletonPattern.cpp where all 3 | functions are static. Is the instance( ) function still 4 | necessary in this case? 5 | */ 6 | 7 | #include 8 | 9 | using namespace std; 10 | 11 | class Singleton { 12 | static Singleton s; 13 | int i; 14 | Singleton(int x) : i(x) { } 15 | Singleton& operator=(Singleton&); // Disallowed 16 | Singleton(const Singleton&); // Disallowed 17 | public: 18 | //static Singleton& instance() { return s; } 19 | static int getValue() { return s.i; } 20 | static void setValue(int x) { s.i = x; } 21 | }; 22 | 23 | Singleton Singleton::s(47); 24 | 25 | int main() { 26 | //Singleton& s = Singleton::instance(); 27 | cout << Singleton::getValue() << endl; 28 | //Singleton& s2 = Singleton::instance(); 29 | Singleton::setValue(9); 30 | cout << Singleton::getValue() << endl; 31 | } ///:~ 32 | -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex02.cfg: -------------------------------------------------------------------------------- 1 | Option1=1 2 | Option2="SOMETHING HERE" 3 | Option3=2.3 4 | -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex06/ex06_1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ex06.h" 3 | 4 | class MyApp1 : public AppFramework { 5 | protected: 6 | void file_handler(std::string filename, std::ofstream& result) { 7 | 8 | std::ifstream file(filename, std::ios::in); 9 | 10 | std::string buf; 11 | 12 | while (getline(file, buf)) { 13 | std::transform(buf.begin(), buf.end(), buf.begin(), ::tolower); 14 | result << buf << '\n'; 15 | } 16 | 17 | file.close(); 18 | } 19 | }; 20 | 21 | int main(int argc, char** argv) { 22 | 23 | MyApp1 app1; 24 | app1.templateMethod(argc, argv); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex06/ex06_1.txt: -------------------------------------------------------------------------------- 1 | opens 2 | file 3 | pattern -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex06/ex06_2.txt: -------------------------------------------------------------------------------- 1 | Create a framework (that is, use the Template Method pattern) 2 | that takes a list of file names on the command line. It opens 3 | each file except the last for reading, and the last file it opens 4 | for writing. The framework will process each input file using 5 | an undetermined policy and write the output to the last file. 6 | Inherit to customize this framework to create two separate -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex06/ex06_3.txt: -------------------------------------------------------------------------------- 1 | Converts all the letters in each file to uppercase. 2 | Searches the files for words given in the first file. -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex07/ex07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ex07.h" 3 | 4 | 5 | int main(int argc, char** argv) { 6 | 7 | ToLower tl; 8 | Searcher sr; 9 | 10 | Context c1(tl), c2(sr); 11 | 12 | std::cout << "Choose default behaviour: \n" 13 | << "1) Convert all the letters in each file to uppercase.\n" 14 | << "2) Search the files for words given in the first file.\n"; 15 | 16 | int x = 0; 17 | std::cin >> x; 18 | 19 | if (x == 1) 20 | c1.handler(argc, argv); 21 | else 22 | c2.handler(argc, argv); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex07/ex07_1.txt: -------------------------------------------------------------------------------- 1 | opens 2 | file 3 | pattern -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex07/ex07_2.txt: -------------------------------------------------------------------------------- 1 | Create a framework (that is, use the Template Method pattern) 2 | that takes a list of file names on the command line. It opens 3 | each file except the last for reading, and the last file it opens 4 | for writing. The framework will process each input file using 5 | an undetermined policy and write the output to the last file. 6 | Inherit to customize this framework to create two separate -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex07/ex07_3.txt: -------------------------------------------------------------------------------- 1 | Converts all the letters in each file to uppercase. 2 | Searches the files for words given in the first file. -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex16/Observable.h: -------------------------------------------------------------------------------- 1 | #ifndef OBSERVABLE_H_ 2 | #define OBSERVABLE_H_ 3 | 4 | #include 5 | #include "Observer.h" 6 | 7 | class Observable { 8 | public: 9 | virtual void add_observer(Observer& o) { observers.insert(&o); } 10 | virtual void delete_observer(Observer& o) { observers.erase(&o); } 11 | 12 | virtual void notify_observers() { 13 | for(auto& o : observers) 14 | o->update(this); 15 | } 16 | 17 | virtual ~Observable() {} 18 | private: 19 | std::set observers; 20 | }; 21 | 22 | 23 | 24 | #endif /* OBSERVABLE_H_ */ 25 | -------------------------------------------------------------------------------- /Volume 2/10. Patterns/ex16/Observer.h: -------------------------------------------------------------------------------- 1 | /* 2 | Create a minimal Observer-Observable design in two classes, 3 | without base classes and without the extra arguments in Observer.h 4 | and the member functions in Observable.h. Just create the bare minimum 5 | in the two classes, and then demonstrate your design by creating one 6 | Observable and many Observers and cause the Observable to update the Observers. 7 | */ 8 | 9 | #ifndef OBSERVER_H_ 10 | #define OBSERVER_H_ 11 | 12 | class Observable; 13 | 14 | class Observer { 15 | public: 16 | virtual void update(Observable* o) = 0; 17 | virtual ~Observer() {} 18 | }; 19 | 20 | #endif /* OBSERVER_H_ */ 21 | -------------------------------------------------------------------------------- /Volume 2/10. Patterns/purge.h: -------------------------------------------------------------------------------- 1 | //: :purge.h 2 | // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. 3 | // (c) 1995-2004 MindView, Inc. All Rights Reserved. 4 | // See source code use permissions stated in the file 'License.txt', 5 | // distributed with the code package available at www.MindView.net. 6 | // Delete pointers in an STL sequence container. 7 | #ifndef PURGE_H 8 | #define PURGE_H 9 | #include 10 | 11 | template void purge(Seq& c) { 12 | typename Seq::iterator i; 13 | for(i = c.begin(); i != c.end(); ++i) { 14 | delete *i; 15 | *i = 0; 16 | } 17 | } 18 | 19 | // Iterator version: 20 | template void purge(InpIt begin, InpIt end) { 21 | while(begin != end) { 22 | delete *begin; 23 | *begin = 0; 24 | ++begin; 25 | } 26 | } 27 | #endif // PURGE_H ///:~ 28 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write and test a function that reverses the order of 3 | the characters in a string. 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | std::string reverse(const std::string& str) { 10 | return std::string(str.rbegin(), str.rend()); 11 | } 12 | 13 | int main() { 14 | std::string s = "Abc de f."; 15 | std::cout << "Initial string: " << s 16 | << "\nReversed string: " << reverse(s) << '\n'; 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | A palindrome is a word or group of words that read the 3 | same forward and backward. For example "madam" or "wow." 4 | Write a program that takes a string argument from the 5 | command line and, using the function from the previous 6 | exercise, prints whether the string was a palindrome or not. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | std::string reverse(const std::string& str) { 13 | return std::string(str.rbegin(), str.rend()); 14 | } 15 | 16 | bool is_palindrome(const std::string& s) { 17 | return reverse(s) == s; 18 | } 19 | 20 | int main() { 21 | std::string str; 22 | std::cout << "Type a string: "; 23 | std::getline(std::cin, str); 24 | 25 | if ( is_palindrome(str) ) 26 | std::cout << "Palindrome!\n"; 27 | else 28 | std::cout << "Not a palindrome!\n"; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex08.txt: -------------------------------------------------------------------------------- 1 | aibohphobia symphony example this LEVEL 2 | love Rotator 3 | deleveled Madam sports 4 | level smile rotator 5 | madam -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex09.html: -------------------------------------------------------------------------------- 1 |

Some header here

2 |

Abstract text.

3 | Bold text begins here... 4 | ...and ends here. -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex09_ReplaceAll.cpp: -------------------------------------------------------------------------------- 1 | //: C03:ReplaceAll.cpp {O} 2 | // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. 3 | // (c) 1995-2004 MindView, Inc. All Rights Reserved. 4 | // See source code use permissions stated in the file 'License.txt', 5 | // distributed with the code package available at www.MindView.net. 6 | #include "ex09_ReplaceAll.h" 7 | 8 | #include 9 | using namespace std; 10 | 11 | string& replaceAll(string& context, const string& from, 12 | const string& to) { 13 | size_t lookHere = 0; 14 | size_t foundHere; 15 | while((foundHere = context.find(from, lookHere)) 16 | != string::npos) { 17 | context.replace(foundHere, from.size(), to); 18 | lookHere = foundHere + to.size(); 19 | } 20 | return context; 21 | } ///:~ 22 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex09_ReplaceAll.h: -------------------------------------------------------------------------------- 1 | //: C03:ReplaceAll.h 2 | // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. 3 | // (c) 1995-2004 MindView, Inc. All Rights Reserved. 4 | // See source code use permissions stated in the file 'License.txt', 5 | // distributed with the code package available at www.MindView.net. 6 | #ifndef REPLACEALL_H 7 | #define REPLACEALL_H 8 | #include 9 | 10 | std::string& replaceAll(std::string& context, 11 | const std::string& from, const std::string& to); 12 | #endif // REPLACEALL_H ///:~ 13 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex10.txt: -------------------------------------------------------------------------------- 1 | Write a program that takes three command-line arguments 2 | (a filename and two strings) and displays to the console 3 | all lines in the file that have both strings in the line, 4 | either string, only one string, or neither string, based 5 | on user input at the beginning of the program (the user 6 | will choose which matching mode to use). For all but 7 | the "neither string" option, highlight the input string(s) 8 | by placing an asterisk (*) at the beginning and end of 9 | each string’s occurrence when it is displayed. -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex11.txt: -------------------------------------------------------------------------------- 1 | Write a program that takes two command-line arguments 2 | (a filename and a string) and counts the number of times 3 | the string occurs in the file, even as a substring 4 | (but ignoring overlaps). For example, an input string 5 | of “ba” would match twice in the word “basketball,” but 6 | an input string of “ana” would match only once in the 7 | word “banana.” Display to the console the number of 8 | times the string is matched in the file, as well as 9 | the average length of the words where the string occurred. 10 | (If the string occurs more than once in a word, only count 11 | the word once in figuring the average.) -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex12.txt: -------------------------------------------------------------------------------- 1 | Write a program that takes a filename from the command line. -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex14.txt: -------------------------------------------------------------------------------- 1 | C++ strings dynamically and transparently grow their internal data storage 2 | space to accommodate increases in the size of the string data. when the data 3 | in a string grows beyond the limits of the memory initially allocated to it, 4 | the string object will make the memory management calls that take space from 5 | and return space (to the heap). consistent allocation schemes prevent memory 6 | leaks! and have the potential to be much more efficient than "roll your own" 7 | memory management. -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex16.txt: -------------------------------------------------------------------------------- 1 | Write a program that accepts two command-line arguments 2 | (a filename and a number) and scrambles each word in the 3 | file by randomly switching two of its letters the number 4 | of times specified in the second argument. (That is, if 0 5 | is passed into your program from the command-line, the 6 | words should not be scrambled; if 1 is passed in, one 7 | pair of randomly-chosen letters should be swapped, for an 8 | input of 2, two random pairs should be swapped, etc.). -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex16_result.txt: -------------------------------------------------------------------------------- 1 | Weitr a pragrom that actepcs two conmamd-einl arguments 2 | (a fileeamn and a nembur) and scrambles hace dorw in the 3 | eilf by randymlo switching two of its lertets the nembur 4 | of tsmei specified in the sncoed argutenm. (thaT is, if 0 5 | is pessad onti rouy pragrom mrof the conmamd-einl, the 6 | wsrdo slouhd not be scrambled; if 1 is pessad in, one 7 | raip of randymlo-ceoshn lertets slouhd be sweppad, for an 8 | itpun of 2, two rondam psira slouhd be sweppad, etc.). 9 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex17.txt: -------------------------------------------------------------------------------- 1 | First sentence. C++ string objects provide developers with a number 2 | of great advantages over their C counterparts. Last sentence. -------------------------------------------------------------------------------- /Volume 2/3. Strings/ex18.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Prove to yourself that the at( ) member function really 3 | will throw an exception if an attempt is made to go out 4 | of bounds, and that the indexing operator ([ ]) won’t. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main() { 12 | 13 | try { 14 | std::string str = "Test"; 15 | //str[10] = 'Z'; 16 | str.at(10) = 'Z'; 17 | } 18 | catch (const std::out_of_range& e) { 19 | std::cout << "Exception detected: " << e.what() << '\n'; 20 | } 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/result_06.txt: -------------------------------------------------------------------------------- 1 | I've got a message from John. 2 | John said that he called his boat "John's Warship". 3 | It's strange but I don't think that %tag% is crazy. 4 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/result_07.txt: -------------------------------------------------------------------------------- 1 | I've got a message from John. 2 | John said that he called his boat "John's Warship". 3 | It's strange but I don't think that John is crazy. 4 | -------------------------------------------------------------------------------- /Volume 2/3. Strings/source.txt: -------------------------------------------------------------------------------- 1 | I've got a message from %Tag%. 2 | %Tag% said that he called his boat "%Tag%'s Warship". 3 | It's strange but I don't think that %tag% is crazy. -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Open a file by creating an ifstream object. Make an 3 | ostringstream object and read the entire contents into 4 | the ostringstream using the rdbuf( ) member function. 5 | Extract a string copy of the underlying buffer and 6 | capitalize every character in the file using the 7 | Standard C toupper( ) macro defined in . 8 | Write the result out to a new file. 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | int main() { 17 | /*Some command line arguments parsing here*/ 18 | std::ifstream source("ex01.cpp", std::ios::in); 19 | std::ofstream result("ex01.txt", std::ios::out); 20 | std::ostringstream ss; 21 | 22 | ss << source.rdbuf(); 23 | std::string content = ss.str(); 24 | 25 | for (auto& s : content) 26 | s = toupper(s); 27 | 28 | result << content; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex01.txt: -------------------------------------------------------------------------------- 1 | /* 2 | OPEN A FILE BY CREATING AN IFSTREAM OBJECT. MAKE AN 3 | OSTRINGSTREAM OBJECT AND READ THE ENTIRE CONTENTS INTO 4 | THE OSTRINGSTREAM USING THE RDBUF( ) MEMBER FUNCTION. 5 | EXTRACT A STRING COPY OF THE UNDERLYING BUFFER AND 6 | CAPITALIZE EVERY CHARACTER IN THE FILE USING THE 7 | STANDARD C TOUPPER( ) MACRO DEFINED IN . 8 | WRITE THE RESULT OUT TO A NEW FILE. 9 | */ 10 | 11 | #INCLUDE 12 | #INCLUDE 13 | #INCLUDE 14 | #INCLUDE 15 | 16 | INT MAIN() { 17 | 18 | STD::IFSTREAM SOURCE("EX01.CPP", STD::IOS::IN); 19 | STD::OFSTREAM RESULT("EX01.TXT", STD::IOS::OUT); 20 | STD::OSTRINGSTREAM SS; 21 | 22 | SS << SOURCE.RDBUF(); 23 | STD::STRING CONTENT = SS.STR(); 24 | 25 | FOR (AUTO& S : CONTENT) 26 | S = TOUPPER(S); 27 | 28 | RESULT << CONTENT; 29 | 30 | RETURN 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex02.txt: -------------------------------------------------------------------------------- 1 | 2: Create a program that opens a file (the first argument 2 | 3: on the command line) and searches it for any one of a 3 | 4: set of words (the remaining arguments on the command line). 4 | 6: (with line numbers) that match to the new file. 5 | 22: arguments.emplace_back("command"); 6 | 23: arguments.emplace_back("file"); 7 | 24: arguments.emplace_back("="); 8 | 27: int line_counter = 0; 9 | 33: flag = false; 10 | 36: if ( buf.find(s) != std::string::npos ) { 11 | 37: flag = true; 12 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex03.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that adds a copyright notice to the beginning 3 | of all source-code files indicated by the program’s command-line arguments. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main() { 12 | /*Some command line arguments parsing here*/ 13 | std::ifstream source("ex03.cpp", std::ios::in); 14 | std::ofstream result("ex03_.cpp", std::ios::out); 15 | 16 | const std::string notice = "/**\n* Author: XXX\n* Date: xx.xx.xxxx\n* All rights reserved.\n*/\n\n"; 17 | 18 | std::ostringstream ss; 19 | 20 | ss << notice << source.rdbuf(); 21 | 22 | result << ss.str(); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex06.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Demonstrate to yourself that if the fail or bad bit 3 | is set, and you subsequently turn on stream exceptions, 4 | that the stream will immediately throw an exception. 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main() { 11 | 12 | try { 13 | std::ifstream my_stream("not found"); 14 | my_stream.exceptions(std::ios::failbit); 15 | } catch(std::ios_base::failure& e) { 16 | std::cout << "Exception detected: " << e.what() << '\n'; 17 | } 18 | 19 | 20 | 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex08.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/djkovrik/EckelCpp/fc534a974939475774fec852352134809fc458f6/Volume 2/4. iostreams/ex08.bin -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex09.txt: -------------------------------------------------------------------------------- 1 | 2 Lisa Masterson 17 Park street, 231 8372767 2 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex11.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write size_t(-1) (the largest unsigned int on your platform) 3 | to a text file 1,000,000 times. Repeat, but write to a binary file. 4 | Compare the size of the two files, and see how much room is saved 5 | using the binary format. (You may first want to calculate how much 6 | will be saved on your platform.) 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main() { 13 | 14 | std::ofstream bin("ex11.bin", std::ios::binary); 15 | std::ofstream txt("ex11.txt", std::ios::out); 16 | 17 | size_t z = size_t(-1); 18 | 19 | for (int i = 0; i < 1'000'000; ++i) { 20 | txt << z; 21 | bin.write(reinterpret_cast(&z), sizeof(z)); 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex12.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Discover the maximum number of digits of precision your 3 | implementation of iostreams will print by repeatedly 4 | increasing the value of the argument to precision( ) 5 | when printing a transcendental number such as sqrt(2.0). 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() { 13 | 14 | for (int i = 0; i < 45; i++) { 15 | std::cout.precision(i); 16 | std::cout << i << ": " << sqrt(2.0) << '\n'; 17 | } 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex13.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that reads real numbers from a file 3 | and prints their sum, average, minimum, and maximum. 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | int main() { 12 | 13 | std::ifstream source("ex13.txt", std::ios::in); 14 | std::stringstream ss; 15 | 16 | ss << source.rdbuf(); 17 | 18 | double d; 19 | std::vector doubles; 20 | 21 | while (ss >> d) 22 | doubles.push_back(d); 23 | 24 | double sum = 0; 25 | double min, max; 26 | 27 | min = max = doubles.front(); 28 | 29 | for (const auto& d : doubles) { 30 | sum += d; 31 | if (d > max) 32 | max = d; 33 | else if (d < min) 34 | min = d; 35 | } 36 | 37 | 38 | std::cout << "Sum: " << sum 39 | << "\nAverage: " << sum / doubles.size() 40 | << "\nMin: " << min 41 | << "\nMax: " << max << '\n'; 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Volume 2/4. iostreams/ex13.txt: -------------------------------------------------------------------------------- 1 | 2.1 3.2 15.745 65.3 7.5002 6.23 99.8003 43.6 56.221 0.002 2 | 54.615 28.2 66.666 92.4 24.1 63.2 76.22 8.3469 5.332 4.23 -------------------------------------------------------------------------------- /Volume 2/5. Templates/ex01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a unary function template that takes a single 3 | type template parameter. Create a full specialization 4 | for the type int. Also create a non-template overload 5 | for this function that takes a single int parameter. 6 | Have your main program invoke three function variations. 7 | */ 8 | 9 | #include 10 | 11 | template 12 | void foo(T t) { 13 | std::cout << "1. template \n void foo(T t)" << '\n'; 14 | } 15 | 16 | template<> 17 | void foo(int t) { 18 | std::cout << "2. template<>\n void foo(int t)" << '\n'; 19 | } 20 | 21 | void foo(int t) { 22 | std::cout << "3. void foo(int t)" << '\n'; 23 | } 24 | 25 | int main() { 26 | foo(2.2); 27 | foo<>(2); 28 | foo(2); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 2/5. Templates/ex10.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a class template that takes two type parameters. 3 | Define a partial specialization for the first parameter, 4 | and another partial specialization that specifies the 5 | second parameter. In each specialization, introduce 6 | members that are not in the primary template. 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | template 13 | class X { 14 | public: 15 | void f() { std::cout << "Primary template.\n"; } 16 | }; 17 | 18 | template 19 | class X { 20 | public: 21 | void f() { std::cout << "B = double.\n"; } 22 | }; 23 | 24 | template 25 | class X { 26 | public: 27 | void f() { std::cout << "A = char.\n"; } 28 | }; 29 | 30 | 31 | int main() { 32 | 33 | X().f(); 34 | X().f(); 35 | X().f(); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Create a generator that returns the current value of 3 | clock( ) (in ). Create a list, and fill 4 | it with your generator using generate_n( ). Remove any 5 | duplicates in the list and print it to cout using copy( ). 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int main() { 15 | 16 | std::list my_list; 17 | 18 | std::generate_n( std::back_inserter(my_list), 100000, []() { return clock(); } ); 19 | 20 | my_list.sort(); 21 | my_list.unique(); 22 | 23 | std::copy(my_list.begin(), my_list.end(), std::ostream_iterator(std::cout, "\n")); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Using transform( ) and toupper( ) (in ), write 3 | a single function call that will convert a string 4 | to all uppercase letters. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | std::string to_upper(const std::string str) { 13 | 14 | std::string result(str); 15 | 16 | std::transform(result.begin(), result.end(), result.begin(), 17 | [](char c) { return std::toupper(c); } 18 | ); 19 | 20 | return result; 21 | } 22 | 23 | int main() { 24 | 25 | std::cout << "Type a string: "; 26 | std::string s; 27 | std::cin >> s; 28 | std::cout << "Result: " << to_upper(s) << '\n'; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex03.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Create a Sum function object template that will 3 | accumulate all the values in a range when used with 4 | for_each( ). 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main() { 12 | 13 | int a[] = {1,2,3,4,5}; 14 | std::vector v(a, a + sizeof(a) / sizeof(a[0])); 15 | 16 | int sum = 0; 17 | std::for_each(v.begin(), v.end(), [&sum](int i) { sum += i; }); 18 | std::cout << sum << '\n'; 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex04.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write an anagram generator that takes a word as 3 | a command-line argument and produces all possible 4 | permutations of the letters. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int main() { 12 | 13 | std::string str; 14 | 15 | std::cout << "Type a string: "; 16 | std::cin >> str; 17 | 18 | std::cout << "Anagrams: \n"; 19 | 20 | std::sort(str.begin(), str.end()); 21 | 22 | 23 | 24 | do { 25 | std::cout << str << '\n'; 26 | } while(std::next_permutation(str.begin(), str.end())); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex09.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Write a program that takes an integer as a command 3 | line argument and finds all of its factors. 4 | */ 5 | 6 | #include 7 | 8 | int main() { 9 | 10 | int x = 0; 11 | std::cin >> x; 12 | 13 | for (int i = 2; i <= x; ++i) { 14 | while (x % i == 0) { 15 | x /= i; 16 | std::cout << i << '\n'; 17 | } 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex10.txt: -------------------------------------------------------------------------------- 1 | Write a program that takes as a command-line argument 2 | the name of a text file. Open this file and read it a 3 | word at a time (hint: use >>). Store each word into a 4 | vector. Force all the words to lowercase, sort 5 | them, remove all the duplicates, and print the results. -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex11_1.txt: -------------------------------------------------------------------------------- 1 | one two three four five 2 | two three one five four 3 | three five four six two -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex11_2.txt: -------------------------------------------------------------------------------- 1 | six seven eight nine ten 2 | ten six eight seven nine 3 | nine eight seven six two -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex15.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Fill a vector with numbers representing 3 | angles in radians. Using function object composition, 4 | take the sine of all the elements in your vector 5 | (see ). 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | const double pi = std::acos(-1); 14 | 15 | void print_sinus(double d) { 16 | std::cout << "sin(" << d << ") = " << std::sin(d * pi / 180) << '\n'; 17 | } 18 | 19 | int main() { 20 | 21 | double d[] = {30.0, 45, 90, 65.4, 15}; 22 | std::vector vd(d, d + sizeof(d)/sizeof(d[0])); 23 | 24 | //std::for_each(vd.begin(), vd.end(), print_sinus); 25 | 26 | std::for_each(vd.begin(), vd.end(), 27 | [](double d) { 28 | std::cout << "sin(" << d << ") = " << std::sin(d * pi / 180) << '\n'; 29 | } 30 | ); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex20_src.txt: -------------------------------------------------------------------------------- 1 | Using the characters generate a codebook using an input file 2 | given on the command line as a dictionary of words. Don’t worry 3 | about stripping off the non-alphabetic characters nor worry about 4 | case of the words in the dictionary file. Map each permutation 5 | of the character string to a word such as the following: etc. 6 | Make sure that no duplicate codes or words exist in your code book. 7 | Use lexicographical_compare() to perform a sort on the codes. 8 | Use your code book to encode the dictionary file. Decode your 9 | encoding of the dictionary file, and make sure you get the same 10 | contents back. -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex32.txt: -------------------------------------------------------------------------------- 1 | John Doe; age 34; phone 222 8945; salary 500$ 2 | Michael Bay; age 31 phone 756 3920; salary 110$ 3 | Jane Doe; age 29; phone 222 8432; salary 250$ 4 | Samantha White; age 34; phone 222 1234; salary 100$ 5 | Alex Petrov; age 28; phone 756 8592; salary 504$ 6 | Jane Kelly; age 33; phone 222 6002; salary 444$ 7 | 222 1111 222 2222 222 3333 -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex32_result.txt: -------------------------------------------------------------------------------- 1 | John Doe; age 34; phone 863 8945; salary 500$ 2 | Michael Bay; age 31 phone 756 3920; salary 110$ 3 | Jane Doe; age 29; phone 863 8432; salary 250$ 4 | Samantha White; age 34; phone 863 1234; salary 100$ 5 | Alex Petrov; age 28; phone 756 8592; salary 504$ 6 | Jane Kelly; age 33; phone 863 6002; salary 444$ 7 | 863 1111 863 2222 863 3333 -------------------------------------------------------------------------------- /Volume 2/6. Algorithms/ex33.txt: -------------------------------------------------------------------------------- 1 | John Doe 345 9483 2 | Nick Bonham 349 2930 3 | Jane Doe 283 2819 4 | John Black 345 5939 5 | Elly Bonham 349 2930 6 | Jane Burk 283 2819 7 | Alice Burk 345 2337 8 | Mike Black 283 5539 9 | Sam Burk 349 9943 10 | Kate Doe 346 2206 -------------------------------------------------------------------------------- /Volume 2/7. Containers/Noisy.cpp: -------------------------------------------------------------------------------- 1 | //: C07:Noisy.cpp {O} 2 | // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. 3 | // (c) 1995-2004 MindView, Inc. All Rights Reserved. 4 | // See source code use permissions stated in the file 'License.txt', 5 | // distributed with the code package available at www.MindView.net. 6 | #include "Noisy.h" 7 | long Noisy::create = 0, Noisy::assign = 0, 8 | Noisy::copycons = 0, Noisy::destroy = 0; 9 | NoisyReport NoisyReport::nr; 10 | ///:~ 11 | -------------------------------------------------------------------------------- /Volume 2/7. Containers/ex01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Create a set, open a file (whose name is provided 3 | on the command line), and read that file in a char at a 4 | time, placing each char in the set. Print the results, 5 | and observe the organization. Are there any letters in the 6 | alphabet that are not used in that particular file? 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | int main() { 14 | 15 | std::ifstream source("ex01.cpp", std::ios::in); 16 | std::set symbols; 17 | 18 | char c; 19 | while (source >> c) 20 | symbols.insert(c); 21 | 22 | for (const auto& s : symbols) 23 | std::cout << s << ' '; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Volume 2/7. Containers/ex04.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Create a generator that produces random int 3 | values between 0 and 20 inclusive, and use 4 | it to fill a multiset. Count the occurrences 5 | of each value, following the example given 6 | in MultiSetWordCount.cpp. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | int main() { 17 | 18 | std::multiset mi; 19 | 20 | std::generate_n(inserter(mi, mi.begin()), 25, 21 | []() { return std::rand()%20 + 1; } 22 | ); 23 | 24 | auto it = mi.begin(); 25 | while(it != mi.end()) { 26 | auto p = mi.equal_range(*it); 27 | int count = std::distance(p.first, p.second); 28 | std::cout << *it << ": " << count << '\n'; 29 | it = p.second; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 2/7. Containers/ex09.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Open a text file whose name is provided on the 3 | command line. Read the file a word at a time, 4 | and use a multiset to create a word 5 | count for each word. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | int main() { 15 | 16 | std::ifstream source("ex09.txt", std::ios::in); 17 | std::multiset words; 18 | 19 | std::string buf; 20 | 21 | while (source >> buf) 22 | words.insert(buf); 23 | 24 | auto it = words.begin(); 25 | while(it != words.end()) { 26 | auto p = words.equal_range(*it); 27 | int count = std::distance(p.first, p.second); 28 | std::cout << *it << ": " << count << '\n'; 29 | it = p.second; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 2/7. Containers/ex09.txt: -------------------------------------------------------------------------------- 1 | one four five six seven nine ten one two four six seven eight ten two four five six eight 2 | ten one four five six seven eight nine two five six five nine ten two three five six seven -------------------------------------------------------------------------------- /Volume 2/8. RTTI/ex02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Modify C16:AutoCounter.h in Volume 1 of this book so 3 | that it becomes a useful debugging tool. It will be 4 | used as a nested member of each class that you are 5 | interested in tracing. Turn AutoCounter into a template 6 | that takes the class name of the surrounding class as 7 | the template argument, and in all the error messages 8 | use RTTI to print the name of the class. 9 | */ 10 | 11 | #include "AutoCounter.h" 12 | 13 | class Base { 14 | public: 15 | Base() : acb(this) {} 16 | private: 17 | AutoCounter acb; 18 | }; 19 | 20 | class Derived : public Base { 21 | public: 22 | Derived() : acd(this) {} 23 | private: 24 | AutoCounter acd; 25 | }; 26 | 27 | int main() { 28 | 29 | Base b; 30 | Derived d; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 2/8. RTTI/ex03.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Use RTTI to assist in program debugging by printing 3 | out the exact name of a template using typeid( ). 4 | Instantiate the template for various types and see 5 | what the results are. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | template 14 | struct Bunch { 15 | A a; 16 | B b; 17 | C c; 18 | }; 19 | 20 | struct Abstract {}; 21 | 22 | int main() { 23 | 24 | Bunch b1; 25 | Bunch b2; 26 | Bunch, Abstract> b3; 27 | 28 | std::cout << typeid(b1).name() << '\n' 29 | << typeid(b2).name() << '\n' 30 | << typeid(b3).name() << '\n'; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Volume 2/9. Multiple Inheritance/ex15_log.txt: -------------------------------------------------------------------------------- 1 | Log has been created. 2 | A() 3 | A::f() 4 | A() 5 | B() 6 | C() 7 | D() 8 | D::f() 9 | ~D() 10 | ~C() 11 | ~B() 12 | ~A() 13 | ~A() 14 | Log has been closed. 15 | -------------------------------------------------------------------------------- /Volume 2/9. Multiple Inheritance/ex16.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Modify BreakTie.cpp by adding a class named VeryBottom 3 | that derives (non-virtually) from Bottom. VeryBottom 4 | should look just like Bottom except change "Left" to "Right" 5 | in the using declaration for f. Change main( ) to instantiate 6 | a VeryBottom instead of a Bottom object. Which f( ) gets called? 7 | */ 8 | 9 | #include 10 | 11 | class Top { 12 | public: 13 | virtual ~Top() {} 14 | }; 15 | 16 | class Left : virtual public Top { 17 | public: 18 | void f() { std::cout << "Left::f()\n"; } 19 | }; 20 | 21 | class Right : virtual public Top { 22 | public: 23 | void f() { std::cout << "Right::f()\n"; } 24 | }; 25 | 26 | class Bottom : public Left, public Right { 27 | public: 28 | using Left::f; 29 | }; 30 | 31 | class VeryBottom : public Bottom { 32 | public: 33 | using Right::f; 34 | }; 35 | 36 | 37 | int main() { 38 | //Bottom b; 39 | VeryBottom b; 40 | b.f(); 41 | } 42 | --------------------------------------------------------------------------------