├── 1 ├── array.cpp ├── const.cpp ├── datatype.cpp ├── datatype17.cpp ├── for_each.cpp ├── for_each_lambda.cpp ├── hello.cpp ├── loop.cpp ├── ranged.cpp ├── stdarray.cpp ├── stdarray2.cpp └── while.cpp ├── 2 ├── .std_array3.cpp.swp ├── raw_array.cpp ├── std_array3.cpp ├── std_map.cpp ├── std_queue.cpp ├── std_set.cpp ├── std_stack.cpp ├── std_unordered_map.cpp ├── std_vector.cpp └── std_vector2.cpp ├── 3 ├── ampersand.cpp ├── call_stack.cpp ├── constexpr.cpp ├── function1.cpp ├── function2.cpp ├── function3.cpp ├── function4.cpp ├── function5.cpp ├── function6.cpp ├── function7.cpp ├── math │ ├── .main.cpp.swp │ ├── .math.cpp.swp │ ├── .math.hpp.swp │ ├── main.cpp │ ├── math.cpp │ ├── math.hpp │ └── prog ├── pass_by_const_reference.cpp ├── pass_by_reference.cpp ├── polymorphism.cpp ├── scope.cpp ├── scope_error.cpp ├── scope_function.cpp ├── static.cpp ├── value.cpp └── value_address.cpp ├── 4 ├── ascii.cpp ├── cin.cpp ├── cout.cpp ├── filesystem.cpp ├── input.cpp ├── output.cpp ├── output.txt ├── proper.cpp ├── scanf.cpp ├── sscanf.cpp ├── string.cpp ├── text.txt ├── textWriterOutput.txt └── textwriter.cpp ├── 5 ├── address.cpp ├── address_operator.cpp ├── ampersand.cpp ├── arithmetic.cpp ├── arithmetic2.cpp ├── leak.cpp ├── pass_pointer_by_value.cpp ├── passbyreference.cpp ├── passbyvalue.cpp ├── pointer1.cpp ├── pointer2.cpp ├── pointer3.cpp ├── pointer4.cpp ├── pointer5.cpp ├── pointer6.cpp ├── segfault.cpp ├── sizeof.cpp ├── sizeof2.cpp └── type.cpp ├── 6 ├── address.cpp ├── arithmetic.cpp ├── arithmetic2.cpp ├── decay2.cpp ├── decay2_fixed.cpp ├── decay_brackets_warning.cpp ├── exercise.cpp ├── heap.cpp ├── multiple_pointers.cpp ├── pass_pointer_by_value.cpp ├── stack_risk.cpp └── static.cpp ├── 7 ├── array.cpp ├── array2.cpp ├── array2_std_copy.cpp ├── lerp.cpp ├── sfml │ ├── lines.cpp │ ├── simple.cpp │ └── simple2.cpp └── vector.cpp ├── 8 ├── .struct7.cpp.swp ├── exercise.cpp ├── list1.cpp ├── list2.cpp ├── list3.cpp ├── struct.cpp ├── struct1.cpp ├── struct2.cpp ├── struct3.cpp ├── struct4.cpp ├── struct5.cpp ├── struct6.cpp └── struct7.cpp ├── 9 ├── Math.cpp ├── Math.hpp ├── global_namespace │ └── main.cpp ├── main.cpp ├── namespace │ ├── Math.cpp │ ├── Math.hpp │ └── main.cpp ├── preprocessor │ ├── .preprocessor2.cpp.swp │ ├── preprocessor1.cpp │ └── preprocessor2.cpp └── scope │ ├── Point2D.cpp │ ├── Point2D.hpp │ └── main.cpp ├── 11 ├── array2d.cpp ├── gameloop.cpp └── image.cpp ├── 12 ├── array.cpp ├── copies.cpp ├── copyassignment.cpp ├── instance.cpp ├── intArray.cpp ├── intArrayBad.cpp ├── intArrayClass.cpp ├── intArrayFixed.cpp ├── intMyVectorError.cpp ├── list.cpp ├── objectFromScratch.cpp ├── struct.cpp ├── struct_malicious.cpp ├── struct_private.cpp ├── student.cpp └── template.cpp ├── 13 ├── intArrayDeepCopy.cpp ├── intArrayInitializer.cpp └── intArrayShallowCopy.cpp ├── 14 ├── composition.cpp ├── composition_bad.cpp ├── constructors.cpp ├── informationHiding.cpp ├── inheritance.cpp ├── purevirtual.cpp └── virtual.cpp ├── 15 ├── include │ └── BinaryTree.hpp └── src │ ├── BinaryTree.cpp │ └── main.cpp ├── 16 ├── anatomy.cpp ├── binarytree │ ├── include │ │ └── BinaryTree.hpp │ └── src │ │ ├── BinaryTree.cpp │ │ └── main.cpp ├── classtemplateproject │ ├── intArrayClass.hpp │ ├── intArrayClass.impl │ └── main.cpp ├── defaultArgs.cpp ├── error.cpp ├── friend.cpp ├── functiontemplate.cpp ├── functiontemplate2.cpp ├── functiontemplate3.cpp ├── functiontemplate4.cpp ├── insights.cpp ├── intArrayClass.cpp ├── max.cpp ├── max_fixed.cpp ├── multipleArgs.cpp ├── special.cpp ├── stl.cpp └── templatefriend.cpp ├── 17 ├── collision │ ├── include │ │ └── ColliderShape2D.hpp │ └── src │ │ └── main.cpp ├── intArrayClass.cpp ├── pointer.cpp ├── problems.cpp ├── shared.cpp ├── smart_pointer.cpp ├── stack.cpp ├── unique.cpp ├── vector.cpp ├── weak.cpp └── weak_pointers.cpp ├── 18 ├── Readme.md ├── average.cpp ├── average2.cpp ├── average3.cpp ├── average_algorithm.cpp ├── average_algorithm_is_sorted.cpp ├── average_algorithm_partition.cpp ├── iota.cpp ├── iterator.cpp ├── median.cpp ├── par.cpp ├── part5.cpp ├── ranged_for_more.cpp ├── rangedfor.cpp ├── vector.cpp ├── vector2.cpp ├── vector3.cpp ├── vector4.cpp └── vector5.cpp ├── 19 ├── async.cpp ├── atomics.cpp ├── cv.cpp ├── thread1.cpp ├── thread10.cpp ├── thread2.cpp ├── thread3.cpp ├── thread3_fix.cpp ├── thread4.cpp ├── thread5.cpp ├── thread6.cpp ├── thread7_deadlock.cpp ├── thread8_exception.cpp └── thread9.cpp ├── 22 ├── AdjacencyList.cpp ├── AdjacencyList.hpp ├── adjacency_list_unordered_map │ └── main.cpp ├── adjacency_matrix_unweighted │ └── main.cpp └── main.cpp ├── 24 ├── lvalue.cpp ├── lvalue_working.cpp ├── move.cpp ├── rvalue_move.cpp ├── simple_move.cpp └── simple_move_assignment.cpp ├── 25 ├── example.cpp └── lua │ ├── lua-5.4.4 │ ├── Makefile │ ├── README │ ├── doc │ │ ├── contents.html │ │ ├── index.css │ │ ├── logo.gif │ │ ├── lua.1 │ │ ├── lua.css │ │ ├── luac.1 │ │ ├── manual.css │ │ ├── manual.html │ │ ├── osi-certified-72x60.png │ │ └── readme.html │ └── src │ │ ├── Makefile │ │ ├── lapi.c │ │ ├── lapi.h │ │ ├── lapi.o │ │ ├── lauxlib.c │ │ ├── lauxlib.h │ │ ├── lauxlib.o │ │ ├── lbaselib.c │ │ ├── lbaselib.o │ │ ├── lcode.c │ │ ├── lcode.h │ │ ├── lcode.o │ │ ├── lcorolib.c │ │ ├── lcorolib.o │ │ ├── lctype.c │ │ ├── lctype.h │ │ ├── lctype.o │ │ ├── ldblib.c │ │ ├── ldblib.o │ │ ├── ldebug.c │ │ ├── ldebug.h │ │ ├── ldebug.o │ │ ├── ldo.c │ │ ├── ldo.h │ │ ├── ldo.o │ │ ├── ldump.c │ │ ├── ldump.o │ │ ├── lfunc.c │ │ ├── lfunc.h │ │ ├── lfunc.o │ │ ├── lgc.c │ │ ├── lgc.h │ │ ├── lgc.o │ │ ├── liblua.a │ │ ├── linit.c │ │ ├── linit.o │ │ ├── liolib.c │ │ ├── liolib.o │ │ ├── ljumptab.h │ │ ├── llex.c │ │ ├── llex.h │ │ ├── llex.o │ │ ├── llimits.h │ │ ├── lmathlib.c │ │ ├── lmathlib.o │ │ ├── lmem.c │ │ ├── lmem.h │ │ ├── lmem.o │ │ ├── loadlib.c │ │ ├── loadlib.o │ │ ├── lobject.c │ │ ├── lobject.h │ │ ├── lobject.o │ │ ├── lopcodes.c │ │ ├── lopcodes.h │ │ ├── lopcodes.o │ │ ├── lopnames.h │ │ ├── loslib.c │ │ ├── loslib.o │ │ ├── lparser.c │ │ ├── lparser.h │ │ ├── lparser.o │ │ ├── lprefix.h │ │ ├── lstate.c │ │ ├── lstate.h │ │ ├── lstate.o │ │ ├── lstring.c │ │ ├── lstring.h │ │ ├── lstring.o │ │ ├── lstrlib.c │ │ ├── lstrlib.o │ │ ├── ltable.c │ │ ├── ltable.h │ │ ├── ltable.o │ │ ├── ltablib.c │ │ ├── ltablib.o │ │ ├── ltm.c │ │ ├── ltm.h │ │ ├── ltm.o │ │ ├── lua │ │ ├── lua.c │ │ ├── lua.h │ │ ├── lua.hpp │ │ ├── lua.o │ │ ├── luac │ │ ├── luac.c │ │ ├── luac.o │ │ ├── luaconf.h │ │ ├── lualib.h │ │ ├── lundump.c │ │ ├── lundump.h │ │ ├── lundump.o │ │ ├── lutf8lib.c │ │ ├── lutf8lib.o │ │ ├── lvm.c │ │ ├── lvm.h │ │ ├── lvm.o │ │ ├── lzio.c │ │ ├── lzio.h │ │ └── lzio.o │ ├── lua01.cpp │ ├── lua02.cpp │ ├── lua03.cpp │ ├── lua03.lua │ ├── lua04.cpp │ ├── lua04.lua │ ├── lua05.cpp │ └── lua05.lua ├── README.md ├── backup ├── 8 │ ├── copyconstructor.cpp │ ├── intArrayClass.cpp │ └── project │ │ ├── intArrayClass.cpp │ │ ├── intArrayClass.hpp │ │ └── main.cpp ├── 9 │ ├── hello.cpp │ ├── hello.hpp │ ├── intArrayClass.cpp │ └── project │ │ ├── main.cpp │ │ ├── math.cpp │ │ └── math.hpp ├── 14 │ ├── intArrayClass.cpp │ ├── pointer.cpp │ ├── problems.cpp │ ├── shared.cpp │ ├── smart_pointer.cpp │ ├── stack.cpp │ ├── unique.cpp │ ├── vector.cpp │ ├── weak.cpp │ └── weak_pointers.cpp ├── 19 │ ├── shared_ptr_version │ │ ├── AdjacencyList.cpp │ │ ├── AdjacencyList.hpp │ │ └── main.cpp │ └── starter │ │ └── main.cpp ├── 20 │ ├── main.cpp │ └── sprite.bmp ├── 21 │ ├── prog │ ├── thread.cpp │ ├── thread2.cpp │ ├── thread3.cpp │ ├── thread4.cpp │ └── thread5.cpp ├── 22 │ ├── async.cpp │ └── new.cpp ├── 23 │ ├── accumulate.cpp │ ├── copy.cpp │ ├── find.cpp │ ├── nth.cpp │ ├── reverse.cpp │ ├── swap.cpp │ ├── unique.cpp │ └── vector.cpp └── 26 │ └── test.cpp └── project_samples └── fallingsands ├── README.md ├── include ├── Particle.hpp └── World.hpp └── src ├── Particle.cpp ├── World.cpp └── main.cpp /1/array.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 array.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g array.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file array.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Entry point to program 'main' function 17 | int main(int argc, char* argv[]){ 18 | 19 | int array[5]; 20 | array[0] = 0; 21 | array[1] = 1; 22 | array[2] = 2; 23 | array[3] = 3; 24 | array[4] = 4; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1/const.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 const.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g const.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file const.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | const float PI = 3.1415; 17 | 18 | // Entry point to program 'main' function 19 | int main(int argc, char* argv[]){ 20 | 21 | // Use << 'the stream operator' to output 22 | // multiple values. 23 | std::cout << "PI is: " << PI << std::endl; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /1/datatype.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 datatype.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g datatype.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file datatype.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Entry point to program 'main' function 17 | int main(int argc, char* argv[]){ 18 | 19 | // We can use the 'sizeof' queries the size of an 20 | // object in terms of bytes. 21 | std::cout << "sizeof(bool) = " << sizeof(bool) << std::endl; 22 | std::cout << "sizeof(char) = " << sizeof(char) << std::endl; 23 | std::cout << "sizeof(short) = " << sizeof(short) << std::endl; 24 | std::cout << "sizeof(int) = " << sizeof(int) << std::endl; 25 | std::cout << "sizeof(long) = " << sizeof(long) << std::endl; 26 | std::cout << "sizeof(float) = " << sizeof(float) << std::endl; 27 | std::cout << "sizeof(double)= " << sizeof(double) << std::endl; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1/datatype17.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 datatype17.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g datatype17.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file datatype17.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Entry point to program 'main' function 17 | int main(int argc, char* argv[]){ 18 | 19 | // We can use the 'sizeof' queries the size of an 20 | // object in terms of bytes. 21 | std::cout << "sizeof(std::byte) = " << sizeof(std::byte) << std::endl; 22 | std::cout << "sizeof(uint8_t) = " << sizeof(uint8_t) << std::endl; 23 | std::cout << "sizeof(int16_t) = " << sizeof(int16_t) << std::endl; 24 | std::cout << "sizeof(void*) = " << sizeof(void*) << std::endl; 25 | std::cout << "sizeof(int64_t) = " << sizeof(int64_t) << std::endl; 26 | std::cout << "sizeof(long double)= " << sizeof(long double) << std::endl; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /1/for_each.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(){ 6 | 7 | std::vector collection {1,2,3,4}; 8 | collection.push_back(5); 9 | 10 | std::for_each(cbegin(collection),cend(collection),[] (const auto& element){ 11 | std::cout << element << std::endl; 12 | }); 13 | 14 | return 0; 15 | } 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /1/for_each_lambda.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(){ 6 | 7 | std::vector collection {1,2,3,4}; 8 | collection.push_back(5); 9 | 10 | // lambda function 11 | // 'auto' automatically deduces the return type from the function 12 | // auto in general, allows us to use C++ 'more-like' a dynamic language. 13 | // (But keep in mind, C++ is a strongly statically typed language) 14 | auto println = [] (const auto& element){ 15 | std::cout << element << std::endl; 16 | }; 17 | 18 | // Call the function 'println' that we've made, on each element. 19 | std::for_each(cbegin(collection),cend(collection),println); 20 | 21 | return 0; 22 | } 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /1/hello.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 hello.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g hello.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file hello.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Entry point to program 'main' function 17 | int main(int argc, char* argv[]){ 18 | // From the standard library namespace 19 | // Output to standard output stream 20 | // a string "hello!" 21 | std::cout << "hello!" << std::endl; 22 | 23 | // Return from our function the value 24 | // 0, which typically means 'success' from 25 | // the main function 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /1/loop.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 loop.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g loop.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file loop.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | // Standard library array 20 | // Created with 'integers' 21 | // This one also constructed with '5' 22 | std::array arr; 23 | arr.fill(7); 24 | // Iterate through our array 25 | for(int i=0; i < arr.size(); i++){ 26 | std::cout << arr.at(i) << std::endl; 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1/ranged.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 ranged.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g ranged.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file ranged.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | // Standard library array 20 | // Created with 'integers' 21 | // This one also constructed with '5' 22 | std::array arr; 23 | arr.fill(7); 24 | // Iterate through our array 25 | for(auto &i : arr){ 26 | std::cout << i << std::endl; 27 | } 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /1/stdarray.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 stdarray.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g stdarray.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file stdarray.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | // Standard library array 20 | // Created with 'integers' 21 | // This one also constructed with '5' 22 | std::array arr; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /1/stdarray2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 stdarray2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g stdarray2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file stdarray2.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | // Standard library array 20 | // Created with 'integers' 21 | // This one also constructed with '5' 22 | std::array arr; 23 | arr.fill(7); // Fill array with 7's 24 | std::cout << "Confirm a 7: " << arr[3] << std::endl; 25 | // Another way to index into an array, 'at' uses bounds checking 26 | // to ensure that you do not go out of bounds 27 | std::cout << "Confirm a 7: " << arr.at(3) << std::endl; 28 | std::cout << "Check our size of array: " << arr.size() << std::endl; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /1/while.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 while.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g while.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file while.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | 20 | while(true){ 21 | std::cout << "print forever" << std::endl; 22 | } 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /11/array2d.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 array2d.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g array2d.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file array2d.cpp 11 | #include // I/O stream library 12 | 13 | void printArray(int* array, int w, int h){ 14 | for(int y=0; y < h; y++){ 15 | for(int x=0; x < w; x++){ 16 | std::cout << array[y*w+x] << "\t"; 17 | } 18 | std::cout << std::endl; 19 | } 20 | } 21 | 22 | // Entry point to program 'main' array2d.cpp 23 | int main(int argc, char* argv[]){ 24 | 25 | const int width = 4; 26 | const int height = 3; 27 | 28 | int* array = new int[width*height]; 29 | 30 | int value=0; 31 | for(int y=0; y < height; y++){ 32 | for(int x=0; x < width; x++){ 33 | array[y*width+x] = value; 34 | value++; 35 | } 36 | } 37 | 38 | printArray(array,width,height); 39 | 40 | 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /11/gameloop.cpp: -------------------------------------------------------------------------------- 1 | // g++ -g gameloop.cpp -o prog -lsfml-system -lsfml-graphics -lsfml-window && ./prog 2 | // @file gameloop.cpp 3 | #include 4 | 5 | int main(){ 6 | 7 | // Main render window 8 | sf::RenderWindow renderWindow(sf::VideoMode(400,400),"Game Loop Structure"); 9 | // Setup Objects 10 | sf::CircleShape shape(25.0f); 11 | 12 | // Main Application Loop 13 | while(renderWindow.isOpen()){ 14 | 15 | // Handle Events 16 | sf::Event event; 17 | while(renderWindow.pollEvent(event)){ 18 | if(event.type == sf::Event::Closed){ 19 | renderWindow.close(); 20 | } 21 | } 22 | 23 | // Handle Inputs 24 | if(sf::Mouse::isButtonPressed(sf::Mouse::Left)){ 25 | sf::Vector2i mouse = sf::Mouse::getPosition(renderWindow); 26 | shape.setPosition(mouse.x,mouse.y); 27 | } 28 | 29 | // ======== Draw our scene ============= 30 | // Clear the window 31 | renderWindow.clear(); 32 | // Draw the sprite 33 | renderWindow.draw(shape); 34 | // Display the contents drawn to the renderWindow 35 | renderWindow.display(); 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /12/copies.cpp: -------------------------------------------------------------------------------- 1 | //@file copies.cpp 2 | #include 3 | 4 | class T{ 5 | public: 6 | T(){ std::cout << "constructor called\n"; } 7 | ~T(){ std::cout << "destructor called\n"; } 8 | 9 | T(const T& copy){ 10 | std::cout << "copy constructor called\n"; 11 | } 12 | }; 13 | 14 | // Pass by Value 15 | void functionValue(T object){ 16 | // Remember why we pass by value 17 | // versus pass by reference? 18 | // Observe the extra copies made! 19 | } 20 | 21 | // Pass by Ref 22 | void functionRef(const T& object){ 23 | // Remember why we pass by value 24 | // versus pass by reference? 25 | // Observe the extra copies made! 26 | } 27 | 28 | int main(){ 29 | 30 | T object1; 31 | 32 | for(int i=0; i < 10; i++){ 33 | //functionValue(object1); 34 | // UNCOMMENT THE LINE BELOW AND 35 | // COMMENT THE LINE ABOVE TO SEE THE DIFFERNCE 36 | functionRef(object1); 37 | } 38 | 39 | 40 | 41 | 42 | // This is wher %rbp will return 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /12/copyassignment.cpp: -------------------------------------------------------------------------------- 1 | //@file copyassignment.cpp 2 | #include 3 | 4 | class T{ 5 | int data; 6 | public: 7 | T(){ std::cout << "constructor called\n"; } 8 | ~T(){ std::cout << "destructor called\n"; } 9 | // Copy constructor 10 | T(const T& copy){ 11 | std::cout << "copy constructor called\n"; 12 | this->data = copy.data; 13 | } 14 | // Copy assignment 15 | T& operator=(const T& rhs){ 16 | std::cout << "copy assignment operator called\n"; 17 | // Special case, meaning, avoid copying ourselves 18 | if(&rhs == this){ 19 | return *this; 20 | } 21 | this->data = rhs.data; 22 | 23 | return *this; 24 | } 25 | void SetData(int value){ 26 | data = value; 27 | } 28 | void printData(){ 29 | std::cout << this << ": " << data << std::endl; 30 | } 31 | }; 32 | 33 | int main(){ 34 | 35 | T object1; 36 | object1.SetData(77); 37 | T object2 = object1; 38 | T object3; 39 | 40 | // Copy Assignment here -- both objects exist already! 41 | object3 = object1; // a.k.a: object1=(object3); 42 | 43 | object1.printData(); 44 | object2.printData(); 45 | object3.printData(); 46 | 47 | 48 | // This is wher %rbp will return 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /12/instance.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 instance.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g instance.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file instance.cpp 11 | #include // I/O stream library 12 | #include // Get type information 13 | 14 | struct Student{ 15 | int age; 16 | }; 17 | 18 | struct Animal{ 19 | int age; 20 | }; 21 | 22 | 23 | // Entry point to program 'main' instance.cpp 24 | int main(int argc, char* argv[]){ 25 | 26 | Student s1; // Create an instance of an object 27 | // The type of this instance is 'Student' 28 | std::cout << "what type is s1?: " << typeid(s1).name() << std::endl; 29 | Animal a1; // Create an instance of an object 30 | // The type of this instance is 'Animal' 31 | std::cout << "what type is a1?: " << typeid(a1).name() << std::endl; 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /12/list.cpp: -------------------------------------------------------------------------------- 1 | //@file list.cpp 2 | #include 3 | 4 | struct Node{ 5 | int data; 6 | Node* next; 7 | // or you can type 'struct Node* next;' 8 | }; 9 | 10 | 11 | void printList(Node* root){ 12 | // Responsible for pointing to the next node. 13 | Node* iterator; 14 | iterator = root; 15 | while(iterator != nullptr){ 16 | std::cout << iterator->data << " "; 17 | iterator = iterator->next; 18 | } 19 | 20 | } 21 | 22 | int main(){ 23 | 24 | Node n1; 25 | Node n2; 26 | Node n3; 27 | 28 | n1.data = 1; 29 | n1.next = &n2; 30 | 31 | n2.data = 2; 32 | n2.next = &n3; 33 | 34 | n3.data = 3; 35 | n3.next = nullptr; 36 | 37 | printList(&n1); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /12/struct.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 struct.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g struct.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct.cpp 11 | #include 12 | 13 | struct myIntVector{ 14 | public: // By default this is implicit in a struct 15 | int* storage; 16 | int size; 17 | myIntVector(int _size){ // A constructor 18 | storage = new int[_size]; 19 | for(int i =0; i < _size; i++){ 20 | storage[i] = i; 21 | } 22 | size = _size; // save size; 23 | } 24 | ~myIntVector(){delete[] storage;} 25 | // print method 26 | void print(){ 27 | for(int i =0; i < size; i++){ 28 | std::cout << storage[i] << "\n"; 29 | } 30 | } 31 | }; 32 | 33 | 34 | // Entry point to program 'main' struct.cpp 35 | int main(){ 36 | myIntVector a(20); 37 | a.print(); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /12/struct_malicious.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 struct_malicious.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g struct_malicious.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct_malicious.cpp 11 | #include 12 | 13 | struct myIntVector{ 14 | public: // By default this is implicit in a struct 15 | int* storage; 16 | int size; 17 | myIntVector(int _size){ // A constructor 18 | storage = new int[_size]; 19 | for(int i =0; i < _size; i++){ 20 | storage[i] = i; 21 | } 22 | size = _size; // save size; 23 | } 24 | ~myIntVector(){delete[] storage;} 25 | // print method 26 | void print(){ 27 | for(int i =0; i < size; i++){ 28 | std::cout << storage[i] << "\n"; 29 | } 30 | } 31 | }; 32 | 33 | 34 | // Entry point to program 'main' struct_malicious.cpp 35 | int main(){ 36 | myIntVector a(20); 37 | a.size = 5000; // hehe -- I am malicious or otherwise 38 | a.print(); // unknowingly breaking the behavior 39 | // of this program 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /12/struct_private.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 struct_private.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g struct_private.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct_private.cpp 11 | #include // I/O stream library 12 | 13 | struct myIntVector{ 14 | private: 15 | int size; // Now size is hidden, cannot be accessed anywhere! 16 | public: 17 | int* storage; 18 | myIntVector(int _size){ 19 | storage = new int[_size]; 20 | for(int i =0; i < _size; i++){ 21 | storage[i] = i; 22 | } 23 | size = _size; 24 | } 25 | ~myIntVector(){delete[] storage;} 26 | // print method 27 | void print(){ 28 | for(int i =0; i < size; i++){ 29 | std::cout << storage[i] << "\n"; 30 | } 31 | } 32 | }; 33 | 34 | // Entry point to program 'main' struct_private.cpp 35 | int main(){ 36 | myIntVector a(20); 37 | // a.size = 5000; // This line would not compile! 38 | a.print(); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /12/template.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 heap.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g heap.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file heap.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' heap.cpp 14 | int main(int argc, char* argv[]){ 15 | 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /14/composition.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // See preprocessor output: g++ -std=c++17 composition.cpp - 3 | // 4 | // Compile with: g++ -std=c++17 composition.cpp -o prog 5 | // Run with : ./prog 6 | // 7 | // Compile for debugging with: 8 | // g++ -std=c++17 -g composition.cpp -o prog 9 | // Run with gdb: gdb ./prog --tui 10 | ///////////////////////////////////////////////// 11 | 12 | // @file composition.cpp 13 | #include 14 | 15 | struct Wheel{ 16 | int tirePressure; 17 | int diameter; 18 | } 19 | 20 | struct Engine{ 21 | int Liters; 22 | int cylinders; 23 | } 24 | 25 | // For examples in class, I will sometimes, not separate 26 | // the interface from the implementation, so you can see, everthing on one screen! 27 | class Car{ 28 | // We are composing our car with 29 | // a data type, vector of wheels. 30 | std::vector wheel; 31 | // Another example of composition 32 | Engine m_engine; 33 | 34 | public: 35 | Car(){} 36 | ~Car(){} 37 | }; 38 | 39 | 40 | // Entry point to program 'main' composition.cpp 41 | int main(){ 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /14/composition_bad.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // See preprocessor output: g++ -std=c++17 composition_bad.cpp - 3 | // 4 | // Compile with: g++ -std=c++17 composition_bad.cpp -o prog 5 | // Run with : ./prog 6 | // 7 | // Compile for debugging with: 8 | // g++ -std=c++17 -g composition_bad.cpp -o prog 9 | // Run with gdb: gdb ./prog --tui 10 | ///////////////////////////////////////////////// 11 | #include 12 | 13 | // @file composition_bad.cpp 14 | class Car{ 15 | // Represent the wheels 16 | int tirePressure[4]; 17 | int diameter[4]; 18 | // Represent the engine 19 | int Liters; 20 | int cylinders; 21 | 22 | public: 23 | Car(){} 24 | ~Car(){} 25 | }; 26 | 27 | 28 | // Entry point to program 'main' composition_bad.cpp 29 | int main(){ 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /14/informationHiding.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // See preprocessor output: g++ -std=c++17 informationHiding.cpp - 3 | // 4 | // Compile with: g++ -std=c++17 informationHiding.cpp -o prog 5 | // Run with : ./prog 6 | // 7 | // Compile for debugging with: 8 | // g++ -std=c++17 -g informationHiding.cpp -o prog 9 | // Run with gdb: gdb ./prog --tui 10 | ///////////////////////////////////////////////// 11 | 12 | // @file informationHiding.cpp 13 | 14 | struct POD{ 15 | float x,y,z; 16 | private: 17 | int important; 18 | }; 19 | 20 | // Entry point to program 'main' informationHiding.cpp 21 | int main(){ 22 | 23 | POD object; 24 | object.x = 1; 25 | object.y = 1; 26 | object.z = 1; 27 | 28 | // We want to avoid exposing all of our internals 29 | // to the user. 30 | object.important = 7; 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /14/purevirtual.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // See preprocessor output: g++ -std=c++17 purevirtual.cpp - 3 | // 4 | // Compile with: g++ -std=c++17 purevirtual.cpp -o prog 5 | // Run with : ./prog 6 | // 7 | // Compile for debugging with: 8 | // g++ -std=c++17 -g purevirtual.cpp -o prog 9 | // Run with gdb: gdb ./prog --tui 10 | ///////////////////////////////////////////////// 11 | 12 | // @file purevirtual.cpp 13 | #include 14 | 15 | class Shape{ 16 | public: 17 | int width, height; 18 | // Here is a pure virtual function in C++ 19 | // 20 | // It forces, any derived classes, to implement 21 | // this member function 22 | virtual int area() = 0; 23 | 24 | }; 25 | 26 | class Rectangle: public Shape{ 27 | public: 28 | Rectangle(){ 29 | } 30 | 31 | int area(){ 32 | std::cout << "Rectangle::area()" << std::endl; 33 | return width*height; 34 | } 35 | 36 | }; 37 | 38 | 39 | // Entry point to program 'main' purevirtual.cpp 40 | int main(){ 41 | 42 | Rectangle r; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /15/include/BinaryTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_HPP 2 | #define BINARY_TREE_HPP 3 | 4 | struct BinaryNode{ 5 | int value; 6 | BinaryNode* left; 7 | BinaryNode* right; 8 | }; 9 | 10 | class BinaryTree{ 11 | private: 12 | BinaryNode* m_root; 13 | 14 | // Private helper function, to help 15 | // make insertion easier. 16 | void Insert(int value, BinaryNode* node); 17 | // Private helper function to help 18 | // with search 19 | bool Exists(int value, BinaryNode* node); 20 | // Private hlper function to enable, 21 | // printing. 22 | void Print(BinaryNode* node); 23 | public: 24 | // Constructor 25 | BinaryTree(); 26 | // Destructor 27 | ~BinaryTree(); 28 | // Copy Constructor 29 | BinaryTree(const BinaryTree& copy); 30 | // Copy Assignment Constructor 31 | BinaryTree& operator=(const BinaryTree& rhs); 32 | 33 | void Insert(int value); 34 | bool Exists(int value); 35 | void Print(); 36 | // Delete(); 37 | }; 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /15/src/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 main.cpp BinaryTree.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g main.cpp BinaryTree.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file main.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | #include "BinaryTree.hpp" 17 | 18 | // Entry point to program 'main' function 19 | int main(int argc, char* argv[]){ 20 | 21 | BinaryTree tree; 22 | tree.Insert(4); 23 | tree.Insert(2); 24 | tree.Insert(3); 25 | tree.Insert(5); 26 | tree.Insert(7); 27 | 28 | std::cout << "Is 3 in the tree? " << tree.Exists(3) << std::endl; 29 | std::cout << "Is 9 in the tree? " << tree.Exists(9) << std::endl; 30 | std::cout << "Is 7 in the tree? " << tree.Exists(7) << std::endl; 31 | 32 | tree.Print(); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /16/anatomy.cpp: -------------------------------------------------------------------------------- 1 | // @file anatomy.cpp 2 | 3 | template 4 | T name(T arg){ 5 | // Function body 6 | // .... 7 | // 8 | return arg; 9 | } 10 | 11 | 12 | -------------------------------------------------------------------------------- /16/binarytree/include/BinaryTree.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BINARY_TREE_HPP 2 | #define BINARY_TREE_HPP 3 | 4 | struct BinaryNode{ 5 | int value; 6 | BinaryNode* left; 7 | BinaryNode* right; 8 | }; 9 | 10 | class BinaryTree{ 11 | private: 12 | BinaryNode* m_root; 13 | 14 | // Private helper function, to help 15 | // make insertion easier. 16 | void Insert(int value, BinaryNode* node); 17 | // Private helper function to help 18 | // with search 19 | bool Exists(int value, BinaryNode* node); 20 | // Private hlper function to enable, 21 | // printing. 22 | void Print(BinaryNode* node); 23 | public: 24 | // Constructor 25 | BinaryTree(); 26 | // Destructor 27 | ~BinaryTree(); 28 | // Copy Constructor 29 | BinaryTree(const BinaryTree& copy); 30 | // Copy Assignment Constructor 31 | BinaryTree& operator=(const BinaryTree& rhs); 32 | 33 | void Insert(int value); 34 | bool Exists(int value); 35 | void Print(); 36 | // Delete(); 37 | }; 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /16/binarytree/src/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 ./src/*.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g ./src/*.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file main.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | #include "BinaryTree.hpp" 17 | 18 | // Entry point to program 'main' function 19 | int main(int argc, char* argv[]){ 20 | 21 | BinaryTree tree; 22 | tree.Insert(4); 23 | tree.Insert(2); 24 | tree.Insert(3); 25 | tree.Insert(5); 26 | tree.Insert(7); 27 | 28 | std::cout << "Is 3 in the tree? " << tree.Exists(3) << std::endl; 29 | std::cout << "Is 9 in the tree? " << tree.Exists(9) << std::endl; 30 | std::cout << "Is 7 in the tree? " << tree.Exists(7) << std::endl; 31 | 32 | tree.Print(); 33 | 34 | return 0; 35 | } 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /16/classtemplateproject/intArrayClass.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ARRAY_HPP 2 | #define ARRAY_HPP 3 | 4 | namespace mike{ 5 | 6 | // @file classtemplateproject/intArrayClass.hpp 7 | template 8 | class Array{ 9 | // By default all of our member variables and 10 | // member functions are private in a class 11 | T* m_storage; // Sometimes I prefix member variables 12 | int m_size; // with 'm_' to indicate they are member variables 13 | public: 14 | // Our constructor 15 | Array(int _size); 16 | // Destructor 17 | ~Array(); 18 | // Copy constructor 19 | Array(const Array& copy); 20 | // copy assignment operator 21 | Array& operator=(const Array& rhs); 22 | 23 | void Print(); 24 | 25 | int GetSize() const; 26 | }; 27 | 28 | } 29 | 30 | // Implementation file of our template header 31 | #include "intArrayClass.impl" 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /16/classtemplateproject/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 main.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g main.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file classtemplateproject/main.cpp 11 | #include 12 | 13 | #include "intArrayClass.hpp" 14 | 15 | 16 | // Entry point to program 'main' intArrayClass.cpp 17 | int main(){ 18 | 19 | // IntArray is now in the class scope, with 20 | // the namespace 'mike' 21 | mike::Array a(20); 22 | // IntArray b =a ; 23 | mike::Array b(10); 24 | b = a; 25 | std::cout << "m_size = " << a.GetSize(); // Access value for read only 26 | a.Print(); 27 | b.Print(); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /16/defaultArgs.cpp: -------------------------------------------------------------------------------- 1 | // @file defaultArgs.cpp 2 | 3 | template 4 | void function(T1 arg1, T2 arg2){ 5 | // Function body 6 | } 7 | 8 | int main(){ 9 | function(5,7); // By default, second argument 10 | // is treated as an 'int' 11 | function(true,false); 12 | 13 | return 0; 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /16/error.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 error.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g error.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file error.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | class SomeObject{ 15 | public: 16 | SomeObject(){ 17 | } 18 | 19 | }; 20 | 21 | // Entry point to program 'main' error 22 | int main(int argc, char* argv[]){ 23 | 24 | SomeObject myObject; 25 | 26 | std::cout << myObject << std::endl; 27 | 28 | 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /16/friend.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 friend.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g friend.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file friend.cpp 11 | #include 12 | 13 | class SomeClass{ 14 | public: 15 | SomeClass(){} 16 | ~SomeClass(){} 17 | int GetPrivateValue() { 18 | return m_privateValue; 19 | } 20 | // Declare that this function is a 'friend' 21 | // of the class, and thus can see *anything* inside 22 | // of the class 23 | friend void FriendFunctionOfSomeClass(SomeClass& s); 24 | private: 25 | int m_privateValue; 26 | }; 27 | 28 | // friend function can access the private member 29 | // variables of a class 30 | void FriendFunctionOfSomeClass(SomeClass& s){ 31 | s.m_privateValue = 42; 32 | } 33 | 34 | // Entry point to program 'main' friend.cpp 35 | int main(){ 36 | SomeClass object; 37 | FriendFunctionOfSomeClass(object); 38 | std::cout << "private value of object is now:" << object.GetPrivateValue() << std::endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /16/functiontemplate.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 functiontemplate.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g functiontemplate.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file functiontemplate.cpp 11 | #include // I/O stream library 12 | 13 | int square(int x){ 14 | return x*x; 15 | } 16 | 17 | float square(float x){ 18 | return x*x; 19 | } 20 | 21 | // Note 'T' is replaced everywhere 22 | // with the data type we specify in the 23 | // brackets. 24 | template 25 | T squareTemplated(T x){ 26 | return x*x; 27 | } 28 | 29 | // Entry point to program 'main' functiontemplate 30 | int main(int argc, char* argv[]){ 31 | 32 | std::cout << square(5) << std::endl; 33 | std::cout << squareTemplated(5) << std::endl; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /16/functiontemplate2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 functiontemplate2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g functiontemplate2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file functiontemplate2.cpp 11 | #include // I/O stream library 12 | 13 | template 14 | T squareTemplated(T x){ 15 | return x*x; 16 | } 17 | 18 | // Entry point to program 'main' functiontemplate2 19 | int main(int argc, char* argv[]){ 20 | 21 | std::cout << squareTemplated(5) << std::endl; 22 | std::cout << squareTemplated(5.0f) << std::endl; 23 | std::cout << squareTemplated(1) << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /16/functiontemplate3.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 functiontemplate3.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g functiontemplate3.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file functiontemplate3.cpp 11 | #include // I/O stream library 12 | 13 | template 14 | T squareTemplated(T x){ 15 | return x*x; 16 | } 17 | 18 | // Entry point to program 'main' functiontemplate2 19 | int main(int argc, char* argv[]){ 20 | 21 | // Observe, compiler can deduce type 22 | std::cout << squareTemplated(5) << std::endl; 23 | std::cout << squareTemplated(5.0f) << std::endl; 24 | std::cout << squareTemplated(1) << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /16/functiontemplate4.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 functiontemplate4.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g functiontemplate4.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file functiontemplate4.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | template 15 | T squareTemplated(T x){ 16 | return x*x; 17 | } 18 | 19 | // Entry point to program 'main' functiontemplate4 20 | int main(int argc, char* argv[]){ 21 | 22 | // Observe, compiler can deduce type 23 | auto one = squareTemplated(5); 24 | auto two = squareTemplated(5.0f); 25 | auto three = squareTemplated(1); 26 | 27 | std::cout << one << '\n' 28 | << two << '\n' 29 | << three << std::endl; 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /16/insights.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 insights.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g insights.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file insights.cpp 11 | #include // I/O stream library 12 | 13 | template 14 | T squareTemplated(T x){ 15 | return x*x; 16 | } 17 | 18 | // Handling a special situation 19 | template<> 20 | bool squareTemplated(bool x) 21 | { 22 | std::cout << "bool version" << std::endl; 23 | // Always return false when handling booleans 24 | // alternative...maybe just through an error... 25 | return false; 26 | } 27 | 28 | // Entry point to program 'main' insights 29 | int main(int argc, char* argv[]){ 30 | 31 | std::cout << squareTemplated(5) << std::endl; 32 | std::cout << squareTemplated(5.0f) << std::endl; 33 | std::cout << squareTemplated(1) << std::endl; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /16/max.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 max.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g max.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file max.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | template 15 | T1 max(T1 a, T2 b){ 16 | return b < a ? a : b; 17 | } 18 | 19 | // Entry point to program 'main' max 20 | int main(int argc, char* argv[]){ 21 | 22 | std::cout << max(7.3f,7) << std::endl;; 23 | // This is a bug! 24 | std::cout << max(7L,7.1f) << std::endl;; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /16/max_fixed.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 max_fixed.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g max_fixed.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file max_fixed.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | template 15 | auto max(T1 a, T2 b){ 16 | return b < a ? a : b; 17 | } 18 | 19 | // Entry point to program 'main' max_fixed 20 | int main(int argc, char* argv[]){ 21 | 22 | std::cout << max(7.3f,7) << std::endl;; 23 | // This is a bug! 24 | std::cout << max(7L,7.1f) << std::endl;; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /16/multipleArgs.cpp: -------------------------------------------------------------------------------- 1 | // @file multipleArgs.cpp 2 | 3 | template 4 | void function(T1 arg1, T2 arg2){ 5 | // Function body 6 | } 7 | 8 | int main(){ 9 | 10 | function(true,7); 11 | function(5.0f,"hi"); 12 | 13 | return 0; 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /16/special.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 special.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g special.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file special.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | template 15 | T squareTemplated(T x){ 16 | return x*x; 17 | } 18 | 19 | // Here's a specialized version 20 | template<> 21 | int squareTemplated(std::string x){ 22 | std::cout << "Specialized version" << std::endl; 23 | return -1; 24 | } 25 | 26 | // Entry point to program 'main' special 27 | int main(int argc, char* argv[]){ 28 | 29 | std::cout << squareTemplated(5) << std::endl; 30 | std::cout << squareTemplated("hi") << std::endl; 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /16/stl.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 stl.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g stl.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file stl.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | 15 | // Entry point to program 'main' stl 16 | int main(int argc, char* argv[]){ 17 | 18 | std::vector myVector; 19 | myVector.push_back(7.0); 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /17/pointer.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer 14 | int main(int argc, char* argv[]){ 15 | 16 | float* p_float = nullptr; // I point to floats 17 | float some_float = 3.5f; // I am a regular float 18 | p_float = &some_float; // Point to address of variable 19 | p_float = &(some_float); // Same line as above, but useful 20 | // to understand '&' is an operator, 21 | // but we can think of the same as 22 | // a function. 23 | 24 | std::cout << "dereference p_float: " << *p_float << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /17/problems.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 problems.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g problems.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file problems.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' problems 14 | int main(int argc, char* argv[]){ 15 | 16 | // (1) Dereference nullptr 17 | float* p_float = nullptr; 18 | *p_float = 7.0f; // ERROR - Dereferencing a null pointer 19 | 20 | // (2) Double free 21 | int* array = new int[10]; 22 | delete[] array; 23 | delete[] array; // ERROR - 'Double free' 24 | 25 | // (3) Dangling pointer - refering to something that 26 | // has already been deleted 27 | int* shared_array = new int[10]; 28 | int* pointer_to_array = shared_array; 29 | delete[] shared_array; 30 | pointer_to_array[0] = 7; // ERROR - Cannot derefernce, because 31 | // we deleted the shared_array 32 | // that we were pointing to. 33 | 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /17/smart_pointer.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 smart_pointer.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g smart_pointer.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file smart_pointer.cpp 11 | #include // I/O stream library 12 | 13 | // Let's build a smart pointer for handling integers. 14 | class SmartIntPointer{ 15 | private: 16 | int* m_pointer = nullptr; // initialize the pointer 17 | public: 18 | // Handle creation of our 19 | SmartIntPointer(){ 20 | m_pointer = nullptr; 21 | } 22 | // Destructor 23 | ~SmartIntPointer(){ 24 | if(m_pointer!=nullptr){ 25 | delete m_pointer; 26 | } 27 | } 28 | // Handle dereferencing 29 | int* operator->(){ 30 | return this->m_pointer; 31 | } 32 | // And more.... 33 | // To be continued when we learn about move semantics... 34 | 35 | 36 | }; 37 | 38 | // Entry point to program 'main' smart_pointer 39 | int main(int argc, char* argv[]){ 40 | 41 | SmartIntPointer myPointer; 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /17/stack.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 stack.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g stack.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file stack.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | // Global piece of data 15 | char* g_pointer; 16 | 17 | void foo(){ 18 | // Chunk of memory, allocated in foo 19 | char array[100]; 20 | 21 | std::memset(array,65,sizeof(char)*100); 22 | 23 | g_pointer = &array[0]; 24 | std::cout << "g_pointer: " << g_pointer[0] << std::endl; 25 | } 26 | 27 | 28 | // Entry point to program 'main' stack 29 | int main(int argc, char* argv[]){ 30 | g_pointer = new char[1]; 31 | 32 | // Stack allocated 33 | char* p_main = nullptr; 34 | foo(); 35 | char array[9000]; 36 | std::memset(array,66,sizeof(char)*9000); 37 | 38 | std::cout << "g_pointer: " << g_pointer[0] << std::endl; 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /17/unique.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 unique.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g unique.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file unique.cpp 11 | #include // I/O stream library 12 | #include // Access smart pointers 13 | 14 | class Object{ 15 | public: 16 | Object() { std::cout << "Constructor\n"; } 17 | ~Object() { std::cout << "Destructor\n"; } 18 | }; 19 | 20 | 21 | // Entry point to program 'main' unique 22 | int main(int argc, char* argv[]){ 23 | 24 | { 25 | std::unique_ptr myObjectPtr(new Object); 26 | // NOTE: Below is 'illegal' because we cannot 27 | // assign to a unique_ptr--awesome! 28 | //std::unique_ptr myObjectPtr2 = myObjectPtr; 29 | } 30 | 31 | // An alternative way to create the pointer 32 | // Much more explict, avoids the call to 'new' 33 | // Also does some error handling. 34 | { 35 | std::unique_ptr myObjectPtr = std::make_unique(); 36 | } 37 | 38 | 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /17/vector.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 vector.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g vector.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file vector.cpp 11 | #include // I/O stream library 12 | #include 13 | #include 14 | 15 | // Entry point to program 'main' vector 16 | int main(int argc, char* argv[]){ 17 | 18 | 19 | // I prefer this style 20 | std::vector> vector_of_smart_ints; 21 | // over the below 22 | std::vector vector_of_ints; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /17/weak.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 weak.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g weak.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file weak.cpp 11 | #include // I/O stream library 12 | #include // Access smart pointers 13 | 14 | class Object{ 15 | public: 16 | Object() { std::cout << "Constructor\n"; } 17 | ~Object() { std::cout << "Destructor\n"; } 18 | }; 19 | 20 | 21 | // Entry point to program 'main' weak 22 | int main(int argc, char* argv[]){ 23 | 24 | { 25 | // weak_ptr is almost like a 'temporary' pointer that we just 26 | // want to be able to point to something if it exists. 27 | std::weak_ptr myWeakPtr; 28 | { 29 | // Make a second pointer 30 | std::shared_ptr mySharedObjectPtr2 = std::make_shared(); 31 | // Assign our weak pointer to a shared pointer, but 32 | // we do not increase the reference count on sharedObjectPtr2. 33 | myWeakPtr = mySharedObjectPtr2; 34 | } 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /17/weak_pointers.cpp: -------------------------------------------------------------------------------- 1 | // @file weak_pointers.cpp 2 | #include 3 | #include 4 | 5 | // Here we're creating a global weak pointer(gw) 6 | std::weak_ptr gw; 7 | 8 | void f(){ 9 | 10 | // Pointing a new shared pointer, to whatever 11 | // our weak_ptr (gw) holds. 12 | auto spt = gw.lock(); 13 | 14 | // If the shared pointer we created, which now 15 | // also points to the same thingg as our weak_ptr, 16 | // then we can proceed. 17 | // However, if the thing our weak_ptr, pointed to 18 | // no longer exists (i.e "sp" below"), thten 19 | if (spt != nullptr) { // Has to be copied into a shared_ptr before usage 20 | std::cout << *spt << "\n"; 21 | } 22 | else { 23 | std::cout << "gw is expired\n"; 24 | } 25 | } 26 | 27 | int main(){ 28 | { // Create a scope within main 29 | int x = 5; 30 | int* p = &x; 31 | std::shared_ptr sp = std::make_shared(42); 32 | gw = sp; // Assigning weak_ptr to the shared_ptr 33 | f(); 34 | } // Exit the scope at line 23, thus deleting all local variables 35 | // between lines 23 and 29 36 | 37 | f(); 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /18/Readme.md: -------------------------------------------------------------------------------- 1 | Code 2 | -------------------------------------------------------------------------------- /18/average.cpp: -------------------------------------------------------------------------------- 1 | // average.cpp 2 | // g++ -std=c++20 average.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {-3,-2,-1,1,2,3}; 9 | 10 | int sum= 0; 11 | int numberOfElements= 0; 12 | for(const int& element : collection){ 13 | // Sum all of the positive elements 14 | // And put them in a new list 15 | if(element > 0){ 16 | sum+= element; 17 | numberOfElements+=1; 18 | } 19 | } 20 | 21 | std::cout << "Average of Positive Values: " 22 | << (float)sum/(float)numberOfElements 23 | << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /18/average2.cpp: -------------------------------------------------------------------------------- 1 | // average2.cpp 2 | // g++ -std=c++20 average2.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {-3,-2,-1,1,2,3}; 9 | std::vector result_collection; 10 | 11 | int sum= 0; 12 | 13 | for(const int& element : collection){ 14 | // Sum all of the positive elements 15 | // And put them in a new list 16 | if(element > 0){ 17 | sum+= element; 18 | 19 | result_collection.push_back(element); 20 | } 21 | } 22 | 23 | std::cout << "Average of Positive Values: " 24 | << (float)sum/(float)result_collection.size() 25 | << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | 31 | -------------------------------------------------------------------------------- /18/average_algorithm.cpp: -------------------------------------------------------------------------------- 1 | // average_algorithm.cpp 2 | // g++ -std=c++20 average_algorithm.cpp -o prog 3 | #include 4 | #include 5 | #include // NEW LIBRARY (for copy_if) 6 | #include // NEW LIBRARY (for accumulate) 7 | 8 | int main(){ 9 | 10 | std::vector collection {-1,1,-2,2,-3,3,-4,4,-5,5}; 11 | 12 | std::vector result_collection; 13 | std::copy_if(collection.begin(), collection.end(), 14 | std::back_inserter(result_collection), 15 | [](int n){ 16 | return n > 0; 17 | }); 18 | 19 | std::sort(result_collection.begin(),result_collection.end()); 20 | 21 | int sum = std::accumulate(end(result_collection)-3, 22 | end(result_collection),0); 23 | 24 | std::cout << "Average of Positive Values: " 25 | << (float)sum/3.0f 26 | << std::endl; 27 | 28 | return 0; 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /18/average_algorithm_is_sorted.cpp: -------------------------------------------------------------------------------- 1 | // average_algorithm_is_sorted.cpp 2 | // g++ -std=c++20 average_algorithm_is_sorted.cpp -o prog 3 | #include 4 | #include 5 | #include // NEW LIBRARY (for copy_if) 6 | #include // NEW LIBRARY (for accumulate) 7 | 8 | int main(){ 9 | 10 | std::vector collection {-1,1,-2,2,-3,3,-4,4,-5,5}; 11 | 12 | std::vector result_collection; 13 | std::copy_if(collection.begin(), collection.end(), 14 | std::back_inserter(result_collection), 15 | [](int n){ 16 | return n > 0; 17 | }); 18 | 19 | if(!std::is_sorted(result_collection.begin(), 20 | result_collection.end())) 21 | { 22 | std::sort(result_collection.begin(), 23 | result_collection.end()); 24 | } 25 | 26 | int sum = std::accumulate(end(result_collection)-3, 27 | end(result_collection),0); 28 | 29 | std::cout << "Average of Positive Values: " 30 | << (float)sum/3.0f 31 | << std::endl; 32 | 33 | return 0; 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /18/average_algorithm_partition.cpp: -------------------------------------------------------------------------------- 1 | // average_algorithm_partition.cpp 2 | // g++ -std=c++20 average_algorithm_partition.cpp -o prog 3 | #include 4 | #include 5 | #include // NEW LIBRARY (for copy_if) 6 | #include // NEW LIBRARY (for accumulate) 7 | 8 | int main(){ 9 | 10 | std::vector collection {-1,1,-2,2,-3,3,-4,4,-5,5}; 11 | 12 | auto secondGroupIterator = std::partition(collection.begin(), 13 | collection.end(), 14 | [](int n){ return n < 0;}); 15 | 16 | if(!std::is_sorted(secondGroupIterator,collection.end())) 17 | std::sort(secondGroupIterator,collection.end()); 18 | 19 | int sum = std::accumulate(end(collection)-3,end(collection),0); 20 | 21 | std::cout << "Average of Positive Values: " 22 | << (float)sum/3.0f 23 | << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /18/iota.cpp: -------------------------------------------------------------------------------- 1 | // iota.cpp 2 | // g++ -std=c++2b iota.cpp -o prog 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(){ 10 | 11 | std::vector collection(10); 12 | // Fill in range with successive elements 13 | std::iota(collection.begin(), collection.end(),-5); 14 | // Shuffle the range 15 | std::random_device randomDevice; 16 | std::mt19937 randGenerator{randomDevice()}; 17 | 18 | std::ranges::shuffle(collection, randGenerator); 19 | 20 | for(const int& element : collection){ 21 | std::cout << element << std::endl; 22 | } 23 | 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /18/iterator.cpp: -------------------------------------------------------------------------------- 1 | // iterator.cpp 2 | // g++ -std=c++20 iterator.cpp -o prog 3 | #include 4 | #include 5 | 6 | 7 | int main(){ 8 | 9 | std::vector collection {1,2,3}; 10 | collection.push_back(4); 11 | 12 | for(std::vector::iterator it = collection.begin(); 13 | it != collection.end(); 14 | ++it){ 15 | std::cout << *it << std::endl; 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /18/median.cpp: -------------------------------------------------------------------------------- 1 | // median.cpp 2 | // g++ -std=c++20 median.cpp -o prog 3 | #include 4 | #include 5 | #include // NEW LIBRARY (for copy_if) 6 | #include // NEW LIBRARY (for accumulate) 7 | 8 | int main(){ 9 | 10 | std::vector collection {-1,1,-2,2,-3,4,-4,4,-5,5}; 11 | 12 | auto median = collection.begin() + collection.size()/2; 13 | std::nth_element(collection.begin(),median,collection.end()); 14 | std::nth_element(collection.begin(),median+1,collection.end()); 15 | std::nth_element(collection.begin(),median-1,collection.end()); 16 | 17 | float sum = collection[collection.size()/2] 18 | + collection[collection.size()/2 - 1] 19 | + collection[collection.size()/2 + 1]; 20 | 21 | std::cout << "Median of 3 Positive Values: " 22 | << sum/3.0f 23 | << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /18/par.cpp: -------------------------------------------------------------------------------- 1 | // par.cpp 2 | // g++ -std=c++20 par.cpp -o prog 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(){ 10 | 11 | std::vector collection(100); 12 | // Populate 13 | std::iota(collection.begin(),collection.end(),0); 14 | // Parallel operaition 15 | std::transform(std::execution::par,collection.begin(),collection.end(),[](int n){ return n*n;}); 16 | 17 | 18 | return 0; 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /18/rangedfor.cpp: -------------------------------------------------------------------------------- 1 | // rangedfor.cpp 2 | // g++ -std=c++20 rangedfor.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {1,2,3}; 9 | collection.push_back(4); 10 | 11 | for(const int& element : collection){ 12 | std::cout << element << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /18/vector.cpp: -------------------------------------------------------------------------------- 1 | // vector.cpp 2 | // g++ -std=c++20 vector.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {1,2,3}; 9 | collection.push_back(4); 10 | 11 | for(int i=0; i < collection.size(); i++){ 12 | std::cout << collection[i] << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /18/vector2.cpp: -------------------------------------------------------------------------------- 1 | // vector2.cpp 2 | // g++ -std=c++20 vector2.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {1,2,3}; 9 | collection.push_back(4); 10 | 11 | for(unsigned int i=0; i < collection.size(); ++i){ 12 | std::cout << collection[i] << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /18/vector3.cpp: -------------------------------------------------------------------------------- 1 | // vector5.cpp 2 | // g++ -std=c++20 vector5.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {1,2,3}; 9 | collection.push_back(4); 10 | 11 | for(auto element ; collection){ 12 | std::cout << element << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /18/vector4.cpp: -------------------------------------------------------------------------------- 1 | // vector4.cpp 2 | // g++ -std=c++20 vector4.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {1,2,3}; 9 | collection.push_back(4); 10 | 11 | for(size_t i=0; i < collection.size(); ++i){ 12 | std::cout << collection[i] << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /18/vector5.cpp: -------------------------------------------------------------------------------- 1 | // vector5.cpp 2 | // g++ -std=c++20 vector5.cpp -o prog 3 | #include 4 | #include 5 | 6 | int main(){ 7 | 8 | std::vector collection {1,2,3}; 9 | collection.push_back(4); 10 | 11 | for(size_t i=0; i < collection.size(); ++i){ 12 | std::cout << collection.at(i) << std::endl; 13 | } 14 | 15 | return 0; 16 | } 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /19/atomics.cpp: -------------------------------------------------------------------------------- 1 | // @file atomics.cpp 2 | // g++ -std=c++17 atomics.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | #include // New library 7 | 8 | // Some shared value 9 | static std::atomic shared_value=0; 10 | 11 | void increment_shared_value(){ 12 | shared_value ++; 13 | } 14 | 15 | int main() { 16 | std::vector threads; 17 | // Create a collection of threads 18 | for(int i=0; i < 1000; i++){ 19 | threads.push_back(std::thread(increment_shared_value)); 20 | } 21 | // Join our threads using a ranged-based loop 22 | // (Our intent is to iterate through all threads in container) 23 | for(auto& th: threads){ 24 | threads.join(); 25 | } 26 | // Retrieve our result 27 | std::cout << "Result = " << shared_value << std::endl; 28 | 29 | return 0; 30 | } 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /19/thread1.cpp: -------------------------------------------------------------------------------- 1 | // @file thread1.cpp 2 | // g++ -std=c++17 thread1.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | 6 | // Test function which we'll launch threads from 7 | void test(int x) { 8 | std::cout << "Hello from our thread!" << std::endl; 9 | std::cout << "Argument passed in:" << x << std::endl; 10 | } 11 | 12 | int main() { 13 | // Create a new thread and pass one parameter 14 | std::thread myThread(&test, 100); 15 | // Join with the main thread, which is the same as 16 | // saying "hey, main thread--wait until myThread 17 | // finishes before executing further." 18 | myThread.join(); 19 | 20 | // Continue executing the main thread 21 | std::cout << "Hello from the main thread!" << std::endl; 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /19/thread10.cpp: -------------------------------------------------------------------------------- 1 | // @file thread10.cpp 2 | // g++ -std=c++17 thread10.cpp -o prog -lpthread 3 | #include 4 | #include 5 | 6 | int square(int x){ 7 | return x*x; 8 | } 9 | 10 | int main() { 11 | 12 | // asyncFunction is a 'future' 13 | // type = std::future 14 | auto asyncFunction = std::async(&square,12); 15 | // .... some time passes 16 | int result = asyncFunction.get(); // We are blocked here if 17 | // our value has not been 18 | // computed. Otherwise, the 19 | // value from get() (which 20 | // is wrapped in a promise 21 | // is returned. 22 | std::cout << "The async thread has returned! " << result 23 | << std:: endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /19/thread2.cpp: -------------------------------------------------------------------------------- 1 | // @file thread2.cpp 2 | // g++ -std=c++17 thread2.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | 6 | int main() { 7 | 8 | // This time create a lambda function 9 | auto lambda = [](int x){ 10 | std::cout << "Hello from our thread!" << std::endl; 11 | std::cout << "Argument passed in:" << x << std::endl; 12 | }; 13 | 14 | // Create a new thread with our lambda this time 15 | std::thread myThread(lambda,100); 16 | // Join with the main thread, which is the same as 17 | // saying "hey, main thread--wait until myThread 18 | // finishes before executing further." 19 | myThread.join(); 20 | 21 | // Continue executing the main thread 22 | std::cout << "Hello from the main thread!" << std::endl; 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /19/thread3.cpp: -------------------------------------------------------------------------------- 1 | // @file thread3.cpp 2 | // g++ -std=c++17 thread3.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | 7 | int main() { 8 | 9 | // This time create a lambda function 10 | auto lambda = [](int x){ 11 | std::cout << "thread.get_id:" << std::this_thread::get_id() << std::endl; 12 | std::cout << "Argument passed in:" << x << std::endl; 13 | }; 14 | 15 | std::vector threads; 16 | // Create a collection of threads 17 | for(int i=0; i < 10; i++){ 18 | threads.push_back(std::thread(lambda,i)); 19 | threads[i].join(); 20 | } 21 | 22 | // Continue executing the main thread 23 | std::cout << "Hello from the main thread!" << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /19/thread3_fix.cpp: -------------------------------------------------------------------------------- 1 | // @file thread3_fix.cpp 2 | // g++ -std=c++17 thread3_fix.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | 7 | int main() { 8 | 9 | // This time create a lambda function 10 | auto lambda = [](int x){ 11 | std::cout << "thread.get_id:" << std::this_thread::get_id() << std::endl; 12 | std::cout << "Argument passed in:" << x << std::endl; 13 | }; 14 | 15 | std::vector threads; 16 | // Create a collection of threads 17 | for(int i=0; i < 10; i++){ 18 | threads.push_back(std::thread(lambda,i)); 19 | } 20 | // Join all of our threads here-- 21 | // one or more may have launched, but we'll have 22 | // to wait in main until ALL threads finish. 23 | for(int i=0; i < 10; i++){ 24 | threads[i].join(); 25 | } 26 | 27 | // Continue executing the main thread 28 | std::cout << "Hello from the main thread!" << std::endl; 29 | 30 | return 0; 31 | } 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /19/thread4.cpp: -------------------------------------------------------------------------------- 1 | // @file thread4.cpp 2 | // g++-10 -std=c++20 thread4.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | 7 | int main() { 8 | 9 | // This time create a lambda function 10 | auto lambda = [](int x){ 11 | std::cout << "thread.get_id:" << std::this_thread::get_id() << std::endl; 12 | std::cout << "Argument passed in:" << x << std::endl; 13 | }; 14 | 15 | // Note: We now have a jthread 16 | // No joins in the program 17 | std::vector threads; 18 | // Create a collection of threads 19 | for(int i=0; i < 10; i++){ 20 | threads.push_back(std::jthread(lambda,i)); 21 | } 22 | 23 | // Continue executing the main thread 24 | std::cout << "Hello from the main thread!" << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /19/thread5.cpp: -------------------------------------------------------------------------------- 1 | // @file thread5.cpp 2 | // g++ -std=c++17 thread5.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | 7 | // Some shared value 8 | // A little ugly that it's a static global but it's a toy example. 9 | static int shared_value = 0; 10 | 11 | void increment_shared_value(){ 12 | shared_value = shared_value + 1; 13 | } 14 | 15 | int main() { 16 | std::vector threads; 17 | // Create a collection of threads 18 | for(int i=0; i < 1000; i++){ 19 | threads.push_back(std::thread(increment_shared_value)); 20 | } 21 | // Join our threads 22 | for(int i=0; i < 1000; i++){ 23 | threads[i].join(); 24 | } 25 | // Retrieve our result 26 | std::cout << "Result = " << shared_value << std::endl; 27 | 28 | return 0; 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /19/thread6.cpp: -------------------------------------------------------------------------------- 1 | // @file thread6.cpp 2 | // g++ -std=c++17 thread6.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | # include // New library 7 | 8 | // Some shared value 9 | static int shared_value = 0; 10 | std::mutex gLock; // A global lock 11 | 12 | void increment_shared_value(){ 13 | gLock.lock(); 14 | shared_value = shared_value + 1; 15 | gLock.unlock(); 16 | } 17 | 18 | int main() { 19 | std::vector threads; 20 | // Create a collection of threads 21 | for(int i=0; i < 1000; i++){ 22 | threads.push_back(std::thread(increment_shared_value)); 23 | } 24 | // Join our threads 25 | for(int i=0; i < 1000; i++){ 26 | threads[i].join(); 27 | } 28 | // Retrieve our result 29 | std::cout << "Result = " << shared_value << std::endl; 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /19/thread7_deadlock.cpp: -------------------------------------------------------------------------------- 1 | // @file thread7_deadlock.cpp 2 | // g++ -std=c++17 thread7_deadlock.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | #include // New library 7 | 8 | // Some shared value 9 | static int shared_value = 0; 10 | std::mutex gLock; // A global lock 11 | 12 | void increment_shared_value(){ 13 | gLock.lock(); 14 | shared_value = shared_value + 1; 15 | // gLock.unlock(); // Oops, never return lock 16 | } 17 | 18 | int main() { 19 | std::vector threads; 20 | // Create a collection of threads 21 | for(int i=0; i < 1000; i++){ 22 | threads.push_back(std::thread(increment_shared_value)); 23 | } 24 | // Join our threads 25 | for(int i=0; i < 1000; i++){ 26 | threads[i].join(); 27 | } 28 | // Retrieve our result 29 | std::cout << "Result = " << shared_value << std::endl; 30 | 31 | return 0; 32 | } 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /19/thread8_exception.cpp: -------------------------------------------------------------------------------- 1 | // @file thread8_exception.cpp 2 | // g++ -std=c++17 thread8_exception.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | #include // New library 7 | 8 | // Some shared value 9 | static int shared_value = 0; 10 | std::mutex gLock; // A global lock 11 | 12 | void increment_shared_value(){ 13 | gLock.lock(); 14 | try{ 15 | shared_value = shared_value + 1; 16 | throw "Dangerous exception abort"; 17 | }catch(...){ 18 | std::cout << "handle exception by returning from thread\n"; 19 | return; 20 | } 21 | gLock.unlock(); // Oops, never return lock 22 | } 23 | 24 | int main() { 25 | std::vector threads; 26 | // Create a collection of threads 27 | for(int i=0; i < 1000; i++){ 28 | threads.push_back(std::thread(increment_shared_value)); 29 | } 30 | // Join our threads 31 | for(int i=0; i < 1000; i++){ 32 | threads[i].join(); 33 | } 34 | // Retrieve our result 35 | std::cout << "Result = " << shared_value << std::endl; 36 | 37 | return 0; 38 | } 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /19/thread9.cpp: -------------------------------------------------------------------------------- 1 | // @file thread9.cpp 2 | // g++ -std=c++17 thread9.cpp -o prog -lpthread 3 | #include 4 | #include // Include the thread library 5 | #include 6 | #include // New library 7 | 8 | // Some shared value 9 | static int shared_value = 0; 10 | std::mutex gLock; // A global lock 11 | 12 | void increment_shared_value(){ 13 | // lock_guard follows RAII principles and will 14 | // release lock after leaving scope. 15 | // This includes if an exception is thrown. 16 | std::lock_guard lockGuard(gLock); 17 | try{ 18 | shared_value = shared_value + 1; 19 | throw "Dangerous exception abort"; 20 | }catch(...){ 21 | std::cout << "handle exception by returning from thread\n"; 22 | return; 23 | } 24 | } 25 | 26 | int main() { 27 | std::vector threads; 28 | // Create a collection of threads 29 | for(int i=0; i < 1000; i++){ 30 | threads.push_back(std::thread(increment_shared_value)); 31 | } 32 | // Join our threads 33 | for(int i=0; i < 1000; i++){ 34 | threads[i].join(); 35 | } 36 | // Retrieve our result 37 | std::cout << "Result = " << shared_value << std::endl; 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /2/.std_array3.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/2/.std_array3.cpp.swp -------------------------------------------------------------------------------- /2/raw_array.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 raw_array.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g raw_array.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file raw_array.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Entry point to program 'main' function 17 | int main(int argc, char* argv[]){ 18 | 19 | int raw_array[9]; 20 | raw_array[0] = 7; 21 | raw_array[1] = 9; 22 | raw_array[2] = 12; 23 | raw_array[3] = 16; 24 | raw_array[4] = 15; 25 | raw_array[5] = 17; 26 | raw_array[6] = 24; 27 | raw_array[7] = -5; 28 | raw_array[8] = -14; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /2/std_array3.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 std_array3.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g std_array3.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file std_array3.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include // Include the array library 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | 20 | // The first parameter is the type 21 | // The second parameter is the 'fixed size' of 22 | // the array. i.e. how many elements we can store. 23 | std::array myArray; 24 | 25 | // We can access an array wit the index operator, 26 | // that is the []'s 27 | myArray[0] = 7; 28 | // We can use member functions in our array as well 29 | // with the .at(position) 30 | myArray.at(0) = 9; 31 | 32 | // Some other member functions. 33 | // Let's test what they return in GDB! 34 | myArray.front(); 35 | myArray.back(); 36 | myArray.size(); 37 | myArray.max_size(); 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /2/std_map.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 std_map.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g std_map.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file std_map.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include // Include the map library 16 | #include // String data type 17 | 18 | // Entry point to program 'main' function 19 | int main(int argc, char* argv[]){ 20 | 21 | // Key and value pair 22 | // - A string will be the key 23 | // - A string will then be the value 24 | std::map dictionary; 25 | 26 | // Create a key/value pair 27 | std::pair cat("cat","a funny animal"); 28 | 29 | // Add elements to our map 30 | dictionary.insert(cat); 31 | 32 | // Lookup a value by the key 33 | std::cout << dictionary["cat"] << std::endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /2/std_queue.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 std_queue.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g std_queue.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file std_queue.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include // Include the queue library 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | 20 | // The first parameter is the type 21 | std::queue myQueue; 22 | 23 | // Add elements to our queue 24 | myQueue.push('a'); 25 | myQueue.push('b'); 26 | myQueue.push('c'); 27 | // peek at the first element 28 | std::cout << "front:" << myQueue.front() << std::endl; 29 | // remove the first element 30 | myQueue.pop(); 31 | // peek at the first element 32 | std::cout << "front:" << myQueue.front() << std::endl; 33 | // Report on the size 34 | std::cout << "size:" << myQueue.size() << std::endl; 35 | 36 | 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /2/std_set.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 std_set.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g std_set.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file std_set.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include // Include the set library 16 | #include // String data type 17 | 18 | // Entry point to program 'main' function 19 | int main(int argc, char* argv[]){ 20 | 21 | // A single key 22 | std::set mySet; 23 | 24 | // Add elements to our set 25 | mySet.insert("cat"); 26 | mySet.insert("cat"); 27 | mySet.insert("cat"); 28 | mySet.insert("cat"); 29 | mySet.insert("cat"); 30 | 31 | // Lookup a value by the key 32 | std::cout << "Unique items: " << mySet.size() << std::endl; 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /2/std_stack.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 std_stack.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g std_stack.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file std_stack.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include // Include the stack library 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | 20 | // The first parameter is the type 21 | std::stack myStack; 22 | 23 | // Add elements to our stack 24 | myStack.push('a'); 25 | myStack.push('b'); 26 | myStack.push('c'); 27 | // peek at the top element 28 | std::cout << "top:" << myStack.top() << std::endl; 29 | // remove the top element 30 | myStack.pop(); 31 | // peek at the top element 32 | std::cout << "top:" << myStack.top() << std::endl; 33 | // Report on the size 34 | std::cout << "size:" << myStack.size() << std::endl; 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /2/std_unordered_map.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 std_unordered_map.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g std_unordered_map.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file std_unordered_map.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include // Include the unordered_map library 16 | #include // String data type 17 | 18 | // Entry point to program 'main' function 19 | int main(int argc, char* argv[]){ 20 | 21 | // Key and value pair 22 | // - A string will be the key 23 | // - A string will then be the value 24 | std::unordered_map dictionary; 25 | 26 | // Create a key/value pair 27 | std::pair cat("cat","a funny animal"); 28 | 29 | // Add elements to our unordered_map 30 | dictionary.insert(cat); 31 | 32 | // Lookup a value by the key 33 | std::cout << dictionary["cat"] << std::endl; 34 | 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /2/std_vector.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 std_vector.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g std_vector.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file std_vector.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | #include // Include the vector library 16 | 17 | // Entry point to program 'main' function 18 | int main(int argc, char* argv[]){ 19 | 20 | // The first parameter is the type 21 | std::vector myVector; 22 | 23 | // Add elements to our vector 24 | myVector.push_back(1); 25 | myVector.push_back(2); 26 | myVector.push_back(3); 27 | // Access element at a specific position 28 | myVector.at(2); 29 | // Remove the last element 30 | myVector.pop_back(); 31 | // Print everything in the vector 32 | for(int i=0; i < myVector.size(); i++){ 33 | // Use the [ ] operator to access an element 34 | // at the i'th position in the vector 35 | std::cout << myVector[i] << std::endl; 36 | } 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /22/AdjacencyList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ADJACENCY_LIST_HPP 2 | #define ADJACENCY_LIST_HPP 3 | 4 | #include 5 | #include 6 | //#include 7 | 8 | struct node_t{ 9 | std::string name; 10 | std::vector neighbors; 11 | }; 12 | 13 | class AdjacencyList{ 14 | public: 15 | AdjacencyList(); 16 | ~AdjacencyList(); 17 | 18 | // Add a node new 19 | void AddNode(std::string name); 20 | void AddDirectedEdge(std::string source, std::string destination); 21 | 22 | void print(); 23 | private: 24 | std::vector m_nodes; 25 | }; 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /22/adjacency_list_unordered_map/main.cpp: -------------------------------------------------------------------------------- 1 | // g++ -std=c++11 main.cpp -o prog 2 | // Note: This example needs at least C++ 11 because of unordered_map 3 | #include 4 | #include 5 | #include 6 | 7 | typedef std::unordered_map > adjList_t; 8 | 9 | // @file adjacency_list_unordered_map/main.cpp 10 | void AddEdge(std::unordered_map >& graph, 11 | int src, 12 | int dst){ 13 | 14 | graph[src].push_back(dst); 15 | } 16 | 17 | void PrintAdjaceneyList(const std::unordered_map >& adj){ 18 | for(const auto key: adj){ 19 | // Note: An unordered_map is a key/value pair, so when we 20 | // iterate we get key/value pairs. 21 | // Thus, we have to access the first element as the 'key' 22 | std::cout << key.first << "-->"; 23 | for(auto value: key.second){ 24 | std::cout << value << " "; 25 | } 26 | std::cout << std::endl; 27 | } 28 | } 29 | 30 | // @file adjacency_list_unordered_map/main.cpp 31 | int main(){ 32 | 33 | adjList_t adj; 34 | 35 | AddEdge(adj,1,2); 36 | AddEdge(adj,1,3); 37 | AddEdge(adj,1,4); 38 | AddEdge(adj,2,1); 39 | AddEdge(adj,2,4); 40 | 41 | PrintAdjaceneyList(adj); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /22/main.cpp: -------------------------------------------------------------------------------- 1 | // Compile with: 2 | // g++ -std=c++17 -g main.cpp AdjacencyList.cpp -o prog 3 | 4 | #include "AdjacencyList.hpp" 5 | 6 | bool unitTest1(){ 7 | AdjacencyList list; 8 | list.AddNode("A"); 9 | list.AddNode("B"); 10 | list.AddNode("C"); 11 | 12 | list.AddDirectedEdge("A","B"); 13 | list.AddDirectedEdge("A","C"); 14 | list.AddDirectedEdge("B","C"); 15 | 16 | list.print(); 17 | 18 | return true; 19 | } 20 | 21 | 22 | int main(){ 23 | 24 | unitTest1(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /24/lvalue.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 lvalue.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g lvalue.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file lvalue.cpp 11 | #include 12 | #include // std::lvalue 13 | #include 14 | 15 | int foo(){ 16 | return 42; 17 | } 18 | 19 | // Entry point to program 'main' lvalue.cpp 20 | int main(){ 21 | 22 | foo() = 77; 23 | 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /24/lvalue_working.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 lvalue_working.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g lvalue_working.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file lvalue_working.cpp 11 | #include 12 | #include 13 | #include 14 | 15 | int x = 42; 16 | int& foo(){ 17 | return x; 18 | } 19 | 20 | // Entry point to program 'main' lvalue_working.cpp 21 | int main(){ 22 | 23 | foo() = 77; 24 | 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /24/rvalue_move.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 rvalue_move.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g rvalue_move.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file rvalue_move.cpp 11 | #include 12 | #include // std::move 13 | #include 14 | 15 | // Entry point to program 'main' rvalue_move.cpp 16 | int main(){ 17 | 18 | std::string value = "some really long string"; 19 | std::string& alias = value; 20 | 21 | // I'm going to 'steal' the resource during the 22 | // construction of the newString object. 23 | std::string newString{std::move(alias)}; 24 | 25 | // Prints out empty 26 | std::cout << "value :" << value << std::endl; 27 | std::cout << "alis :" << alias << std::endl; 28 | // Observe that ownership is transferred to newString 29 | std::cout << "newString:" << newString << std::endl; 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /24/simple_move.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 simple_move.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g simple_move.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file simple_move.cpp 11 | #include 12 | #include // std::move 13 | #include 14 | 15 | // Entry point to program 'main' simple_move.cpp 16 | int main(){ 17 | 18 | std::string value = "some really long string"; 19 | // I'm going to 'steal' the resource during the 20 | // construction of the newString object. 21 | std::string newString{std::move(value)}; 22 | 23 | // Prints out empty 24 | std::cout << "value :" << value << std::endl; 25 | // Observe that ownership is transferred to newString 26 | std::cout << "newString:" << newString << std::endl; 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /25/example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int add(int i, int j){ 4 | 5 | return i+j; 6 | } 7 | 8 | int mult(int i, int j){ 9 | 10 | return i*j; 11 | } 12 | 13 | PYBIND11_MODULE(example, m){ 14 | m.doc() = "3520 example"; 15 | m.def("add",&add,"function that adds two ints"); 16 | m.def("mult",&mult,"function that multiplies two ints"); 17 | 18 | } 19 | -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/README: -------------------------------------------------------------------------------- 1 | 2 | This is Lua 5.4.4, released on 13 Jan 2022. 3 | 4 | For installation instructions, license details, and 5 | further information about Lua, see doc/readme.html. 6 | 7 | -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/doc/index.css: -------------------------------------------------------------------------------- 1 | ul { 2 | list-style-type: none ; 3 | } 4 | 5 | ul.contents { 6 | padding: 0 ; 7 | } 8 | 9 | table { 10 | border: none ; 11 | border-spacing: 0 ; 12 | border-collapse: collapse ; 13 | } 14 | 15 | td { 16 | vertical-align: top ; 17 | padding: 0 ; 18 | text-align: left ; 19 | line-height: 1.25 ; 20 | width: 15% ; 21 | } 22 | -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/doc/logo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/doc/logo.gif -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/doc/manual.css: -------------------------------------------------------------------------------- 1 | h3 code { 2 | font-family: inherit ; 3 | font-size: inherit ; 4 | } 5 | 6 | pre, code { 7 | font-size: 12pt ; 8 | } 9 | 10 | span.apii { 11 | color: gray ; 12 | float: right ; 13 | font-family: inherit ; 14 | font-style: normal ; 15 | font-size: small ; 16 | } 17 | 18 | h2:before { 19 | content: "" ; 20 | padding-right: 0em ; 21 | } 22 | -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/doc/osi-certified-72x60.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/doc/osi-certified-72x60.png -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lapi.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lapi.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lauxlib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lauxlib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lbaselib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lbaselib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lcode.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lcode.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lcorolib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lcorolib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lctype.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lctype.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/ldblib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/ldblib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/ldebug.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/ldebug.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/ldo.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/ldo.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/ldump.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/ldump.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lfunc.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lfunc.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lgc.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lgc.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/liblua.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/liblua.a -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/linit.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/linit.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/liolib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/liolib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/llex.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/llex.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lmathlib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lmathlib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lmem.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lmem.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/loadlib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/loadlib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lobject.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lobject.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lopcodes.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lopcodes.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/loslib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/loslib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lparser.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lparser.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lprefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lprefix.h $ 3 | ** Definitions for Lua code that must come before any other header file 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lprefix_h 8 | #define lprefix_h 9 | 10 | 11 | /* 12 | ** Allows POSIX/XSI stuff 13 | */ 14 | #if !defined(LUA_USE_C89) /* { */ 15 | 16 | #if !defined(_XOPEN_SOURCE) 17 | #define _XOPEN_SOURCE 600 18 | #elif _XOPEN_SOURCE == 0 19 | #undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */ 20 | #endif 21 | 22 | /* 23 | ** Allows manipulation of large files in gcc and some other compilers 24 | */ 25 | #if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS) 26 | #define _LARGEFILE_SOURCE 1 27 | #define _FILE_OFFSET_BITS 64 28 | #endif 29 | 30 | #endif /* } */ 31 | 32 | 33 | /* 34 | ** Windows stuff 35 | */ 36 | #if defined(_WIN32) /* { */ 37 | 38 | #if !defined(_CRT_SECURE_NO_WARNINGS) 39 | #define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */ 40 | #endif 41 | 42 | #endif /* } */ 43 | 44 | #endif 45 | 46 | -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lstate.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lstate.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lstring.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lstring.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lstrlib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lstrlib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/ltable.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/ltable.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/ltablib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/ltablib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/ltm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/ltm.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lua: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lua -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lua.hpp: -------------------------------------------------------------------------------- 1 | // lua.hpp 2 | // Lua header files for C++ 3 | // <> not supplied automatically because Lua also compiles as C++ 4 | 5 | extern "C" { 6 | #include "lua.h" 7 | #include "lualib.h" 8 | #include "lauxlib.h" 9 | } 10 | -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lua.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lua.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/luac: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/luac -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/luac.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/luac.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lundump.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lundump.h $ 3 | ** load precompiled Lua chunks 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lundump_h 8 | #define lundump_h 9 | 10 | #include "llimits.h" 11 | #include "lobject.h" 12 | #include "lzio.h" 13 | 14 | 15 | /* data to catch conversion errors */ 16 | #define LUAC_DATA "\x19\x93\r\n\x1a\n" 17 | 18 | #define LUAC_INT 0x5678 19 | #define LUAC_NUM cast_num(370.5) 20 | 21 | /* 22 | ** Encode major-minor version in one byte, one nibble for each 23 | */ 24 | #define MYINT(s) (s[0]-'0') /* assume one-digit numerals */ 25 | #define LUAC_VERSION (MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)) 26 | 27 | #define LUAC_FORMAT 0 /* this is the official format */ 28 | 29 | /* load one chunk; from lundump.c */ 30 | LUAI_FUNC LClosure* luaU_undump (lua_State* L, ZIO* Z, const char* name); 31 | 32 | /* dump one chunk; from ldump.c */ 33 | LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, 34 | void* data, int strip); 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lundump.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lundump.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lutf8lib.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lutf8lib.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lvm.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lvm.o -------------------------------------------------------------------------------- /25/lua/lua-5.4.4/src/lzio.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/25/lua/lua-5.4.4/src/lzio.o -------------------------------------------------------------------------------- /25/lua/lua03.lua: -------------------------------------------------------------------------------- 1 | function Return123() 2 | return 123; 3 | end 4 | -------------------------------------------------------------------------------- /25/lua/lua04.lua: -------------------------------------------------------------------------------- 1 | function add(a, b) 2 | return a+b 3 | end 4 | -------------------------------------------------------------------------------- /25/lua/lua05.lua: -------------------------------------------------------------------------------- 1 | AddCpp(500,200) 2 | -------------------------------------------------------------------------------- /3/ampersand.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 ampersand.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g ampersand.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file ampersand.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Entry point to program 'main' ampersand 17 | int main(int argc, char* argv[]){ 18 | 19 | // X is a variable, and variables live 20 | // 'somewhere' in memory. 21 | int x = 9; 22 | // Let's print out where 23 | std::cout << "x's value is : " << x << std::endl; 24 | std::cout << "x's address is: " << &x << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /3/call_stack.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 call_stack.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g call_stack.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file call_stack.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | void foo(int x){ 17 | bar(); 18 | return; 19 | } 20 | 21 | 22 | // Entry point to program 'main' call_stack 23 | int main(int argc, char* argv[]){ 24 | 25 | square(7); 26 | square(7.0f); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /3/constexpr.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 constexpr.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g constexpr.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file constexpr.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Sometimes it's possible to compute a value 17 | // at 'compile-time' by qualifying the return 18 | // of a function as 'constexpr' 19 | // This idea of computing at compile-time is 20 | // really powerful, because then we don't have 21 | // to compute the value at run-time (i.e. when 22 | // the program is running!) thus making our programs 23 | // run faster! 24 | constexpr int square(int x){ 25 | return x * x; 26 | } 27 | 28 | // Entry point to program 'main' constexpr 29 | int main(int argc, char* argv[]){ 30 | 31 | std::cout << square(5) << std::endl; 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /3/function1.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 function1.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g function1.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file function1.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Name: myFunction 17 | // Return Value: None 18 | // Arguments: None 19 | void myFunction(){ 20 | std::cout << "No parameters, just do some work\n"; 21 | } 22 | 23 | // Entry point to program 'main' function1 24 | int main(int argc, char* argv[]){ 25 | 26 | // Make a functioon call 27 | myFunction(); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /3/function2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 function2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g function2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file function2.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | // Function declaration 17 | // This makes the function available, 18 | // and we must otherwise provide the definition 19 | // (i.e. the implementation) in our codebase, 20 | // either later in this file, another file, or 21 | // by linking in a library. 22 | void myFunction(); 23 | 24 | // Entry point to program 'main' function2 25 | int main(int argc, char* argv[]){ 26 | 27 | // Make a functioon call 28 | myFunction(); 29 | 30 | return 0; 31 | } 32 | 33 | // Function Defintion here 34 | 35 | // Name: myFunction 36 | // Return Value: None 37 | // Arguments: None 38 | void myFunction(){ 39 | std::cout << "No parameters, just do some work\n"; 40 | } 41 | -------------------------------------------------------------------------------- /3/function3.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 function3.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g function3.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file function3.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | void myFunction(); 17 | void myFunction2(); 18 | 19 | void myFunction(){ 20 | myFunction2(); 21 | } 22 | 23 | void myFunction2(){ 24 | std::cout << "Hello from myFunction2, which was called \ 25 | from myFunction. In order to do this, we \ 26 | had to forward declare myFunction2 at \ 27 | line 17.\n"; 28 | } 29 | 30 | // Entry point to program 'main' function2 31 | int main(int argc, char* argv[]){ 32 | 33 | // Make a functioon call 34 | myFunction(); 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /3/function4.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 function4.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g function4.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file function4.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | int add(int a, int b){ 17 | return a+b; 18 | } 19 | 20 | // Entry point to program 'main' function4 21 | int main(int argc, char* argv[]){ 22 | 23 | int result = add(7,2); 24 | std::cout << "Result of add(7,2) = " << result << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /3/function5.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 function5.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g function5.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file function5.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | int add(int a, int b){ 17 | return a+b; 18 | } 19 | 20 | // Entry point to program 'main' function5 21 | int main(int argc, char* argv[]){ 22 | 23 | std::cout << "Result of add(7,2) = " << add(7,2) << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /3/function6.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 function6.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g function6.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file function6.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | int factorial(int n){ 17 | if(n<=1){ 18 | return 1; 19 | }else{ 20 | return factorial(n-1)*n; 21 | } 22 | } 23 | 24 | // Entry point to program 'main' function6 25 | int main(int argc, char* argv[]){ 26 | 27 | std::cout << "factorial(0) = " << factorial(0) << std::endl; 28 | std::cout << "factorial(1) = " << factorial(1) << std::endl; 29 | std::cout << "factorial(2) = " << factorial(2) << std::endl; 30 | std::cout << "factorial(3) = " << factorial(3) << std::endl; 31 | std::cout << "factorial(4) = " << factorial(4) << std::endl; 32 | std::cout << "factorial(5) = " << factorial(5) << std::endl; 33 | std::cout << "factorial(6) = " << factorial(6) << std::endl; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /3/math/.main.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/3/math/.main.cpp.swp -------------------------------------------------------------------------------- /3/math/.math.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/3/math/.math.cpp.swp -------------------------------------------------------------------------------- /3/math/.math.hpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/3/math/.math.hpp.swp -------------------------------------------------------------------------------- /3/math/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "math.hpp" 4 | 5 | void doWork(){ 6 | std::cout << "add(7,2) = " << add(7,2) << std::endl; 7 | std::cout << "multiply(7,2) = " << multiply(7,2) << std::endl; 8 | } 9 | 10 | int main(){ 11 | 12 | doWork(); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /3/math/math.cpp: -------------------------------------------------------------------------------- 1 | // math.cpp 2 | 3 | #include "math.hpp" 4 | 5 | int add(int a, int b){ 6 | return a+b; 7 | } 8 | 9 | int multiply(int x, int y){ 10 | return x*y; 11 | } 12 | -------------------------------------------------------------------------------- /3/math/math.hpp: -------------------------------------------------------------------------------- 1 | // math.hpp 2 | #ifndef MATH_HPP 3 | #define MATH_HPP 4 | 5 | int add(int a, int b); 6 | int multiply(int x, int y); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /3/math/prog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/3/math/prog -------------------------------------------------------------------------------- /3/pass_by_const_reference.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pass_by_const_reference.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pass_by_const_reference.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pass_by_const_reference.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | void func(const int &x){ 17 | // Not allowed, x is 'const' 18 | x = 9999; 19 | } 20 | 21 | // Entry point to program 'main' pass_by_const_reference 22 | int main(int argc, char* argv[]){ 23 | 24 | int x = 9; 25 | // Passing in x by const reference, thus we are not making 26 | // a copy of 'x', and we are also not allowed to modify x 27 | func(x); 28 | std::cout << "x remains: " << x << std::endl; 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /3/pass_by_reference.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pass_by_reference.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pass_by_reference.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pass_by_reference.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | void func(int& x){ 17 | // The copy of x is what we're really modifying 18 | // Look the address is different! 19 | std::cout << "referencing x at same address: " << &x << std::endl; 20 | x = 9999; 21 | } 22 | 23 | // Entry point to program 'main' pass_by_reference 24 | int main(int argc, char* argv[]){ 25 | 26 | int x = 9; 27 | std::cout << "x address is: \t\t\t" << &x << std::endl; 28 | func(x); 29 | std::cout << "x is: " << x << std::endl; 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /3/polymorphism.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 polymorphism.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g polymorphism.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file polymorphism.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | int square(int x){ 17 | std::cout << "int square(int x) called" << std::endl; 18 | return x*x; 19 | } 20 | 21 | float square(float x){ 22 | std::cout << "float square(float x) called" << std::endl; 23 | return x*x; 24 | } 25 | 26 | // Entry point to program 'main' polymorphism 27 | int main(int argc, char* argv[]){ 28 | 29 | square(7); 30 | square(7.0f); 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /3/scope.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 scope.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g scope.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file scope.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | 17 | // Entry point to program 'main' scope 18 | int main(int argc, char* argv[]){ 19 | 20 | { // start of scope 21 | int x = 7; // x is defined 22 | } // x is undefined at end of scope 23 | 24 | { // start of a new scope 25 | int x = 5; // a 'new' x is defined 26 | } // x is once again undefined at end of scope 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /3/scope_error.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 scope_error.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g scope_error.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file scope_error.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | 17 | // Entry point to program 'main' scope_error 18 | int main(int argc, char* argv[]){ 19 | 20 | int x = 6; // x defined as int and set to 6 21 | int x = 7; // x is already defined error! 22 | 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /3/scope_function.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 scope_function.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g scope_function.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file scope_function.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | int square(int x){ 17 | return x * x; 18 | } 19 | 20 | // Entry point to program 'main' scope_function 21 | int main(int argc, char* argv[]){ 22 | 23 | int x = 6; // x defined as int and set to 6 24 | 25 | std::cout << square(5) << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /3/static.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 static.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g static.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file static.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | void foo(){ 17 | static int counter=0; 18 | counter = counter + 1; 19 | std::cout << "foo called: " << counter << " times\n"; 20 | } 21 | 22 | 23 | // Entry point to program 'main' static 24 | int main(int argc, char* argv[]){ 25 | 26 | foo(); 27 | foo(); 28 | foo(); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /3/value.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 value.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g value.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file value.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | void func(int x){ 17 | x = 9999; 18 | } 19 | 20 | // Entry point to program 'main' value 21 | int main(int argc, char* argv[]){ 22 | 23 | int x = 9; 24 | func(x); 25 | std::cout << "x is: " << x << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /3/value_address.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 value_address.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g value_address.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file value_address.cpp 11 | // Bring in a header file on our include path 12 | // this happens to be in the standard library 13 | // (i.e. default compiler path) 14 | #include 15 | 16 | void func(int x){ 17 | // The copy of x is what we're really modifying 18 | // Look the address is different! 19 | std::cout << "copy of x address is: \t" << &x << std::endl; 20 | x = 9999; 21 | } 22 | 23 | // Entry point to program 'main' value_address 24 | int main(int argc, char* argv[]){ 25 | 26 | int x = 9; 27 | std::cout << "x address is: \t\t" << &x << std::endl; 28 | func(x); 29 | std::cout << "x is: " << x << std::endl; 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /4/ascii.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 ascii.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g ascii.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file ascii.cpp 11 | #include // I/O stream library 12 | #include // std::ascii object 13 | 14 | // Entry point to program 'main' ascii 15 | int main(int argc, char* argv[]){ 16 | 17 | char charArray[] = "Mike Shah"; 18 | std::string name = "Mike Shah"; 19 | // Insert a breakpoint here 20 | // and examine 'charArray' and 21 | // 'name' in GDB 22 | std::cout << name << std::endl; 23 | // equivalent to charArray is... 24 | std::cout << name.c_str() << std::endl; 25 | 26 | std::cout << "First letter integer representation is: " 27 | << (int)name[0] << std::endl; 28 | 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /4/cin.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 cin.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g cin.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file cin.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | // Entry point to program 'main' cin 15 | int main(int argc, char* argv[]){ 16 | 17 | std::string age = ""; 18 | std::cout << "How old are you? (1-130) " << std::endl; 19 | // Note: That the >> is pointing towards the variable 20 | // You can think of the arrows as pointing towards 21 | // where you are writing and storing the value. 22 | std::cin >> age; 23 | std::cout << "Wow you are " << age << "!" << std::endl; 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /4/cout.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 cout.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g cout.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file cout.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' cout 14 | int main(int argc, char* argv[]){ 15 | 16 | std::cout << "Hello World" << std::endl; 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /4/filesystem.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 filesystem.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g filesystem.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file filesystem.cpp 11 | #include // I/O stream library 12 | #include // std::filesystem object 13 | #include // New in C++ 17 14 | 15 | // Entry point to program 'main' filesystem 16 | int main(int argc, char* argv[]){ 17 | 18 | std::cout << "Current path of program is: " 19 | << std::filesystem::current_path() 20 | << std::endl; 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /4/input.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 input.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g input.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file input.cpp 11 | #include // I/O stream library 12 | #include // std::string object 13 | #include // Filestream objects 14 | 15 | // Entry point to program 'main' input 16 | int main(int argc, char* argv[]){ 17 | 18 | std::string line; 19 | // (I)nput (F)ile stream is the way to read ifstream. 20 | std::ifstream myFile("text.txt"); 21 | if(myFile.is_open()){ 22 | // Read the file until the end 23 | while(std::getline(myFile,line)){ 24 | std::cout << line << std::endl; 25 | } 26 | // Close the file when we are done with it. 27 | myFile.close(); 28 | }else{ 29 | std::cout << "Unable to open file\n"; 30 | } 31 | 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /4/output.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 output.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g output.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file output.cpp 11 | #include // I/O stream library 12 | #include // std::string object 13 | #include // Filestream objects 14 | 15 | // Entry point to program 'main' output 16 | int main(int argc, char* argv[]){ 17 | 18 | // (O)utput (F)ile stream is the way to read ofstream. 19 | std::ofstream myFile("output.txt"); 20 | if(myFile.is_open()){ 21 | // We can use the same 'stream' operator, but this 22 | // time on our myFile to output data. 23 | myFile << "Line 1 of our file\n"; 24 | myFile << "Line 2 of our file\n"; 25 | myFile << "Line 3 of our file\n"; 26 | // Close the file when we are done with it. 27 | myFile.close(); 28 | }else{ 29 | std::cout << "Unable to create file\n"; 30 | } 31 | 32 | 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /4/output.txt: -------------------------------------------------------------------------------- 1 | Line 1 of our file 2 | Line 2 of our file 3 | Line 3 of our file 4 | -------------------------------------------------------------------------------- /4/scanf.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 scanf.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g scanf.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file scanf.cpp 11 | #include // I/O stream library 12 | #include // std::string object 13 | 14 | // Entry point to program 'main' scanf 15 | int main(int argc, char* argv[]){ 16 | 17 | int age; 18 | std::cout << "How old are you? "; 19 | std::scanf("%d?", &age); 20 | std::cout << "Wow you are " << age << "!" << std::endl; 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /4/sscanf.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 sscanf.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g sscanf.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file scanf.cpp 11 | #include // I/O stream library 12 | #include // std::string object 13 | 14 | // Entry point to program 'main' sscanf 15 | int main(int argc, char* argv[]){ 16 | 17 | std::string formatted_data = "11 13 15\n5 7 8"; 18 | 19 | int values[6]; 20 | // Read in 3 values at a time 21 | for(int i=0; i < 6; i+=3){ 22 | std::sscanf(formatted_data.c_str(),"%d %d %d", &values[i], 23 | &values[i+1], 24 | &values[i+2]); 25 | } 26 | 27 | // Print out all of the values 28 | for(int i=0; i < 6; i++){ 29 | std::cout << values[i] << std::endl; 30 | } 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /4/text.txt: -------------------------------------------------------------------------------- 1 | Hello, 2 | 3 | This is a text file 4 | that we are going 5 | to read one line 6 | at a time. Thank 7 | you for reading! 8 | 9 | -Mike 10 | -------------------------------------------------------------------------------- /4/textWriterOutput.txt: -------------------------------------------------------------------------------- 1 | I hope this works!!! 2 | maybe 3 | will it ever end 4 | is this microsoft word 10? 5 | quit 6 | -------------------------------------------------------------------------------- /4/textwriter.cpp: -------------------------------------------------------------------------------- 1 | // g++ -std=c++17 -g textwriter.cpp -o prog 2 | // @file textwriter.cpp 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(){ 9 | 10 | std::string line = ""; 11 | 12 | std::vector lines; 13 | 14 | std::cout << "Type: 'quit' to exit " << std::endl; 15 | // Main loop, that reads in lines 16 | while(1){ 17 | std::cout << ": "; 18 | //std::cin >> line; 19 | std::getline(std::cin,line); 20 | if(line!="quit"){ 21 | lines.push_back(line+'\n'); 22 | }else{ 23 | break; // terminate the current loop 24 | } 25 | } 26 | 27 | std::ofstream myFile("textWriterOutput.txt"); 28 | if(myFile.is_open()){ 29 | for(int i=0; i < lines.size(); i++){ 30 | myFile << lines[i]; 31 | } 32 | }else{ 33 | std::cout << "Could not save file...sorry\n"; 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /5/address.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 address.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g address.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file address.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' address 14 | int main(int argc, char* argv[]){ 15 | 16 | int a = 5;; 17 | char b = 'b';; 18 | char c = 'c';; 19 | 20 | std::cout << "a address: " << &a << std::endl; 21 | std::cout << "b address: " << (void*)&b << std::endl; 22 | std::cout << "c address: " << static_cast(&c) << std::endl; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /5/address_operator.cpp: -------------------------------------------------------------------------------- 1 | // @addresss_operator.cpp 2 | #include 3 | 4 | int main(){ 5 | 6 | int x = 5; 7 | int& iamareference = x; 8 | 9 | std::cout << " address of x is: " << &(x) << std::endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /5/ampersand.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int x = 42; 6 | 7 | std::cout << "Address of x:" 8 | << &x 9 | << std::endl; 10 | 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /5/arithmetic.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 arithmetic.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g arithmetic.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file arithmetic.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' arithmetic 14 | int main(int argc, char* argv[]){ 15 | 16 | // dynamically allocate an array 17 | int* arr = new int[3]; 18 | // Initialize some values 19 | arr[0] = 11; 20 | arr[1] = 14; 21 | arr[2] = 24; 22 | 23 | // We can index into the array as follows 24 | std::cout << arr[0] << "," << arr[1] << "," << arr[2] << std::endl; 25 | 26 | // Or, we can step to the next element using pointer arithmetic 27 | std::cout << *(arr+0) << "," << *(arr+1) << "," << *(arr+2) << std::endl; 28 | 29 | // delete the dynamically allocated array 30 | delete[] arr; 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /5/arithmetic2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 arithmetic2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g arithmetic2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file arithmetic2.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' arithmetic2 14 | int main(int argc, char* argv[]){ 15 | 16 | int* arr = new int[3]; 17 | arr[0] = 11; 18 | arr[1] = 14; 19 | arr[2] = 24; 20 | 21 | // Note: In this example, note that we are moving the 22 | // 'address' of the data by '4' each time we increment. 23 | // Why 4 bytes? Because that's the sizeof(int). 24 | std::cout << arr+0 << "," << arr+1 << "," << arr+2 << std::endl; 25 | 26 | delete[] arr; 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /5/leak.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 leak.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g leak.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file leak.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' leak 14 | int main(int argc, char* argv[]){ 15 | 16 | // Dynamically allocate a chunk of memory 17 | int* intArray = new int[60]; 18 | // Yikes, we deleted, but only the first element! 19 | // We should have usesd 'delete[]' 20 | delete intArray; // <--- Memory leak 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /5/pass_pointer_by_value.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pass_pointer_by_value.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pass_pointer_by_value.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pass_pointer_by_value.cpp 11 | #include // I/O stream library 12 | 13 | void func(int* pointer_param){ 14 | std::cout << "pointer_param address is: " 15 | << &pointer_param << std::endl; 16 | // However, the address that x points to is copied 17 | // so that means we can still derference it. 18 | *pointer_param = 9999; 19 | 20 | } 21 | 22 | 23 | // Entry point to program 'main' pass_pointer_by_value 24 | int main(int argc, char* argv[]){ 25 | 26 | int x = 5; 27 | std::cout << "x address in main is : " << &x << std::endl; 28 | // Pass in the address of 'x' because 29 | // the input parameter is a pointer. 30 | func(&x); 31 | // Print out value of x 32 | std::cout << "x is: " << x << std::endl; 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /5/passbyreference.cpp: -------------------------------------------------------------------------------- 1 | // @passbyreference.cpp 2 | #include 3 | 4 | void setValue(int& alias){ 5 | std::cout << "address alias:" << &alias << '\n'; 6 | 7 | alias= 9999; 8 | } 9 | 10 | // v's size is 1,000,000,000 11 | // ** think about why we do this for efficiency** 12 | // 13 | void PrintVector(const std::vector& v){ 14 | for(int i=0; i < v.size(); i++) 15 | std::cout << v[0]; 16 | } 17 | 18 | int main(){ 19 | 20 | int x= 42; 21 | std::cout << "address x:" << &x << '\n'; 22 | setValue(x); 23 | std::cout << "x is : " << x << '\n'; 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /5/passbyvalue.cpp: -------------------------------------------------------------------------------- 1 | // @passbyvalue.cpp 2 | #include 3 | 4 | void setValue(int a){ 5 | std::cout << "address a:" << &a << '\n'; 6 | 7 | a= 9999; 8 | } 9 | 10 | int main(){ 11 | 12 | int x= 42; 13 | std::cout << "address x:" << &x << '\n'; 14 | setValue(x); 15 | std::cout << "x is : " << x << '\n'; 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /5/pointer1.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer1.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer1.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer1.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer1 14 | int main(int argc, char* argv[]){ 15 | 16 | // A regular integer 17 | int x; 18 | // A "pointer to an integer" 19 | // Notice: The '*' right next to the data type when 20 | // we declare it. 21 | // It's kind of like a qualifier like 'const', but it 22 | // is a pointer type. 23 | int* px; 24 | 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /5/pointer2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer2.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer2 14 | int main(int argc, char* argv[]){ 15 | 16 | // Initialize an integer 17 | // (We should always initialize our values) 18 | int x=1234; 19 | // Initializng our pointer 20 | // Typically 'nullptr' if it does not 21 | // point to anything. (In, C this is simply NULL) 22 | int* px=nullptr; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /5/pointer3.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer3.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer3.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer3.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer3 14 | int main(int argc, char* argv[]){ 15 | 16 | // Initialize an integer 17 | // (We should always initialize our values) 18 | int x=1234; 19 | // Initializng our pointer 20 | // Now point to an address 21 | // (Hint: Try removing the '&' to see what happens!) 22 | int* px=&x; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /5/pointer4.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer4.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer4.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer4.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer4 14 | int main(int argc, char* argv[]){ 15 | 16 | // Initialize an integer 17 | // (We should always initialize our values) 18 | int x=1234; 19 | // Initializng our pointer 20 | // Now point to an address 21 | // (Hint: Try removing the '&' to see what happens!) 22 | int* px=&x; 23 | 24 | std::cout << "x address : " << &x << std::endl; 25 | std::cout << "px points to: " << px << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /5/pointer5.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer5.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer5.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer5.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer5 14 | int main(int argc, char* argv[]){ 15 | 16 | // Initialize an integer 17 | int x= 1234; 18 | // Initializng our pointer 19 | int* px=&x; 20 | 21 | std::cout << "x address : " << &x << std::endl; 22 | std::cout << "px points to: " << px << std::endl; 23 | 24 | // Notice the '*' in front of the variable we are 25 | // dereferencing! 26 | std::cout << "px dereferenced:" << *px << std::endl; 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /5/pointer6.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer6.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer6.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer6.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer6 14 | int main(int argc, char* argv[]){ 15 | 16 | // Initialize an integer 17 | int x= 1234; 18 | // Initializng our pointer 19 | int* px=&x; 20 | // print 'x' original value 21 | std::cout << "x is: " << x << std::endl; 22 | // Dereference 'px' 23 | *px = 70; 24 | // Hmm, what is x now? 25 | std::cout << "x is: " << x << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /5/segfault.cpp: -------------------------------------------------------------------------------- 1 | 2 | void func(){ 3 | 4 | int x = 9999999; 5 | 6 | int* px = new int[50]; 7 | 8 | px[x] = 50; 9 | 10 | delete[] px; 11 | } 12 | 13 | int main(){ 14 | 15 | func(); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /5/sizeof.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 sizeof.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g sizeof.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file sizeof.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' sizeof 14 | int main(int argc, char* argv[]){ 15 | 16 | std::cout << "bool:" << sizeof(bool) << std::endl; 17 | std::cout << "unsigned char:" << sizeof(unsigned char) << std::endl; 18 | std::cout << "char:" << sizeof(char) << std::endl; 19 | std::cout << "short:" << sizeof(short) << std::endl; 20 | std::cout << "uint8_t:" << sizeof(int8_t) << std::endl; 21 | std::cout << "int:" << sizeof(int) << std::endl; 22 | std::cout << "float:" << sizeof(float) << std::endl; 23 | std::cout << "double:" << sizeof(int) << std::endl; 24 | 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /5/sizeof2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 sizeof2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g sizeof2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file sizeof2.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | // Entry point to program 'main' sizeof2 15 | int main(int argc, char* argv[]){ 16 | 17 | // Be careful however when using sizeof 18 | // with STL data structures however. 19 | std::vector myVector; 20 | for(int i=0; i < 50; i++){ 21 | myVector.push_back(i); 22 | } 23 | 24 | std::cout << "sizeof vector container : " << sizeof(myVector) << std::endl; 25 | 26 | std::cout << "elements in vector container: " << myVector.size() << std::endl; 27 | 28 | 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /5/type.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 type.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g type.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file type.cpp 11 | #include // I/O stream library 12 | #include // Print out type of variable. 13 | 14 | // Entry point to program 'main' type 15 | int main(int argc, char* argv[]){ 16 | 17 | int a = 5; 18 | // Retrieve run-time type information 19 | std::cout << "What's my type? " << typeid(int).name() << std::endl; 20 | std::cout << "What's my type? " << typeid(a).name() << std::endl; 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /6/address.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 object.cpp.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g object.cpp.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file object.cpp.cpp 11 | #include // I/O stream library 12 | #include // std::vector 13 | 14 | // Entry point to program 'main' object.cpp 15 | int main(int argc, char* argv[]){ 16 | 17 | // myVector is an object. 18 | // It is an instance of an object with a type of 19 | // 'std::vector' that can store int's. 20 | std::vector myVector; 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /6/arithmetic.cpp: -------------------------------------------------------------------------------- 1 | 2 | // g++ -std=c++17 -g arithmetic.cpp -o prog 3 | // Run with gdb: gdb ./prog --tui 4 | ///////////////////////////////////////////////// 5 | 6 | // @file arithmetic.cpp 7 | #include // I/O stream library 8 | 9 | // Entry point to program 'main' arithmetic 10 | int main(int argc, char* argv[]){ 11 | 12 | // dynamically allocate an array 13 | int* arr = new int[3]; 14 | // Initialize some values 15 | arr[0] = 11; 16 | arr[1] = 14; 17 | arr[2] = 24; 18 | 19 | // We can index into the array as follows 20 | std::cout << arr[0] << "," 21 | << arr[1] << "," 22 | << arr[2] << std::endl; 23 | 24 | // Or, we can step to the next element using pointer arithmetic 25 | std::cout << *(arr+0) << "," 26 | << *(arr+1) << "," 27 | << *(arr+2) << std::endl; 28 | 29 | // delete the dynamically allocated array 30 | delete[] arr; 31 | 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /6/arithmetic2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 arithmetic2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g arithmetic2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file arithmetic2.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' arithmetic2 14 | int main(int argc, char* argv[]){ 15 | 16 | int* arr = new int[3]; 17 | arr[0] = 11; 18 | arr[1] = 14; 19 | arr[2] = 24; 20 | 21 | // Note: In this example, note that we are moving the 22 | // 'address' of the data by '4' each time we increment. 23 | // Why 4 bytes? Because that's the sizeof(int). 24 | std::cout << arr+0 << "," << arr+1 << "," << arr+2 << std::endl; 25 | 26 | delete[] arr; 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /6/decay2.cpp: -------------------------------------------------------------------------------- 1 | // @file decay2.cpp 2 | // g++ -g -std=c++17 decay2.cpp -o prog 3 | #include 4 | 5 | void arrayDecay(short* arr){ 6 | std::cout << "sizeof(arr) : " 7 | << sizeof(arr) 8 | << std::endl; 9 | } 10 | 11 | int main(){ 12 | 13 | short array[6]; 14 | for(int i=0; i < 6; i++){ 15 | array[i] = i; 16 | } 17 | 18 | std::cout << "sizeof(array): " 19 | << sizeof(array) << std::endl; 20 | // Passing 'array' into function decays into pointer 21 | arrayDecay(array); 22 | 23 | return 0; 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /6/decay2_fixed.cpp: -------------------------------------------------------------------------------- 1 | // @file decay2_fixed.cpp 2 | // g++ -std=c++17 decay2_fixed.cpp -o prog 3 | #include 4 | #include 5 | 6 | void arrayAsPointerWithSize(short* arr, size_t collectionSize){ 7 | std::cout << "sizeof(arr) : " << sizeof(arr) << std::endl; 8 | for(int i=0; i < collectionSize; i++){ 9 | std::cout << arr[i] << std::endl; 10 | } 11 | } 12 | 13 | int main(){ 14 | 15 | short array[6]; 16 | for(int i=0; i < 6; i++){ 17 | array[i] = i; 18 | } 19 | 20 | std::cout << "sizeof(array): " << sizeof(array) << std::endl; 21 | arrayAsPointerWithSize(array,6); 22 | 23 | return 0; 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /6/decay_brackets_warning.cpp: -------------------------------------------------------------------------------- 1 | // @file decay_brackets_warning.cpp 2 | // g++ -g -std=c++17 decay2.cpp -o prog 3 | #include 4 | 5 | // This will give a warning if you pass in a pointer 6 | // type that the converson is taking place 7 | void arrayDecay(short arr[]){ 8 | std::cout << "sizeof(arr) : " 9 | << sizeof(arr) 10 | << std::endl; 11 | } 12 | 13 | int main(){ 14 | 15 | short array[6]; 16 | for(int i=0; i < 6; i++){ 17 | array[i] = i; 18 | } 19 | 20 | std::cout << "sizeof(array): " 21 | << sizeof(array) << std::endl; 22 | // Passing 'array' into function decays into pointer 23 | arrayDecay(array); 24 | 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /6/heap.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 heap.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g heap.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file heap.cpp 11 | #include // I/O stream library 12 | #include 13 | #include 14 | 15 | 16 | int* important(){ 17 | int* x = new int; 18 | (*x) = rand() % 10 + 1; 19 | std::cout << "x is: " << *x << std::endl; 20 | 21 | return x; 22 | } 23 | 24 | // Entry point to program 'main' heap.cpp 25 | int main(int argc, char* argv[]){ 26 | 27 | srand(time(NULL)); 28 | 29 | for(int i=0; i < 1000; i++){ 30 | // We are assigning our pointer 31 | // that we initalize, the the address 32 | // of something allocated on the heap in 'important' 33 | int* temporary = important(); 34 | 35 | // Now we can use that memory 36 | std::cout << "temporary: " << *temporary << std::endl; 37 | // And we still need to delete our heap memory 38 | // before we end our program. 39 | delete temporary; 40 | } 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /6/multiple_pointers.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 multiple_pointers.cpp.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g multiple_pointers.cpp.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file multiple_pointers.cpp.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' multiple_pointers.cpp 14 | int main(int argc, char* argv[]){ 15 | 16 | int x = 5; 17 | int* px = &x; 18 | int* px2 = &x; 19 | 20 | std::cout << "px and px2 are: " << *px << "," << *px2 << '\n'; 21 | 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /6/pass_pointer_by_value.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pass_pointer_by_value.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pass_pointer_by_value.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pass_pointer_by_value.cpp 11 | #include // I/O stream library 12 | 13 | void func(int* pointer_param){ 14 | std::cout << "pointer_param address is: " 15 | << &pointer_param << std::endl; 16 | // However, the address that x points to is copied 17 | // so that means we can still derference it. 18 | *pointer_param = 9999; 19 | } 20 | 21 | 22 | // Entry point to program 'main' pass_pointer_by_value 23 | int main(int argc, char* argv[]){ 24 | 25 | int x = 5; 26 | std::cout << "x address in main is : " 27 | << &x 28 | << std::endl; 29 | // Pass in the address of 'x' because 30 | // the input parameter is a pointer. 31 | func(&x); 32 | // Print out value of x 33 | std::cout << "x is: " << x << std::endl; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /6/static.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 static.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g static.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file static.cpp 11 | #include // I/O stream library 12 | #include 13 | #include 14 | 15 | int* g_pointer; 16 | 17 | void important(){ 18 | static int x = 0; 19 | x = rand() % 10 + 1; 20 | std::cout << "x is: " << x << std::endl; 21 | g_pointer = &x; 22 | } 23 | 24 | void function(){ 25 | int x = 9999; 26 | int y = 10000; 27 | int a = 10001; 28 | int b = 10002; 29 | } 30 | 31 | // Entry point to program 'main' static.cpp 32 | int main(int argc, char* argv[]){ 33 | 34 | srand(time(NULL)); 35 | 36 | for(int i=0; i < 1000; i++){ 37 | important(); 38 | } 39 | 40 | std::cout << "g_pointer is: " << *g_pointer << std::endl; 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /7/array.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 array.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g array.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file array.cpp 11 | #include // I/O stream library 12 | #include // For 'std::array' 13 | 14 | // Entry point to program 'main' 15 | int main(int argc, char* argv[]){ 16 | 17 | // Statically sized arrays are 'fixed-size' 18 | // meaning we cannot change the amount of 19 | // data stored within them. 20 | char myArray[5]; 21 | std::array mySTLArray; 22 | 23 | // Both will report '5' 24 | std::cout << sizeof(myArray) << std::endl; 25 | std::cout << sizeof(mySTLArray) << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /7/array2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 array2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g array2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file array2.cpp 11 | #include // I/O stream library 12 | 13 | void PrintArray(char* array, size_t size){ 14 | for(int i=0; i < size; i++){ 15 | std::cout << array[i]; 16 | } 17 | std::cout << std::endl; 18 | } 19 | 20 | // Entry point to program 'main' 21 | int main(int argc, char* argv[]){ 22 | 23 | // Initialize our array 24 | char myArray[] = {'m','i','k','e','\0'}; 25 | 26 | // Allocate a larger array 27 | char myCopy[10]; 28 | 29 | // Copy elements 1 at a time 30 | for(int i=0; i < 5; i++){ 31 | myCopy[i] = myArray[i]; 32 | } 33 | 34 | // Add some more characters 35 | myCopy[4] = '!' ; 36 | myCopy[5] = '!' ; 37 | myCopy[6] = '!' ; 38 | myCopy[7] = '!' ; 39 | myCopy[8] = '!' ; 40 | myCopy[9] = '\0' ; 41 | 42 | PrintArray(myArray,5); 43 | PrintArray(myCopy,10); 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /7/array2_std_copy.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 array2_std_copy.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g array2_std_copy.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file array2_std_copy.cpp 11 | #include // I/O stream library 12 | #include // for std::copy 13 | 14 | void PrintArray(char* array, size_t size){ 15 | for(int i=0; i < size; i++){ 16 | std::cout << array[i]; 17 | } 18 | std::cout << std::endl; 19 | } 20 | 21 | // Entry point to program 'main' 22 | int main(int argc, char* argv[]){ 23 | 24 | // Initialize our array 25 | char myArray[] = {'m','i','k','e','\0'}; 26 | 27 | // Allocate a larger array 28 | char myCopy[10]; 29 | 30 | // Copy elements 1 at a time 31 | std::copy(std::cbegin(myArray),std::cend(myArray),myCopy); 32 | 33 | // Add some more characters 34 | myCopy[4] = '!' ; 35 | myCopy[5] = '!' ; 36 | myCopy[6] = '!' ; 37 | myCopy[7] = '!' ; 38 | myCopy[8] = '!' ; 39 | myCopy[9] = '\0' ; 40 | 41 | PrintArray(myArray,5); 42 | PrintArray(myCopy,10); 43 | 44 | return 0; 45 | } 46 | 47 | -------------------------------------------------------------------------------- /7/lerp.cpp: -------------------------------------------------------------------------------- 1 | // @file lerp.cpp 2 | // g++ -g -std=c++20 lerp.cpp -o prog 3 | // NOTE: We need c++20 for this feature! 4 | #include 5 | #include 6 | 7 | /* If you don't have C++ 20, use this function from 8 | * https://en.cppreference.com/w/cpp/numeric/lerp 9 | float naive_lerp(float a, float b, float t) 10 | { 11 | return a + t * (b - a); 12 | } 13 | */ 14 | 15 | 16 | int main(){ 17 | 18 | float a=0.0; 19 | float b=100.0; 20 | 21 | for(float t=0.0; t<=1.0f; t+=0.1f){ 22 | std::cout << "std::lerp(a,b,t)=" << std::lerp(a,b,t) << std::endl; 23 | } 24 | 25 | return 0; 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /7/sfml/simple.cpp: -------------------------------------------------------------------------------- 1 | // @file sfml/simple.cpp 2 | /* Linux: 3 | g++ -g simple.cpp -o prog -lsfml-graphics -lsfml-window -lsfml-system 4 | 5 | Mac M1: 6 | g++ -g simple.cpp -I/opt/homebrew/Cellar/sfml/2.5.1_2/include -L/opt/homebrew/Cellar/sfml/2.5.1_2/lib -o prog -lsfml-graphics -lsfml-window -lsfml-system 7 | */ 8 | 9 | #include 10 | #include 11 | 12 | int main(){ 13 | 14 | return 0; 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /7/vector.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 vector.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g vector.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file vector.cpp 11 | #include // I/O stream library 12 | #include // Vector data structure 13 | 14 | // Entry point to program 'main' 15 | int main(int argc, char* argv[]){ 16 | 17 | // Initialize our dynamic array i.e. std::vector 18 | std::vector myVector = {'m','i','k','e','\0'}; 19 | 20 | // Add some more characters 21 | myVector.push_back('!') ; 22 | myVector.push_back('!') ; 23 | myVector.push_back('!') ; 24 | myVector.push_back('!') ; 25 | myVector.push_back('!') ; 26 | myVector.push_back('\0') ; 27 | 28 | for(int i=0; i < myVector.size(); i++){ 29 | // .at(i) does bounds checking, versus 30 | // [i] which does not bounds check 31 | std::cout << myVector.at(i); 32 | } 33 | std::cout << std::endl; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /8/.struct7.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/8/.struct7.cpp.swp -------------------------------------------------------------------------------- /8/list1.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 list1.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g list1.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file list1.cpp 11 | #include // I/O stream library 12 | 13 | struct Node{ 14 | int data; // Data carried in the Node 15 | Node* next; // Pointer to the next Node 16 | }; 17 | 18 | int main(){ 19 | 20 | // Create on node on the heap 21 | Node* firstNode = new Node; 22 | // Setup the node 23 | firstNode->data = 5; 24 | firstNode->next = nullptr; // We don't point to anything 25 | // i.e. no following node 26 | 27 | // Delete our node 28 | delete firstNode; 29 | 30 | return 0; 31 | } 32 | 33 | 34 | -------------------------------------------------------------------------------- /8/struct.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 struct.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g struct.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct.cpp 11 | #include // I/O stream library 12 | 13 | struct color{ 14 | int r,g,b; 15 | }; 16 | 17 | struct pixel{ 18 | int x; // Fields or 'member variables' 19 | int y; // x, y, and color 20 | color rgb; 21 | }; 22 | 23 | // Entry point to program 'main' struct.cpp 24 | int main(int argc, char* argv[]){ 25 | 26 | pixel myFirstPixel; 27 | myFirstPixel.x = 5; 28 | myFirstPixel.y = 15; 29 | myFirstPixel.rgb.r = 255; 30 | myFirstPixel.rgb.g = 125; 31 | myFirstPixel.rgb.b = 65; 32 | 33 | 34 | return 0; 35 | } 36 | 37 | -------------------------------------------------------------------------------- /8/struct1.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 struct1.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g struct1.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct1.cpp 11 | #include // I/O stream library 12 | 13 | struct Point2D{ 14 | float x; 15 | float y; 16 | }; 17 | 18 | int main(){ 19 | 20 | // An instance named 'origin' of type 'Point2D' 21 | Point2D origin; 22 | // Set the 'member variables' (a.k.a fields) 23 | // of our struct. 24 | origin.x = 0.0f; 25 | origin.y = 0.0f; 26 | 27 | std::cout << "Origin is: (" 28 | << origin.x 29 | << "," 30 | << origin.y 31 | << ")" 32 | << std::endl; 33 | 34 | return 0; 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /8/struct2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 struct2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g struct2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct2.cpp 11 | #include // I/O stream library 12 | 13 | struct Point2D{ 14 | float x; 15 | float y; 16 | }; 17 | 18 | int main(){ 19 | 20 | // An instance named 'origin' of type 'Point2D' 21 | Point2D origin; 22 | // Set the 'member variables' (a.k.a fields) 23 | // of our struct. 24 | origin.x = 0.0f; 25 | origin.y = 0.0f; 26 | 27 | 28 | // Query the type size either by the type 29 | // or a specific instance of the type 30 | std::cout << sizeof(Point2D) << std::endl; 31 | std::cout << sizeof(origin) << std::endl; 32 | 33 | 34 | return 0; 35 | } 36 | 37 | 38 | -------------------------------------------------------------------------------- /8/struct3.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 struct3.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g struct3.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct3.cpp 11 | #include // I/O stream library 12 | 13 | struct Point2D{ 14 | public: // By default access level is public in 15 | // a 'struct' in C++ 16 | float x; 17 | float y; 18 | }; 19 | 20 | int main(){ 21 | 22 | // An instance named 'origin' of type 'Point2D' 23 | Point2D origin; 24 | // Set the 'member variables' (a.k.a fields) 25 | // of our struct. 26 | origin.x = 0.0f; 27 | origin.y = 0.0f; 28 | 29 | 30 | // Query the type size either by the type 31 | // or a specific instance of the type 32 | std::cout << sizeof(Point2D) << std::endl; 33 | std::cout << sizeof(origin) << std::endl; 34 | 35 | 36 | return 0; 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /8/struct4.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 struct4.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g struct4.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file struct4.cpp 11 | #include // I/O stream library 12 | 13 | struct Point2D{ 14 | public: // By default access level is public in 15 | // a 'struct' in C++ 16 | float x; 17 | float y; 18 | 19 | // Publically available Member Function 20 | void Print(){ 21 | std::cout << this 22 | << " Point2D(" 23 | << x << "," << y << ")" 24 | << std::endl; 25 | } 26 | }; 27 | 28 | int main(){ 29 | 30 | // An instance named 'origin' of type 'Point2D' 31 | Point2D origin; 32 | // Set the 'member variables' (a.k.a fields) 33 | // of our struct. 34 | origin.x = 0.0f; 35 | origin.y = 0.0f; 36 | 37 | // Usage of our 'struct' 38 | origin.Print(); 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /9/Math.cpp: -------------------------------------------------------------------------------- 1 | // @File Math.cpp 2 | #include "Math.hpp" 3 | 4 | int add(int a, int b){ 5 | return a+b; 6 | } 7 | 8 | 9 | -------------------------------------------------------------------------------- /9/Math.hpp: -------------------------------------------------------------------------------- 1 | // @File Math.hpp 2 | #ifndef MATH_HPP 3 | #define MATH_HPP 4 | 5 | // Function Declaration 6 | int add(int a, int b); 7 | 8 | 9 | #endif 10 | 11 | 12 | -------------------------------------------------------------------------------- /9/global_namespace/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 main.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g main.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file global_namespace/main.cpp 11 | #include // I/O stream library 12 | 13 | namespace mike{ 14 | int x; 15 | void foo(){ 16 | std::cout << "mike::foo()\n"; 17 | } 18 | } 19 | 20 | void foo(){ 21 | std::cout << "::foo (in global namespace)\n"; 22 | } 23 | 24 | int main(){ 25 | 26 | // Observe the difference in the calls here. 27 | mike::foo(); 28 | // Observe the two different ways to refer to foo 29 | // in the global namespace 30 | foo(); 31 | ::foo(); 32 | 33 | return 0; 34 | } 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /9/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 main.cpp Math.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g main.cpp Math.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file main.cpp 11 | #include // I/O stream library 12 | // Our include file for 'Math.hpp' 13 | #include "Math.hpp" 14 | 15 | int main(){ 16 | 17 | std::cout << add(4,7) << std::endl; 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /9/namespace/Math.cpp: -------------------------------------------------------------------------------- 1 | // @File namespace/Math.cpp 2 | #include "Math.hpp" 3 | 4 | // Add is in the 5 | // 'mathlibrary' namespace 6 | namespace mathlibrary{ 7 | int add(int a, int b){ 8 | return a+b; 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /9/namespace/Math.hpp: -------------------------------------------------------------------------------- 1 | // @File namespace/Math.hpp 2 | #ifndef MATH_HPP 3 | #define MATH_HPP 4 | 5 | namespace mathlibrary{ 6 | // Function Declaration 7 | int add(int a, int b); 8 | } 9 | 10 | #endif 11 | 12 | 13 | -------------------------------------------------------------------------------- /9/namespace/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 main.cpp Math.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g main.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file namespace/main.cpp 11 | #include // I/O stream library 12 | // Our include file for 'Math.hpp' 13 | #include "Math.hpp" 14 | 15 | int main(){ 16 | 17 | std::cout << mathlibrary::add(4,7) << std::endl; 18 | 19 | return 0; 20 | } 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /9/preprocessor/.preprocessor2.cpp.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/9/preprocessor/.preprocessor2.cpp.swp -------------------------------------------------------------------------------- /9/preprocessor/preprocessor1.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 preprocessor1.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g preprocessor1.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file preprocessor/preprocessor1.cpp 11 | #include // I/O stream library 12 | 13 | #define UNIX 1 14 | #define MESSAGE "Hello\n" 15 | 16 | int main(){ 17 | 18 | std::cout << MESSAGE << std::endl; 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /9/preprocessor/preprocessor2.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 preprocessor2.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g preprocessor2.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file preprocessor/preprocessor2.cpp 11 | #include // I/O stream library 12 | 13 | #define UNIX 1 14 | #define MESSAGE "Hello\n" 15 | 16 | int main(){ 17 | 18 | #if UNIX 19 | std::cout << "Hi Unix User" << std::endl; 20 | #else 21 | std::cout << MESSAGE << "non-unix user" << std::endl; 22 | #endif 23 | 24 | return 0; 25 | } 26 | 27 | 28 | -------------------------------------------------------------------------------- /9/scope/Point2D.cpp: -------------------------------------------------------------------------------- 1 | // @file scope/Point2D.cpp 2 | #include "Point2D.hpp" 3 | 4 | // Note: Within our implementation we need 5 | // to include any files we need. 6 | // This 'Point2D.hpp' file should be 7 | // able to 'stand alone' without any 8 | // knowledge of other .cpp files. 9 | #include 10 | 11 | // Publically available Member Function 12 | void Point2D::Print(){ 13 | std::cout << this 14 | << " Point2D(" 15 | << x << "," << y << ")" 16 | << std::endl; 17 | } 18 | 19 | // Note: We can use 'this' in front of 20 | // a member variable to make 21 | // our use of the same named 22 | // variables less ambiguous 23 | void Point2D::SetXY(float x, float y){ 24 | // this.x is the 'internal member variable' 25 | // 'x' is the argument passed into the member 26 | // function 27 | this->x = x; 28 | this->y = y; 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /9/scope/Point2D.hpp: -------------------------------------------------------------------------------- 1 | // @file scope/Point2D.hpp 2 | #ifndef POINT2D_HPP 3 | #define POINT2D_HPP 4 | 5 | struct Point2D{ 6 | float x; 7 | float y; 8 | 9 | // Publically available Member Functions 10 | void Print(); 11 | void SetXY(float x, float y); 12 | }; 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /9/scope/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 main.cpp Point2D.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g main.cpp Point2D.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file scope/main.cpp 11 | #include // I/O stream library 12 | 13 | #include "Point2D.hpp" 14 | 15 | // @file scope/main.cpp 16 | int main(){ 17 | 18 | // Stack allocated 19 | Point2D pointsOnStack[10]; 20 | for(int i=0; i < 10; ++i){ 21 | pointsOnStack[i].SetXY(i,i); 22 | pointsOnStack[i].Print(); 23 | } 24 | 25 | // Heap allocated 26 | Point2D* heapAllocatedPointsArray = new Point2D[10]; 27 | for(int i=0; i < 10; ++i){ 28 | heapAllocatedPointsArray[i].SetXY(i*5,i*5); 29 | heapAllocatedPointsArray[i].Print(); 30 | } 31 | 32 | 33 | delete[] heapAllocatedPointsArray; 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Code Repository 2 | 3 | Public code repository for CS 3520 4 | -------------------------------------------------------------------------------- /backup/14/pointer.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 pointer.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g pointer.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file pointer.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' pointer 14 | int main(int argc, char* argv[]){ 15 | 16 | float* p_float = nullptr; // I point to floats 17 | float some_float = 3.5f; // I am a regular float 18 | p_float = &some_float; // Point to address of variable 19 | p_float = &(some_float); // Same line as above, but useful 20 | // to understand '&' is an operator, 21 | // but we can think of the same as 22 | // a function. 23 | 24 | std::cout << "dereference p_float: " << *p_float << std::endl; 25 | 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /backup/14/problems.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 problems.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g problems.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file problems.cpp 11 | #include // I/O stream library 12 | 13 | // Entry point to program 'main' problems 14 | int main(int argc, char* argv[]){ 15 | 16 | // (1) Dereference nullptr 17 | float* p_float = nullptr; 18 | *p_float = 7.0f; // ERROR - Dereferencing a null pointer 19 | 20 | // (2) Double free 21 | int* array = new int[10]; 22 | delete[] array; 23 | delete[] array; // ERROR - 'Double free' 24 | 25 | // (3) Dangling pointer - refering to something that 26 | // has already been deleted 27 | int* shared_array = new int[10]; 28 | int* pointer_to_array = shared_array; 29 | delete[] shared_array; 30 | pointer_to_array[0] = 7; // ERROR - Cannot derefernce, because 31 | // we deleted the shared_array 32 | // that we were pointing to. 33 | 34 | 35 | return 0; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /backup/14/smart_pointer.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 smart_pointer.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g smart_pointer.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file smart_pointer.cpp 11 | #include // I/O stream library 12 | 13 | // Let's build a smart pointer for handling integers. 14 | class SmartIntPointer{ 15 | private: 16 | int* m_pointer = nullptr; // initialize the pointer 17 | public: 18 | // Handle creation of our 19 | SmartIntPointer(){ 20 | m_pointer = nullptr; 21 | } 22 | // Destructor 23 | ~SmartIntPointer(){ 24 | if(m_pointer!=nullptr){ 25 | delete m_pointer; 26 | } 27 | } 28 | // Handle dereferencing 29 | int* operator->(){ 30 | return this->m_pointer; 31 | } 32 | // And more.... 33 | // To be continued when we learn about move semantics... 34 | 35 | 36 | }; 37 | 38 | // Entry point to program 'main' smart_pointer 39 | int main(int argc, char* argv[]){ 40 | 41 | SmartIntPointer myPointer; 42 | 43 | return 0; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /backup/14/stack.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 stack.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g stack.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file stack.cpp 11 | #include // I/O stream library 12 | #include 13 | 14 | // Global piece of data 15 | char* g_pointer; 16 | 17 | void foo(){ 18 | // Chunk of memory, allocated in foo 19 | char array[100]; 20 | 21 | std::memset(array,65,sizeof(char)*100); 22 | 23 | g_pointer = &array[0]; 24 | std::cout << "g_pointer: " << g_pointer[0] << std::endl; 25 | } 26 | 27 | 28 | // Entry point to program 'main' stack 29 | int main(int argc, char* argv[]){ 30 | g_pointer = new char[1]; 31 | 32 | // Stack allocated 33 | char* p_main = nullptr; 34 | foo(); 35 | char array[9000]; 36 | std::memset(array,66,sizeof(char)*9000); 37 | 38 | std::cout << "g_pointer: " << g_pointer[0] << std::endl; 39 | return 0; 40 | } 41 | 42 | -------------------------------------------------------------------------------- /backup/14/unique.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 unique.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g unique.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file unique.cpp 11 | #include // I/O stream library 12 | #include // Access smart pointers 13 | 14 | class Object{ 15 | public: 16 | Object() { std::cout << "Constructor\n"; } 17 | ~Object() { std::cout << "Destructor\n"; } 18 | }; 19 | 20 | 21 | // Entry point to program 'main' unique 22 | int main(int argc, char* argv[]){ 23 | 24 | { 25 | std::unique_ptr myObjectPtr(new Object); 26 | // NOTE: Below is 'illegal' because we cannot 27 | // assign to a unique_ptr--awesome! 28 | //std::unique_ptr myObjectPtr2 = myObjectPtr; 29 | } 30 | 31 | // An alternative way to create the pointer 32 | // Much more explict, avoids the call to 'new' 33 | // Also does some error handling. 34 | { 35 | std::unique_ptr myObjectPtr = std::make_unique(); 36 | } 37 | 38 | 39 | 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /backup/14/vector.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 vector.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g vector.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file vector.cpp 11 | #include // I/O stream library 12 | #include 13 | #include 14 | 15 | // Entry point to program 'main' vector 16 | int main(int argc, char* argv[]){ 17 | 18 | 19 | // I prefer this style 20 | std::vector> vector_of_smart_ints; 21 | // over the below 22 | std::vector vector_of_ints; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /backup/14/weak.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 weak.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g weak.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file weak.cpp 11 | #include // I/O stream library 12 | #include // Access smart pointers 13 | 14 | class Object{ 15 | public: 16 | Object() { std::cout << "Constructor\n"; } 17 | ~Object() { std::cout << "Destructor\n"; } 18 | }; 19 | 20 | 21 | // Entry point to program 'main' weak 22 | int main(int argc, char* argv[]){ 23 | 24 | { 25 | // weak_ptr is almost like a 'temporary' pointer that we just 26 | // want to be able to point to something if it exists. 27 | std::weak_ptr myWeakPtr; 28 | { 29 | // Make a second pointer 30 | std::shared_ptr mySharedObjectPtr2 = std::make_shared(); 31 | // Assign our weak pointer to a shared pointer, but 32 | // we do not increase the reference count on sharedObjectPtr2. 33 | myWeakPtr = mySharedObjectPtr2; 34 | } 35 | } 36 | 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /backup/14/weak_pointers.cpp: -------------------------------------------------------------------------------- 1 | // @file weak_pointers.cpp 2 | #include 3 | #include 4 | 5 | // Here we're creating a global weak pointer(gw) 6 | std::weak_ptr gw; 7 | 8 | void f(){ 9 | 10 | // Pointing a new shared pointer, to whatever 11 | // our weak_ptr (gw) holds. 12 | auto spt = gw.lock(); 13 | 14 | // If the shared pointer we created, which now 15 | // also points to the same thingg as our weak_ptr, 16 | // then we can proceed. 17 | // However, if the thing our weak_ptr, pointed to 18 | // no longer exists (i.e "sp" below"), thten 19 | if (spt != nullptr) { // Has to be copied into a shared_ptr before usage 20 | std::cout << *spt << "\n"; 21 | } 22 | else { 23 | std::cout << "gw is expired\n"; 24 | } 25 | } 26 | 27 | int main(){ 28 | { // Create a scope within main 29 | int x = 5; 30 | int* p = &x; 31 | std::shared_ptr sp = std::make_shared(42); 32 | gw = sp; // Assigning weak_ptr to the shared_ptr 33 | f(); 34 | } // Exit the scope at line 23, thus deleting all local variables 35 | // between lines 23 and 29 36 | 37 | f(); 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /backup/19/shared_ptr_version/AdjacencyList.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ADJACENCY_LIST_HPP 2 | #define ADJACENCY_LIST_HPP 3 | 4 | #include 5 | #include 6 | #include // This has our smart pointers 7 | //#include 8 | 9 | struct node_t{ 10 | std::string name; 11 | std::vector> neighbors; 12 | }; 13 | 14 | class AdjacencyList{ 15 | public: 16 | AdjacencyList(); 17 | ~AdjacencyList(); 18 | 19 | // Add a node new 20 | void AddNode(std::string name); 21 | void AddDirectedEdge(std::string source, std::string destination); 22 | 23 | void print(); 24 | private: 25 | std::vector> m_nodes; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /backup/19/shared_ptr_version/main.cpp: -------------------------------------------------------------------------------- 1 | // Compile with: 2 | // g++ -std=c++17 -g main.cpp AdjacencyList.cpp -o prog 3 | 4 | #include "AdjacencyList.hpp" 5 | 6 | bool unitTest1(){ 7 | AdjacencyList list; 8 | list.AddNode("A"); 9 | list.AddNode("B"); 10 | list.AddNode("C"); 11 | 12 | list.AddDirectedEdge("A","B"); 13 | list.AddDirectedEdge("A","C"); 14 | list.AddDirectedEdge("B","C"); 15 | 16 | list.print(); 17 | 18 | return true; 19 | } 20 | 21 | 22 | int main(){ 23 | 24 | unitTest1(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /backup/20/sprite.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/backup/20/sprite.bmp -------------------------------------------------------------------------------- /backup/21/prog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MikeShah/cpp/3e2eddb1cdd15696e63d0ff12b0e7d175beef485/backup/21/prog -------------------------------------------------------------------------------- /backup/21/thread.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 thread.cpp -o prog -lpthread 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g thread.cpp -o prog -lpthread 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file thread.cpp 11 | #include 12 | #include 13 | 14 | // Test function where we'll create a new thread. 15 | void test(int x){ 16 | std::cout << "Hello from our thread" << std::endl; 17 | std::cout << "Argument passed to thread is: " << x << std::endl; 18 | } 19 | 20 | // Entry point to program 'main' thread.cpp 21 | int main(){ 22 | // Create a new thread and pass in a value for 23 | // it's first parameter. 24 | std::thread myThread(&test,100); 25 | // Join with the main thread, which is the same as 26 | // saying "hey, wait until the main thread finishes executing. 27 | myThread.join(); // Join with the thread, it was called from. 28 | // which in this case, is the main thread. 29 | std::cout << "Hello from the main thread!" << std::endl; 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /backup/21/thread4.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 thread4.cpp -o prog -lpthread 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g thread4.cpp -o prog -lpthread 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file thread4.cpp 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | 18 | std::condition_variable g_cond_var; 19 | std::mutex global_mutex; 20 | 21 | // Entry point to program 'main' thread4.cpp 22 | int main(){ 23 | 24 | int value = 1009; 25 | bool notified = false; 26 | 27 | std::thread reporter([&]{ 28 | std::unique_lock lock(global_mutex); 29 | while(!notified){ 30 | g_cond_var.wait(lock); 31 | } 32 | std::cout << "Print the value as: " << value << std::endl; 33 | }); 34 | 35 | std::thread assigner([&]{ 36 | value = 42; 37 | notified = true; 38 | std::cout << "running thread assigner first" << std::endl; 39 | g_cond_var.notify_one(); 40 | }); 41 | 42 | reporter.join(); 43 | assigner.join(); 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /backup/21/thread5.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 thread5.cpp -o prog -lpthread 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g thread5.cpp -o prog -lpthread 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file thread5.cpp 11 | #include 12 | #include 13 | 14 | int square(int x){ 15 | return x*x; 16 | } 17 | 18 | // Entry point to program 'main' thread5.cpp 19 | int main(){ 20 | 21 | // So 'a' is a future. 22 | // Type 'whatis a' in gdb 23 | // type = std::future 24 | auto a = std::async(&square,12); 25 | // ... some time passes 26 | int v = a.get(); // Then we retrieve the value from 'a' 27 | // and if it hasn't completed execution at this point 28 | // then our program will wait until it does. 29 | std::cout << "The async thread has returned " << v << std::endl; 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /backup/22/new.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 new.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g new.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file new.cpp 11 | #include 12 | #include 13 | 14 | /* 15 | void* operator new(size_t size){ 16 | std::cout << "Bytes allocateed " << size << std::endl;; 17 | void* memory = malloc(size); 18 | return memory; 19 | } 20 | 21 | void operator delete(void* memory,size_t size){ 22 | std::cout << "Bytes being freed " << size << std::endl;; 23 | return free(memory); 24 | } 25 | 26 | void operator delete[](void* memory){ 27 | std::cout << "Bytes being freed " << std::endl;; 28 | return free(memory); 29 | } 30 | */ 31 | 32 | // Entry point to program 'main' new.cpp 33 | int main(){ 34 | 35 | int* arrayOfInts = new int[1000]; 36 | delete[] arrayOfInts; 37 | 38 | return 0; 39 | } 40 | 41 | -------------------------------------------------------------------------------- /backup/23/accumulate.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 accumulate.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g accumulate.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file accumulate.cpp 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include // New library for mathematical algorithms 16 | 17 | // Entry point to program 'main' accumulate.cpp 18 | int main(){ 19 | 20 | std::vector myVector{1,1,1,1,5,1,6,7,8}; 21 | 22 | // Where to start, end, and the initial sum (which is 0) 23 | int sum = std::accumulate(myVector.begin(),myVector.end(),0); 24 | 25 | std::cout << "The sum is: " << sum << std::endl; 26 | 27 | 28 | return 0; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /backup/23/copy.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 copy.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g copy.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file copy.cpp 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // Entry point to program 'main' copy.cpp 17 | int main(){ 18 | std::vector myVector{1,1,1,1,5,1,6,7,8}; 19 | std::vector myVector2; 20 | 21 | // Copy elements 1 at a time 22 | /* 23 | std::copy(myVector.begin(), 24 | myVector.end(), 25 | std::back_inserter(myVector2)); 26 | */ 27 | 28 | // Copy into data structure, based on some condition. 29 | // We use a lambda function, to determine what that 30 | // condition is. 31 | std::copy_if(myVector.begin(), 32 | myVector.end(), 33 | std::back_inserter(myVector2), 34 | [](int x){return x>5;}); 35 | 36 | // Iterate through and print the elements 37 | for(auto& e : myVector2){ 38 | std::cout << e << std::endl; 39 | } 40 | return 0; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /backup/23/nth.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 nth.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g nth.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file nth.cpp 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | // Entry point to program 'main' nth.cpp 18 | int main(){ 19 | std::vector myVector{1,1,1,1,5,1,6,7,8}; 20 | 21 | std::nth_element(myVector.begin(), 22 | myVector.begin() + myVector.size()/2, 23 | myVector.end()); 24 | 25 | std::cout << "The median is " << myVector[myVector.size()/2] << std::endl; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /backup/23/reverse.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 reverse.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g reverse.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file reverse.cpp 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // Entry point to program 'main' reverse.cpp 17 | int main(){ 18 | 19 | std::vector myVector{1,2,3,4,5,6,7,8}; 20 | // Reverse our collection 21 | std::reverse(myVector.begin(),myVector.end()); 22 | // Iterate through and print the elements 23 | // Using a 'for-each' style loop, to just iterate 24 | // through all of our elements. 25 | for(auto& e : myVector){ 26 | std::cout << e << std::endl; 27 | } 28 | 29 | 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /backup/23/swap.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 swap.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g swap.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file swap.cpp 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // Entry point to program 'main' swap.cpp 17 | int main(){ 18 | std::vector myVector{1,1,1,1,5,1,6,7,8}; 19 | std::vector myVector2; 20 | 21 | std::swap(myVector2,myVector); 22 | 23 | // Iterate through and print the elements 24 | std::cout << "myVector2:"; 25 | for(auto& e : myVector2){ 26 | std::cout << e << ","; 27 | } 28 | std::cout << "\nmyVector:"; 29 | for(auto& e : myVector){ 30 | std::cout << e << ","; 31 | } 32 | std::cout << std::endl; 33 | return 0; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /backup/23/unique.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++20 unique.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++20 -g unique.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | // @file unique.cpp 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | // Entry point to program 'main' unique.cpp 17 | int main(){ 18 | 19 | std::vector myVector{1,1,1,1,5,1,6,7,8}; 20 | // We can also sort easily! 21 | std::sort(myVector.begin(),myVector.end()); 22 | // Just retrieve the (adjacent) unique elements in our vector. 23 | auto uniq = std::unique(myVector.begin(), myVector.end()); 24 | // Now, we need to erase, the 'extra' space 25 | // Why we pass in 'uniq', is because remember, that is a 26 | // iterator, and points us to the last element in our new unique 27 | // collection. 28 | myVector.erase(uniq,myVector.end()); 29 | 30 | 31 | // Iterate through and print the elements 32 | for(auto& e : myVector){ 33 | std::cout << e << std::endl; 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /backup/26/test.cpp: -------------------------------------------------------------------------------- 1 | // g++ test.cpp `./wx-config --cxxflags --libs std` -o prog 2 | #include 3 | 4 | class Simple : public wxFrame 5 | { 6 | public: 7 | Simple(const wxString& title) 8 | : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 150)) 9 | { 10 | Centre(); 11 | } 12 | }; 13 | 14 | class MyApp : public wxApp 15 | { 16 | public: 17 | bool OnInit() 18 | { 19 | Simple *simple = new Simple(wxT("Simple")); 20 | simple->Show(true); 21 | return true; 22 | } 23 | }; 24 | 25 | wxIMPLEMENT_APP(MyApp); 26 | 27 | -------------------------------------------------------------------------------- /backup/8/copyconstructor.cpp: -------------------------------------------------------------------------------- 1 | //@file copyconstructor.cpp 2 | #include 3 | 4 | class T{ 5 | public: 6 | T(){ std::cout << "constructor called\n"; } 7 | ~T(){ std::cout << "destructor called\n"; } 8 | 9 | T(const T& copy){ 10 | std::cout << "copy constructor called\n"; 11 | } 12 | }; 13 | 14 | int main(){ 15 | 16 | T object1; 17 | T object2 = object1; 18 | T object3(object1); 19 | 20 | 21 | // This is wher %rbp will return 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /backup/8/project/intArrayClass.hpp: -------------------------------------------------------------------------------- 1 | // @file intArrayClass.hpp 2 | #ifndef INTARRAY_HPP 3 | #define INTARRAY_HPP 4 | 5 | // Interface (the header file) 6 | class IntArray{ 7 | // By default all of our member variables and 8 | // member functions are private in a class 9 | int* m_storage; // Sometimes I prefix member variables 10 | int m_size; // with 'm_' to indicate they are member variables 11 | public: 12 | IntArray(int _size); 13 | ~IntArray(); 14 | IntArray(const IntArray& copy); 15 | IntArray& operator=(const IntArray& rhs); 16 | void Print(); 17 | int GetSize() const; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /backup/8/project/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // Compile with: g++ -std=c++17 intArrayClass.cpp -o prog 3 | // Run with : ./prog 4 | // 5 | // Compile for debugging with: 6 | // g++ -std=c++17 -g intArrayClass.cpp -o prog 7 | // Run with gdb: gdb ./prog --tui 8 | ///////////////////////////////////////////////// 9 | 10 | #include 11 | 12 | #include "intArrayClass.hpp" 13 | 14 | // Our project entry point 15 | // Entry point to program 'main' intArrayClass.cpp 16 | int main(){ 17 | IntArray a(20); 18 | // IntArray b =a ; 19 | IntArray b(10); 20 | b = a; 21 | std::cout << "m_size = " << a.GetSize(); // Access value for read only 22 | a.Print(); 23 | b.Print(); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /backup/9/hello.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // See preprocessor output: g++ -std=c++17 hello.cpp - 3 | // 4 | // Compile with: g++ -std=c++17 hello.cpp -o prog 5 | // Run with : ./prog 6 | // 7 | // Compile for debugging with: 8 | // g++ -std=c++17 -g hello.cpp -o prog 9 | // Run with gdb: gdb ./prog --tui 10 | ///////////////////////////////////////////////// 11 | 12 | // @file hello.cpp 13 | 14 | #include "hello.hpp" 15 | #include "hello.hpp" 16 | 17 | // Entry point to program 'main' hello.cpp 18 | int main(){ 19 | 20 | #ifdef TEST 21 | 2+2; 22 | #endif 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /backup/9/hello.hpp: -------------------------------------------------------------------------------- 1 | //#include 2 | #ifndef HELLO_HPP 3 | #define HELLO_HPP 4 | 5 | void foo(){ 6 | // std::cout << "hi" << std::endl; 7 | } 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /backup/9/project/main.cpp: -------------------------------------------------------------------------------- 1 | ///////////////////////////////////////////////// 2 | // See preprocessor output: g++ -std=c++17 main.cpp - 3 | // 4 | // Compile with: g++ -std=c++17 main.cpp -o prog 5 | // Run with : ./prog 6 | // 7 | // Compile for debugging with: 8 | // g++ -std=c++17 -g main.cpp -o prog 9 | // Run with gdb: gdb ./prog --tui 10 | ///////////////////////////////////////////////// 11 | 12 | // @file main.cpp 13 | #include 14 | #include "math.hpp" 15 | 16 | // Entry point to program 'main' main.cpp 17 | int main(){ 18 | 19 | std::cout << "add(7,2)=" << mike::add(7,2) << std::endl; 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /backup/9/project/math.cpp: -------------------------------------------------------------------------------- 1 | #include "math.hpp" 2 | 3 | namespace mike{ 4 | int add(int a, int b){ 5 | return a+b; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /backup/9/project/math.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MATH_HPP 2 | #define MATH_HPP 3 | // @file math.hpp 4 | 5 | // Our interface, for some math functions 6 | // to demonstrate linking 7 | namespace mike{ 8 | int add(int a, int b); 9 | } 10 | #endif 11 | -------------------------------------------------------------------------------- /project_samples/fallingsands/README.md: -------------------------------------------------------------------------------- 1 | The provided code from this example has a **segmentation fault**. Why? We are not doing any bounds checking within our 2D grid for where we are creating particles, or where they are moving (i.e. checking that a particle moving (x,y+1) is still in range.) 2 | 3 | How to run: `g++ ./src/*.cpp -I./include -o prog -lsfml-system -lsfml-graphics -lsfml-window` 4 | -------------------------------------------------------------------------------- /project_samples/fallingsands/include/Particle.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PARTICLE_HPP 2 | #define PARTICLE_HPP 3 | 4 | #include 5 | 6 | enum PARTICLE_TYPE {AIR,SAND,WATER,STONE}; 7 | 8 | class Particle{ 9 | sf::Vector2i m_position; 10 | PARTICLE_TYPE m_particletype; 11 | public: 12 | Particle(); 13 | ~Particle(); 14 | // Working with the particle 15 | PARTICLE_TYPE GetParticleType(); 16 | void SetParticleType(PARTICLE_TYPE type); 17 | sf::Vector2i GetPosition(); 18 | void SetPosition(int x, int y); 19 | 20 | void Draw(sf::RenderWindow* windowRef); 21 | }; 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /project_samples/fallingsands/include/World.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WORLD_HPP 2 | #define WORLD_HPP 3 | 4 | #include "Particle.hpp" 5 | // Standard libraries 6 | #include 7 | 8 | class World{ 9 | public: 10 | // Constructor 11 | World(sf::RenderWindow* w,int width, int height); 12 | // Destructor 13 | ~World(); 14 | // Add a new particle 15 | void AddParticle(Particle p); 16 | // Updating our world 17 | void Update(); 18 | // Draw the particles 19 | void Draw(); 20 | 21 | private: 22 | sf::RenderWindow* m_windowPtr; 23 | int m_width; 24 | int m_height; 25 | 26 | std::vector m_particles; 27 | 28 | PARTICLE_TYPE* m_grid; // Hold values for what particle occupies 29 | // a particular cell. 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /project_samples/fallingsands/src/Particle.cpp: -------------------------------------------------------------------------------- 1 | #include "Particle.hpp" 2 | 3 | Particle::Particle(){ 4 | } 5 | 6 | Particle::~Particle(){ 7 | 8 | } 9 | 10 | PARTICLE_TYPE Particle::GetParticleType(){ 11 | return m_particletype; 12 | } 13 | 14 | void Particle::SetParticleType(PARTICLE_TYPE type){ 15 | m_particletype = type; 16 | } 17 | 18 | sf::Vector2i Particle::GetPosition(){ 19 | return m_position; 20 | } 21 | 22 | void Particle::SetPosition(int x, int y){ 23 | m_position.x = x; 24 | m_position.y = y; 25 | // TODO: If your particles are falling through the world, 26 | // then you can try to set some 'boundary' for them. 27 | // This is an 'okay' way to do it. 28 | if(m_position.y >= 400){ 29 | m_position.y = 399; 30 | } 31 | } 32 | 33 | void Particle::Draw(sf::RenderWindow* windowRef){ 34 | // Create our shape 35 | sf::RectangleShape shape(sf::Vector2f(1.0f,1.0f)); 36 | // TODO: Implement as a switch 37 | if(m_particletype == PARTICLE_TYPE::AIR){ 38 | shape.setFillColor(sf::Color::Black); 39 | } 40 | if(m_particletype == PARTICLE_TYPE::SAND){ 41 | shape.setFillColor(sf::Color::White); 42 | } 43 | shape.setPosition(m_position.x,m_position.y); 44 | windowRef->draw(shape); 45 | } 46 | --------------------------------------------------------------------------------