├── .gitignore ├── .vscode └── tasks.json ├── 01-what-is-cpp ├── 1-memory-cpp-vs-java │ ├── Makefile │ ├── Memory.cpp │ ├── Memory.java │ ├── run-java │ └── show-memory ├── 1-time-cpp-vs-python │ ├── Makefile │ ├── count_permutations.cpp │ ├── count_permutations.py │ ├── output_containers.hpp │ ├── traveling_salesman.cpp │ └── traveling_salesman.py ├── 2-hello │ ├── Makefile │ ├── hello.cpp │ └── no_main.cpp ├── 3-namespace │ ├── Makefile │ ├── main.cpp │ ├── namespace.cpp │ └── namespace.hpp ├── 5-exceptions │ ├── Makefile │ ├── throw_a_string.cpp │ ├── throw_an_exception.cpp │ └── throw_an_object.cpp ├── 6-assertions │ ├── assert.cpp │ └── run-in-two-modes ├── 8-doctest │ ├── Factorial.cpp │ ├── Factorial.hpp │ ├── FactorialTest.cpp │ ├── Makefile │ ├── ReporterEmpty.cpp │ ├── ReporterGrader.cpp │ ├── TestMain.cpp │ └── doctest.h ├── 99-cpp20-test │ ├── Makefile │ ├── erase.cpp │ └── string.cpp ├── old │ ├── 0-generic-make │ │ ├── .depend │ │ ├── Makefile │ │ ├── greeting1.cpp │ │ ├── greeting2.cpp │ │ ├── link_error.cpp │ │ └── units │ │ │ ├── greeter.cpp │ │ │ └── greeter.hpp │ ├── 4-enum-cpp-vs-c │ │ ├── Makefile │ │ ├── enum.cpp │ │ └── enumclass.cpp │ └── 9-exit-status │ │ ├── return0.bash │ │ ├── return0.cpp │ │ ├── return111.cpp │ │ ├── return222.bash │ │ └── test-exit-status.bash ├── slides-1-cpp-vs-c-vs-java.odp ├── slides-1-cpp-vs-c-vs-java.pdf ├── text-1-cpp-vs-c-vs-java.odt └── text-1-cpp-vs-c-vs-java.pdf ├── 02-classes-constructors-destructors ├── 0-overloading │ ├── Makefile │ └── power.cpp ├── 1-inline-outline-files │ ├── Complex.cpp │ ├── Complex.hpp │ ├── Makefile │ ├── build.bash │ └── main.cpp ├── 1-inline-outline │ ├── Complex.cpp │ └── Makefile ├── 2-makefile │ ├── Circle.cpp │ ├── Circle.hpp │ ├── Makefile │ ├── Point.cpp │ ├── Point.hpp │ ├── Rectangle.cpp │ ├── Rectangle.hpp │ ├── Triangle.cpp │ ├── Triangle.hpp │ └── main.cpp ├── 3-static │ ├── Makefile │ ├── main.cpp │ ├── old │ │ └── points.bounds.cpp │ ├── points.index.cpp │ └── points.index.hpp ├── 4-constructors │ ├── Makefile │ ├── Point.cpp │ ├── Point.hpp │ ├── PointTest.cpp │ ├── PointTestWithArrays.cpp │ ├── WrongProgramWithoutConstructor.cpp │ └── default.cpp ├── 5-destructors │ ├── DestructorsAndScopes.cpp │ ├── Error1MemoryLeak.cpp │ ├── Error2DoubleDestruction.cpp │ ├── Makefile │ ├── old │ │ └── destructor-test.cpp │ ├── scope.cpp │ └── units │ │ ├── IntList.cpp │ │ ├── IntList.hpp │ │ └── IntList2D.hpp ├── 6-destruct-arrays │ ├── ArrayTest.cpp │ ├── Makefile │ └── units │ │ ├── IntList.cpp │ │ └── IntList.hpp ├── 7-valgrind │ ├── Makefile │ ├── a_minimal_program.cpp │ ├── a_new_without_delete.cpp │ ├── class_with_destructor.cpp │ └── class_without_destructor.cpp ├── 9-tidy │ ├── Makefile │ └── hello.cpp ├── slides-0-overloading.odp ├── slides-0-overloading.pdf ├── slides-1-classes.odp ├── slides-1-classes.pdf ├── slides-2-new-delete.odp ├── slides-2-new-delete.pdf ├── slides-3-scope.pdf ├── text-0-overloading.odt ├── text-0-overloading.pdf ├── text-1-classes.odt ├── text-1-classes.pdf ├── text-2-new-delete.odt └── text-2-new-delete.pdf ├── 03-composition-references ├── 0-composition │ ├── Line.class │ ├── Line.java │ ├── LineTest.class │ ├── LineTest.cpp │ ├── LineTest.java │ ├── Makefile │ ├── Point.class │ ├── Point.java │ ├── PointTest.cpp │ ├── run-java │ └── units │ │ ├── Line.cpp │ │ ├── Line.hpp │ │ ├── Point.cpp │ │ └── Point.hpp ├── 1-reference │ ├── Makefile │ ├── assign_a_reference.cpp │ ├── lvalue_rvalue_1.cpp │ ├── lvalue_rvalue_2.cpp │ └── optional │ │ ├── null-reference.cpp │ │ ├── reference-from-pointer.cpp │ │ └── reference-to-pointer.cpp ├── 2-reference-return │ ├── Complex.cpp │ ├── Makefile │ └── a_referece_as_return_value.cpp ├── 7-const-methods │ ├── Complex.cpp │ └── Makefile ├── 8-friend-functions │ ├── Makefile │ └── friend.cpp ├── 9-nsdmi │ └── nsdmi.h ├── slides-1-composition-initialization.odp ├── slides-1-composition-initialization.pdf ├── slides-2-reference.odp ├── slides-2-reference.pdf ├── slides-3-const-and-friend.odp ├── slides-3-const-and-friend.pdf ├── slides-9-nsdmi.pptx ├── text-1-composition-initialization.odt ├── text-1-composition-initialization.pdf ├── text-2-references.odt ├── text-2-references.pdf ├── text-3-const-and-friend.odt └── text-3-const-and-friend.pdf ├── 04-operator-overloading ├── 0-int-string-operators │ ├── IntStringDemo.cpp │ └── Makefile ├── 1-arithmetic-operators │ ├── Complex.cpp │ ├── Complex.hpp │ ├── ComplexDemo.cpp │ ├── Makefile │ ├── input.cpp │ └── inputs │ │ ├── ok1.txt │ │ ├── ok2.txt │ │ ├── w1.txt │ │ ├── w2.txt │ │ ├── w3.txt │ │ ├── w4.txt │ │ ├── w5.txt │ │ └── w6.txt ├── 2-brackets-operator │ ├── Demo.cpp │ ├── Makefile │ └── units │ │ ├── IntList.cpp │ │ └── IntList.hpp ├── 4-parentheses-operator │ ├── Makefile │ ├── old │ │ ├── myfunctor.old.cpp │ │ └── printfunctor.old.cpp │ └── polynomial.cpp ├── 8-suffix-operator │ ├── Makefile │ └── main.cpp ├── slides-2-operators.odp ├── slides-2-operators.pdf ├── slides-3-literals.pdf ├── slides-3-literals.pptx ├── text-2-operators.odt └── text-2-operators.pdf ├── 05-copy-convert ├── 1-copy-constructor │ ├── IntList_bad.cpp │ ├── IntList_good.cpp │ └── Makefile ├── 5-conversion operator │ ├── Fraction.cpp │ └── Makefile ├── old │ ├── Makefile │ ├── conv.cpp │ ├── conv_explicit.cpp │ └── convert_primitives.cpp ├── slides.odp ├── slides.pdf ├── text.odt └── text.pdf ├── 06-inheritance ├── 1-order-of-ctors-and-dtors │ ├── Makefile │ ├── inheritance1.cpp │ └── inheritance2.cpp ├── 4-pure-virtual │ ├── Makefile │ └── pure-virtual.cpp ├── old │ ├── 2-overriding │ │ ├── Makefile │ │ ├── hiding.cpp │ │ ├── static.cpp │ │ ├── virtual.cpp │ │ └── virtual_from_ctor.cpp │ └── 3-different-override-types │ │ ├── Makefile │ │ └── lesson9.cpp ├── slides-inheritance.odp ├── slides-inheritance.pdf ├── slides-virtual.odp ├── slides-virtual.pdf ├── text.odt └── text.pdf ├── 07-rtti ├── 1-redirection │ ├── Makefile │ ├── err.txt │ ├── out.txt │ ├── redirection.bash │ └── redirection.cpp ├── 3-manipulators-float-precision │ ├── Makefile │ ├── boolalpha.cpp │ └── precision.cpp ├── 4-image │ ├── .gitignore │ ├── Makefile │ ├── image1_struct.cpp │ ├── image2_ptr.cpp │ ├── image3_virtual.cpp │ └── old │ │ ├── image.simple.c │ │ └── image.simple.cpp ├── 5-casting │ ├── 1-reinterpret_cast.cpp │ ├── 2-static_cast.cpp │ ├── 3-dynamic_cast.cpp │ └── Makefile ├── 6-typeid │ ├── Makefile │ ├── typeid_compare.cpp │ └── typeid_print.cpp ├── old │ ├── 1-reinterpret_cast.cpp │ ├── 2-static_cast.cpp │ ├── 2-streams │ │ ├── Makefile │ │ ├── abc.txt │ │ └── streams.cpp │ ├── 3-dynamic_cast.cpp │ ├── binary_serialization │ │ ├── Makefile │ │ ├── random_image.hpp │ │ ├── test_binary_serialization.cpp │ │ └── write_read_utils.hpp │ ├── const_cast.cpp │ ├── precision.cpp │ ├── slides-1-rtti.odp │ ├── slides-1-rtti.pdf │ ├── slides-iostreams-full.odp │ ├── slides-iostreams-full.pdf │ └── stringify │ │ ├── Makefile │ │ ├── stringify.hpp │ │ └── test_stringify.cpp ├── slides-0-iostreams.odp ├── slides-0-iostreams.pdf ├── slides-1-rtti.odp ├── slides-1-rtti.pdf ├── text-0-iostreams.odt ├── text-0-iostreams.pdf ├── text-1-rtti.odt └── text-1-rtti.pdf ├── 08-templates-iterators ├── 1-functions │ ├── 1-swap.cpp │ ├── 2-sum.cpp │ ├── 3-pi.cpp │ └── Makefile ├── 2-Stack │ ├── Makefile │ ├── Stk.hpp │ ├── StkMain.cpp │ └── sum.cpp ├── slides-1-function-templates.odp ├── slides-1-function-templates.pdf ├── slides-2-class-templates-iterators.odp ├── slides-2-class-templates-iterators.pdf ├── text-1-function-templates.odt ├── text-1-function-templates.pdf ├── text-2-class-templates-iterators.odt └── text-2-class-templates-iterators.pdf ├── 09-specializations-metaprogramming ├── 1-variations │ ├── Makefile │ ├── double-args-pair.cpp │ ├── numeric-args-array.cpp │ └── numeric-args-default-value.cpp ├── 2-specialization-swap │ ├── Buffer.hpp │ ├── Makefile │ └── main.cpp ├── 3-specialization-classes │ ├── Makefile │ ├── simple-class-specialization.cpp │ └── vector_template.cpp ├── 4-specialization-type-info │ ├── Makefile │ └── is_numeric.cpp ├── 5-decltype │ ├── Makefile │ ├── decltype.cpp │ ├── return-type.cpp │ └── vector.cpp ├── 6-metaprogramming-derivative │ ├── Makefile │ ├── animate_demo.cpp │ ├── derivative.hpp │ ├── derivative_demo.cpp │ ├── func.ppm │ ├── functors_demo.cpp │ ├── rgb.hpp │ └── sincos.hpp ├── 7-metaprogramming-physics │ ├── Makefile │ └── mks.cpp ├── slides.odp ├── slides.pdf ├── text.odt └── text.pdf ├── 10-stl-containers ├── 0-tuples │ ├── Makefile │ └── tuple.cpp ├── 1-vectors │ ├── Makefile │ ├── arrays.cpp │ ├── push_vs_emplace.cpp │ └── vector_iterators.cpp ├── 2-basic-strings │ ├── Makefile │ └── basic_string.cpp ├── 3-sets │ ├── Makefile │ ├── comparators_int.cpp │ ├── comparators_point.cpp │ ├── loops.cpp │ └── set_without_operator_less_than.cpp ├── 4-maps │ ├── Makefile │ ├── find_.cpp │ ├── find_bounds.cpp │ └── loops.cpp ├── 8-type-traits │ ├── Makefile │ ├── is_integral.cpp │ ├── is_same.cpp │ └── vector_rvalue.cpp ├── 9-riddles │ ├── Makefile │ ├── input.txt │ └── riddles.cpp ├── old │ ├── 5-valarrays │ │ ├── Makefile │ │ └── valarray.cpp │ ├── 6-static-vector │ │ ├── Makefile │ │ └── static-vector.cpp │ └── comparators.old.cpp ├── slides-containers-looping.odp ├── slides-containers-looping.pdf ├── slides-containers.odp ├── slides-containers.pdf ├── text-containers.odt └── text-containers.pdf ├── 11-stl-algorithms ├── 01-io-iterators │ ├── Makefile │ ├── istream-iterator.cpp │ ├── ostream-iterator.cpp │ ├── output_containers.hpp │ └── vector-insert-iterator.cpp ├── 03-insert-erase │ ├── Makefile │ ├── insert_one.cpp │ ├── insert_range.cpp │ ├── loop-past-the-end.cpp │ ├── odd_elements_erase.cpp │ └── output_containers.hpp ├── 10-queries │ ├── Makefile │ ├── numeric_queries.cpp │ ├── output_containers.hpp │ ├── property_queries.cpp │ ├── search_queries.cpp │ └── two_range_queries.cpp ├── 11-set-algorithms │ ├── Makefile │ ├── file1.txt │ ├── file2.txt │ ├── output_containers.hpp │ ├── set_algorithms.cpp │ └── union_file.txt ├── 12-permutations │ ├── Makefile │ ├── heaps.cpp │ ├── output_containers.hpp │ ├── partition.cpp │ ├── permutations.cpp │ ├── rotate.cpp │ ├── shapley_value.cpp │ ├── shuffle.cpp │ └── sort.cpp ├── 13-movers │ ├── Makefile │ ├── copy.cpp │ ├── output_containers.hpp │ ├── sample.cpp │ └── swap.cpp ├── 14-value-modifiers │ ├── Makefile │ ├── fill_iota.cpp │ ├── generate_bind.cpp │ ├── output_containers.hpp │ └── replace.cpp ├── 15-runes │ ├── Makefile │ ├── X_copy.cpp │ ├── X_if.cpp │ ├── is_X.cpp │ ├── output_containers.hpp │ └── stable_X.cpp ├── 16-structure-changers │ ├── Makefile │ ├── output_containers.hpp │ ├── remove.cpp │ └── unique.cpp ├── 17-foreach-transform │ ├── Makefile │ ├── for_each.cpp │ ├── output_containers.hpp │ └── transform.cpp ├── old │ └── 02-buffer-with-iterators │ │ ├── Buffer.hpp │ │ ├── BufferTest.cpp │ │ └── Makefile ├── slides-iterators-validity.odp ├── slides-iterators-validity.pdf ├── slides-iterators.odp ├── slides-iterators.pdf ├── slides-stl-algorithms.odp ├── slides-stl-algorithms.pdf ├── text-iterators.odt ├── text-iterators.pdf ├── text-stl-algorithms.odt └── text-stl-algorithms.pdf ├── 12-smart-pointers ├── 1-musician │ ├── AutoPointer.hpp │ ├── Makefile │ ├── SharedPointer.hpp │ ├── UniquePointer.hpp │ ├── main.cpp │ ├── mainstd.cpp │ ├── musician.cpp │ ├── musician.hpp │ ├── old │ │ ├── smartPointer.cpp │ │ └── smartPointer.h │ └── tooNoisy.h ├── 2-move-rvalue-reference │ ├── Makefile │ ├── move_demo.cpp │ ├── move_demo_operators.cpp │ └── move_iterable.OLD.cpp ├── 3-stack │ ├── Makefile │ └── stack.cpp ├── 4-weak_ptr │ ├── Makefile │ └── employee-manager.cpp ├── 5-playground │ ├── Makefile │ └── playground1.cpp ├── slides-eran.odp ├── slides-eran.pdf ├── slides-eran.pptx ├── text.odt └── text.pdf ├── 13-python-integration ├── 0-prerequisites │ ├── Makefile │ └── string_view.cpp ├── 1-python-introduction │ ├── __pycache__ │ │ └── factorial.cpython-35.pyc │ ├── factorial.py │ ├── factorial_demo.py │ └── hello.py ├── 2-cppyy-introduction │ ├── hello.hpp │ ├── hello.py │ └── include.py ├── 3-permutations │ ├── Makefile │ ├── __pycache__ │ │ ├── count.cpython-35.pyc │ │ ├── count_permutations.cpython-35.pyc │ │ ├── traveling_salesman.cpython-35.pyc │ │ └── traveling_salesman.cpython-38.pyc │ ├── count_compare.py │ ├── count_permutations.cpp │ ├── count_permutations.py │ ├── demo.cpp │ ├── demo.py │ ├── output_containers.hpp │ ├── traveling_compare.py │ ├── traveling_salesman.cpp │ └── traveling_salesman.py ├── 4-cppyy-types │ ├── callers.py │ └── functions.hpp ├── __pycache__ │ └── factorial.cpython-35.pyc ├── etc │ ├── 4-mandelbrot │ │ ├── Makefile │ │ ├── mandelbrot.cpp │ │ └── mandelbrot.py │ └── 5-shapley-value │ │ ├── shapley_value.cpp │ │ ├── shapley_value.cppyy.py │ │ └── shapley_value.py ├── text.odt └── text.pdf ├── LICENSE ├── Makefile ├── README.md ├── grade-rules.md ├── late-submissions.md ├── old ├── homework-instructions.odt └── homework-instructions.pdf ├── preparations.md ├── syllabus.docx └── syllabus.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | # LibreOffice lock files 2 | *# 3 | 4 | # Prerequisites 5 | *.d 6 | 7 | # Compiled Object files 8 | *.slo 9 | *.lo 10 | *.o 11 | *.obj 12 | *.pyc 13 | *.class 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Compiled Dynamic libraries 20 | *.so 21 | *.dylib 22 | *.dll 23 | 24 | # Fortran module files 25 | *.mod 26 | *.smod 27 | 28 | # Compiled Static libraries 29 | *.lai 30 | *.la 31 | *.a 32 | *.lib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | demo 39 | test 40 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "shell", 5 | "label": "C/C++: clang++-9 build active file", 6 | "command": "/usr/bin/clang++-9", 7 | "args": [ 8 | "-g", 9 | "${file}", 10 | "-o", 11 | "${fileDirname}/${fileBasenameNoExtension}" 12 | ], 13 | "options": { 14 | "cwd": "${workspaceFolder}" 15 | }, 16 | "problemMatcher": [ 17 | "$gcc" 18 | ], 19 | "group": { 20 | "kind": "build", 21 | "isDefault": true 22 | } 23 | } 24 | ], 25 | "version": "2.0.0" 26 | } -------------------------------------------------------------------------------- /01-what-is-cpp/1-memory-cpp-vs-java/Makefile: -------------------------------------------------------------------------------- 1 | CXX=clang++-9 2 | CXXFLAGS=-std=c++2a 3 | 4 | all: 5 | $(CXX) $(CXXFLAGS) *.cpp 6 | ./a.out 7 | -------------------------------------------------------------------------------- /01-what-is-cpp/1-memory-cpp-vs-java/Memory.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates memory usage in C++. 3 | * @author erelsgl 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class Point { 12 | private: 13 | int x; 14 | int y; 15 | public: 16 | Point() { x = y = 5555; } 17 | }; 18 | 19 | int main() { 20 | const int KB = 1024; 21 | const int SIZE=50*1000*KB; 22 | 23 | cout << "Before new" << ' ' << endl; 24 | this_thread::sleep_for(chrono::seconds(3)); 25 | 26 | Point* p = new Point[SIZE]; 27 | for (int i=0; i 3 | #include 4 | using namespace std; 5 | 6 | int count_permutations(int N) { 7 | vector v(N); 8 | iota(v.begin(), v.end(), 1); // fill the vector with 1,...,N 9 | int count=0; 10 | do { 11 | ++count; 12 | } while ( next_permutation(v.begin(),v.end()) ); 13 | return count; 14 | } 15 | 16 | int main() { 17 | for (int N=1; N<14; ++N) { 18 | cout << "Permutations of 1.." << N << ":" << endl; 19 | auto begin = time(NULL); 20 | auto count = count_permutations(N); 21 | auto end = time(NULL); 22 | auto duration = end-begin; 23 | cout << " " << count << " permutations calculated in " << duration << " seconds" << endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /01-what-is-cpp/1-time-cpp-vs-python/count_permutations.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | import itertools 4 | import time 5 | 6 | def count_permutations(N:int)->int: 7 | count=0 8 | for p in itertools.permutations(range(1,N+1)): 9 | count += 1 10 | return count 11 | 12 | if __name__=="__main__": 13 | for N in range(1,13): 14 | print ("Permutations of 1..{}:".format(N), flush=True) 15 | start = time.time() 16 | count = count_permutations(N) 17 | end = time.time() 18 | duration_in_seconds = end-start 19 | print(" {} permutations calculated in {} seconds".format(count, duration_in_seconds), flush=True) 20 | 21 | -------------------------------------------------------------------------------- /01-what-is-cpp/2-hello/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | CXX=clang++-9 3 | CXXFLAGS=-std=c++2a 4 | 5 | all: 6 | $(CXX) $(CXXFLAGS) *.cpp -o main.exe 7 | ./main.exe 8 | 9 | tidy: 10 | clang-tidy *.cpp -checks=bugprone-*,clang-analyzer-*,cppcoreguidelines-*,hicpp-*,performance-*,portability-*,readability-* --warnings-as-errors=* -- 11 | 12 | tidycore: 13 | clang-tidy *.cpp -checks=cppcoreguidelines-* -- 14 | 15 | tidyread: 16 | clang-tidy *.cpp -checks=readability-* -- 17 | 18 | tidyhi: 19 | clang-tidy *.cpp -checks=hicpp-* -- 20 | -------------------------------------------------------------------------------- /01-what-is-cpp/2-hello/hello.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * This is a "hello world" program for the C++ course. 3 | */ 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | int main() { 10 | string name; 11 | cout << "What is your name? "; 12 | cin >> name; 13 | cout << "What is your age? "; 14 | int age; 15 | cin >> age; 16 | cout << endl << "Hello " << name << age << " !" << endl; 17 | cout << (1 << 5) << endl; 18 | const auto i = "abc"; 19 | //i = "def"; 20 | //i = 5; // error 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /01-what-is-cpp/2-hello/no_main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * This is a "hello world" program for the C++ course. 3 | */ 4 | 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | int main2() { 11 | string name; 12 | cout << "What is your name? "; 13 | cin >> name; 14 | cout << "What is your age? "; 15 | int age; 16 | cin >> age; 17 | cout << endl << "Hello " << name << age << " !" << endl; 18 | cout << (1 << 5) << endl; 19 | const auto i = "abc"; 20 | //i = "def"; 21 | //i = 5; // error 22 | return 0; 23 | } 24 | 25 | -------------------------------------------------------------------------------- /01-what-is-cpp/3-namespace/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | CXX=clang++-9 3 | CXXFLAGS=-std=c++2a 4 | 5 | ifndef MAIN 6 | MAIN=./main.cpp 7 | endif 8 | 9 | all: 10 | # $(CXX) $(CXXFLAGS) $(MAIN) 11 | $(CXX) $(CXXFLAGS) *.cpp 12 | ./a.out 13 | -------------------------------------------------------------------------------- /01-what-is-cpp/3-namespace/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "namespace.hpp" 3 | 4 | void printx() { 5 | std::cout << 5 << std::endl; 6 | } 7 | 8 | int main() { 9 | printx(); 10 | ::printx(); 11 | abc::printx(); 12 | } 13 | -------------------------------------------------------------------------------- /01-what-is-cpp/3-namespace/namespace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #include "namespace.hpp" 5 | 6 | namespace abc { 7 | void printx() { 8 | cout << 6 << endl; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /01-what-is-cpp/3-namespace/namespace.hpp: -------------------------------------------------------------------------------- 1 | 2 | namespace abc { 3 | void printx(); 4 | }; 5 | 6 | 7 | -------------------------------------------------------------------------------- /01-what-is-cpp/5-exceptions/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /01-what-is-cpp/5-exceptions/throw_a_string.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demostrates exceptions and assertions. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-03 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | 14 | // Throw a string: 15 | double safesqrt(double x) { 16 | if (x<0) 17 | throw string( 18 | "x must be non-negative, but it is " 19 | +to_string(x)); 20 | return std::sqrt(x); 21 | } 22 | 23 | 24 | int main() { 25 | cout << "std::sqrt(4) = " << std::sqrt(4) << endl; 26 | cout << "std::sqrt(-4) = " << std::sqrt(-4) << endl; 27 | try { 28 | cout << "safesqrt(4) = " << safesqrt(4) << endl; 29 | cout << "safesqrt(-4) = " << safesqrt(-4) << endl; 30 | } 31 | catch (string message) { 32 | cout << " caught exception: " << message << endl; 33 | } 34 | 35 | cout << safesqrt(-9) << endl; // uncaught exception 36 | cout << "Program finished successfully" << endl; 37 | } 38 | -------------------------------------------------------------------------------- /01-what-is-cpp/5-exceptions/throw_an_exception.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demostrates exceptions and assertions. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-03 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | // Throw a standard exception object: 15 | double safesqrt1(double x) { 16 | if (x<0) 17 | throw std::out_of_range{"x must be positive"}; 18 | double result = std::sqrt(x); 19 | return result; 20 | } 21 | 22 | int main() { 23 | cout << "std::sqrt(4) = " << std::sqrt(4) << endl; 24 | cout << "std::sqrt(-4) = " << std::sqrt(-4) << endl; 25 | try { 26 | cout << "safesqrt1(4) = " << safesqrt1(4) << endl; 27 | cout << "safesqrt1(-4) = " << safesqrt1(-4) << endl; 28 | } 29 | catch (string ex) { 30 | cout << "caught string" << endl; 31 | } 32 | catch (std::out_of_range ex) { 33 | cout << " caught exception: " << ex.what() << endl; 34 | } 35 | catch (std::exception ex) { 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /01-what-is-cpp/5-exceptions/throw_an_object.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demostrates exceptions and assertions. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-03 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | // Throw a custom object: 14 | class argument_must_be_positive {}; 15 | 16 | double safesqrt2(double x) { 17 | if (x<0) 18 | throw argument_must_be_positive{}; 19 | double result = std::sqrt(x); 20 | return result; 21 | } 22 | 23 | int main() { 24 | cout << "std::sqrt(4) = " << std::sqrt(4) << endl; 25 | cout << "std::sqrt(-4) = " << std::sqrt(-4) << endl; 26 | try { 27 | cout << "safesqrt2(4) = " << safesqrt2(4) << endl; 28 | cout << "safesqrt2(-4) = " << safesqrt2(-4) << endl; 29 | } 30 | catch (...) { 31 | auto exception = std::current_exception(); 32 | cout << " caught exception of type " << exception.__cxa_exception_type()->name() << endl; 33 | } 34 | cout << safesqrt2(-9) << endl; // uncaught exception 35 | } 36 | -------------------------------------------------------------------------------- /01-what-is-cpp/6-assertions/assert.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demostrates exceptions and assertions. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-03 5 | */ 6 | 7 | // To disable the assertions: 8 | // Either uncomment this line: 9 | // #define NDEBUG 10 | // Or Compile with the flag -DNDEBUG 11 | 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | double sqrtWithBug(double x) { 20 | double result = -std::sqrt(x); 21 | assert (result>=0); 22 | return result; 23 | } 24 | 25 | int main() { 26 | cout << "std::sqrt(4) " << std::sqrt(4) << endl; 27 | cout << "sqrtWithBug(4) " << sqrtWithBug(4) << endl; 28 | } 29 | -------------------------------------------------------------------------------- /01-what-is-cpp/6-assertions/run-in-two-modes: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | printf "\\nDEBUG MODE:\n\n" 4 | clang++-9 --std=c++2a assert.cpp 5 | ./a.out 6 | 7 | printf "\\nNO-DEBUG MODE:\n\n" 8 | clang++-9 --std=c++2a -DNDEBUG assert.cpp 9 | ./a.out 10 | 11 | rm -f a.out 12 | -------------------------------------------------------------------------------- /01-what-is-cpp/8-doctest/Factorial.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* Write during class, to demonstrate test-driven development */ 3 | -------------------------------------------------------------------------------- /01-what-is-cpp/8-doctest/Factorial.hpp: -------------------------------------------------------------------------------- 1 | int factorial(int number); 2 | -------------------------------------------------------------------------------- /01-what-is-cpp/8-doctest/FactorialTest.cpp: -------------------------------------------------------------------------------- 1 | #include "doctest.h" 2 | #include "Factorial.hpp" 3 | #include 4 | 5 | TEST_CASE("Factorials of small numbers") { 6 | CHECK(factorial(0) == 1); 7 | CHECK(factorial(1) == 1); 8 | CHECK(factorial(2) == 2); 9 | CHECK(factorial(3) == 6); 10 | } 11 | 12 | 13 | TEST_CASE("Factorials of large numbers") { 14 | CHECK(factorial(4) == 24); 15 | CHECK(factorial(6) == 720); 16 | CHECK(factorial(10) == 3628800); 17 | 18 | // auto f3 = factorial(3); 19 | // CHECK((f3==5 || f3==6)); // logic OR requires parentheses! 20 | } 21 | 22 | 23 | TEST_CASE("Factorials of negative numbers") { 24 | CHECK_THROWS(factorial(-1)); // check that some exception is thrown 25 | CHECK_THROWS_AS(factorial(-2), std::out_of_range); // check that a specific exception type is thrown 26 | CHECK_THROWS_AS(factorial(-2), std::exception); // check that a specific exception type (or a descendant) is thrown 27 | } 28 | 29 | 30 | /* add more test cases here */ 31 | -------------------------------------------------------------------------------- /01-what-is-cpp/8-doctest/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | CXX=clang++-9 3 | CXXFLAGS=-std=c++2a 4 | 5 | all: 6 | $(CXX) $(CXXFLAGS) *.cpp -o main.exe 7 | ./main.exe 8 | -------------------------------------------------------------------------------- /01-what-is-cpp/8-doctest/TestMain.cpp: -------------------------------------------------------------------------------- 1 | #define DOCTEST_CONFIG_IMPLEMENT 2 | #include "doctest.h" 3 | 4 | int main(int argc, char** argv) { 5 | /* See here https://github.com/onqtam/doctest/blob/master/doc/markdown/main.md for more options */ 6 | doctest::Context context; 7 | context.addFilter("reporters", "console"); // options: "console", "xml", "empty", "grader" 8 | //context.addFilter("reporters", "grader"); // options: "console", "xml", "empty", "grader" 9 | context.run(); // returns 0 if all tests passed; otherwise returns 1. 10 | } 11 | -------------------------------------------------------------------------------- /01-what-is-cpp/99-cpp20-test/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out 25 | -------------------------------------------------------------------------------- /01-what-is-cpp/99-cpp20-test/erase.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Testing the std::erase function, which was added in C++ 20. 3 | * Date: 2021-03 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | string s = "a b c d "; 13 | std::erase(s, ' '); // c++20 14 | cout << s << endl; 15 | 16 | s = "a b c d "; 17 | s.erase(remove(s.begin(),s.end(), ' '), s.end()); // c++11 18 | cout << s << endl; 19 | 20 | // s = "a b c d "; 21 | // s.erase(' '); // run-time error 22 | // cout << s << endl; 23 | } 24 | -------------------------------------------------------------------------------- /01-what-is-cpp/99-cpp20-test/string.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | const string foo() {return string();} 7 | 8 | int main() { 9 | const string a = foo(); 10 | string b = foo(); 11 | const string& c = foo(); 12 | // string& d = foo(); 13 | } 14 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/0-generic-make/.depend: -------------------------------------------------------------------------------- 1 | greeter.o: units/greeter.cpp units/greeter.hpp 2 | link_error.o: \ 3 | /mnt/d/Dropbox/ariel/cpp-5780/01-what-is-cpp/0-generic-make/link_error.cpp 4 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/0-generic-make/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running simple C++ projects. 2 | # 3 | # USAGE: 4 | # 1. Put all your non-main files in subfolder "units". 5 | # 2. Run: make MAIN=.cpp 6 | # It should create a file ".depend" with all required dependencies, 7 | # compile all the cpp files in "units", link them with your main program, and run it. 8 | # 9 | # Based on https://stackoverflow.com/a/2481326/827927 10 | # AUTHOR: Erel Segal-Halevi 11 | 12 | CXX=clang++-9 13 | CXXFLAGS=-std=c++2a 14 | RM=rm -rf 15 | 16 | ifndef MAIN 17 | MAIN=./main.cpp 18 | endif 19 | 20 | SOURCES=$(shell ls units/*.cpp) $(MAIN) 21 | OBJECTS=$(subst .cpp,.o,$(SOURCES)) 22 | MAINEXECUTABLE=a.out 23 | 24 | all: $(MAINEXECUTABLE) 25 | ./$(MAINEXECUTABLE) 26 | 27 | $(MAINEXECUTABLE): $(OBJECTS) 28 | $(CXX) $(CXXFLAGS) $(OBJECTS) 29 | 30 | 31 | depend: .depend 32 | 33 | .depend: $(SOURCES) 34 | $(RM) ./.depend 35 | $(CXX) $(CXXFLAGS) -MM $^>>./.depend; 36 | 37 | include .depend 38 | 39 | ################# 40 | 41 | clean: 42 | $(RM) *.o $(MAINEXECUTABLE) 43 | 44 | distclean: clean 45 | $(RM) *~ .depend 46 | 47 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/0-generic-make/greeting1.cpp: -------------------------------------------------------------------------------- 1 | #include "units/greeter.hpp" 2 | 3 | int main(int argc, char* argv[]) { 4 | Greeter g("ABC"); 5 | g.greet(); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/0-generic-make/greeting2.cpp: -------------------------------------------------------------------------------- 1 | #include "units/greeter.hpp" 2 | 3 | int main(int argc, char* argv[]) { 4 | Greeter g("DEFG"); 5 | g.greet(); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/0-generic-make/link_error.cpp: -------------------------------------------------------------------------------- 1 | int f(); 2 | 3 | int main() { 4 | f(); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/0-generic-make/units/greeter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "greeter.hpp" 3 | 4 | void Greeter::greet() { 5 | std::cout << "Hello, " << name << ", your name has " << getNameLength() << " chars." << std::endl; 6 | } 7 | 8 | int Greeter::getNameLength() { 9 | return name.length(); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/0-generic-make/units/greeter.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #if defined(DLL_EXPORT) 7 | #define DECLSPEC __declspec(dllexport) 8 | #else 9 | #define DECLSPEC 10 | #endif 11 | 12 | 13 | class DECLSPEC Greeter { 14 | public: 15 | Greeter(std::string name_) : name(name_) {}; 16 | Greeter() : name("World") {}; 17 | void greet(); 18 | int getNameLength(); 19 | private: 20 | std::string name; 21 | }; 22 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/4-enum-cpp-vs-c/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | CXX=clang++-9 3 | CXXFLAGS=-std=c++2a 4 | 5 | ifndef MAIN 6 | MAIN=./enumclass.cpp 7 | endif 8 | 9 | all: 10 | $(CXX) $(CXXFLAGS) $(MAIN) 11 | ./a.out 12 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/4-enum-cpp-vs-c/enum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * A demo of enums in C and C++ 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | enum Season { 9 | WINTER, // = 0 by default 10 | SPRING, // = WINTER + 1 11 | SUMMER, // = WINTER + 2 12 | AUTUMN // = WINTER + 3 13 | }; 14 | 15 | int WINTER = 60; // won't compile - redefinition 16 | 17 | int main() { 18 | cout << WINTER; 19 | int curr_season = WINTER; 20 | curr_season += 50; 21 | cout << curr_season << endl; 22 | cout << (curr_season==0) << endl; 23 | } 24 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/4-enum-cpp-vs-c/enumclass.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * A demo of enums in C and C++ 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | enum class Season { 9 | WINTER, // = 0 by default 10 | SPRING, // = WINTER + 1 11 | SUMMER, // = WINTER + 2 12 | AUTUMN // = WINTER + 3 13 | }; 14 | 15 | int WINTER = 60; 16 | 17 | int main() { 18 | cout << WINTER << endl; 19 | cout << int(Season::WINTER) << endl; 20 | Season curr_season = Season::WINTER; 21 | string name[4] {"winter", "spring", "summer", "autumn"}; 22 | cout << name[int(curr_season)]; 23 | //int curr_season = SUMMER; // won't compile 24 | //int curr_season = Season::SUMMER; // won't compile 25 | //Season curr_season = Season::WINTER; 26 | // curr_season += 50; // won't compile 27 | //int prev_season = Season::SUMMER; // won't compile 28 | //cout << curr_season << endl; // won't compile 29 | //cout << int(curr_season) << endl; 30 | //cout << (curr_season==0) << endl; // won't compile 31 | //cout << (curr_season==Season::WINTER) << endl; 32 | } 33 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/9-exit-status/return0.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "I return 0" 4 | exit 0 5 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/9-exit-status/return0.cpp: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0; 3 | } 4 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/9-exit-status/return111.cpp: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 111; 3 | } 4 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/9-exit-status/return222.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | echo "I return 222!!" 4 | exit 222 5 | -------------------------------------------------------------------------------- /01-what-is-cpp/old/9-exit-status/test-exit-status.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | clang++-9 return0.cpp; ./a.out 4 | echo "Return value $?" 5 | 6 | clang++-9 return111.cpp; ./a.out 7 | echo "Return value $?" 8 | 9 | ./return0.bash 10 | echo "Return value $?" 11 | 12 | ./return222.bash 13 | echo "Return value $?" 14 | 15 | -------------------------------------------------------------------------------- /01-what-is-cpp/slides-1-cpp-vs-c-vs-java.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/01-what-is-cpp/slides-1-cpp-vs-c-vs-java.odp -------------------------------------------------------------------------------- /01-what-is-cpp/slides-1-cpp-vs-c-vs-java.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/01-what-is-cpp/slides-1-cpp-vs-c-vs-java.pdf -------------------------------------------------------------------------------- /01-what-is-cpp/text-1-cpp-vs-c-vs-java.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/01-what-is-cpp/text-1-cpp-vs-c-vs-java.odt -------------------------------------------------------------------------------- /01-what-is-cpp/text-1-cpp-vs-c-vs-java.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/01-what-is-cpp/text-1-cpp-vs-c-vs-java.pdf -------------------------------------------------------------------------------- /02-classes-constructors-destructors/0-overloading/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | 3 | CXX=clang++-9 4 | CXXFLAGS=-std=c++2a 5 | 6 | ifndef MAIN 7 | MAIN=./power.cpp 8 | endif 9 | 10 | build: 11 | $(CXX) $(CXXFLAGS) $(MAIN) 12 | ./a.out 13 | 14 | build_safe: # protect against accidental conversion of a negative number to a large positive number 15 | $(CXX) $(CXXFLAGS) -Wsign-conversion -Werror $(MAIN) 16 | ./a.out 17 | 18 | tidy: 19 | clang-tidy *.cpp -checks=*,-modernize-*,-*-magic-* -- 20 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/0-overloading/power.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * A demo of function overloading in C++. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-03 5 | */ 6 | 7 | #include /* log,exp */ 8 | #include 9 | using std::cout, std::endl; 10 | 11 | int power(int a, unsigned int b) { 12 | cout << " power of uints" << endl; 13 | //cout << b; 14 | return b==0? 1: a*power(a,b-1); 15 | } 16 | 17 | double power(double a, double b) { 18 | cout << " power of reals" << endl; 19 | return exp(b*log(a)); 20 | } 21 | 22 | 23 | int main() { 24 | /* Reminder for signed/unsigned * 25 | signed int si = 32767; // equivalent to: short si = 32767 26 | cout << si << endl; 27 | si++; 28 | cout << si << endl; 29 | 30 | unsigned int ui = 0; 31 | cout << ui << endl; 32 | ui--; 33 | cout << ui << endl; 34 | */ 35 | 36 | cout << power(2, 3) << endl; 37 | cout << power(4.0, 0.5) << endl; 38 | //cout << power(2, 3.5) << endl; // ??? 39 | // cout << power(2.0,3.5) << endl; 40 | // cout << power(2, (int)3.5) << endl; // ??? 41 | // cout << power(2, -3) << endl; // ??? 42 | } 43 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/1-inline-outline-files/Complex.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Implementation file of class "Complex" 3 | */ 4 | 5 | #include "Complex.hpp" 6 | 7 | // "outline" constructor implementation: 8 | Complex::Complex(double re, double im) { 9 | (*this).re = re; 10 | this->im = im; 11 | } 12 | 13 | 14 | // "outline" method implementation: 15 | Complex Complex::sum(Complex b) { 16 | return Complex(re+b.re, im+b.im); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/1-inline-outline-files/Complex.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Header file of class "Complex" 3 | */ 4 | 5 | #include 6 | using namespace std; 7 | 8 | class Complex { 9 | double re, im; 10 | 11 | public: 12 | Complex() { re = im = 999; } // inline constructor 13 | Complex(double re, double im); // "outline" constructor 14 | 15 | string to_string() { // inline method 16 | return std::to_string(re)+"+"+std::to_string(im)+"i"; 17 | } 18 | Complex sum(Complex b); // "outline" method 19 | }; 20 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/1-inline-outline-files/Makefile: -------------------------------------------------------------------------------- 1 | CXX=clang++-9 2 | CXXFLAGS=-std=c++2a 3 | 4 | all: 5 | $(CXX) $(CXXFLAGS) *.cpp 6 | ./a.out 7 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/1-inline-outline-files/build.bash: -------------------------------------------------------------------------------- 1 | #!bash 2 | clang++-9 -std=c++2a --compile Complex.cpp -o Complex.o 3 | clang++-9 -std=c++2a --compile main.cpp -o main.o 4 | clang++-9 -std=c++2a Complex.o main.o 5 | ./a.out 6 | 7 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/1-inline-outline-files/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Main file that uses the class Complex 3 | */ 4 | 5 | #include "Complex.hpp" 6 | 7 | int main() { 8 | Complex a(1,2); 9 | cout << "a = " << a.to_string() << endl; 10 | 11 | Complex b(3,4); 12 | cout << "b = " << b.to_string() << endl; 13 | 14 | Complex c = a.sum(b); 15 | cout << "c = " << c.to_string() << endl; 16 | 17 | c = c.sum(b); 18 | cout << "c = " << c.to_string() << endl; 19 | 20 | Complex d; 21 | cout << "d = " << d.to_string() << endl; 22 | } 23 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/1-inline-outline/Makefile: -------------------------------------------------------------------------------- 1 | CXX=clang++-9 2 | CXXFLAGS=-std=c++2a 3 | 4 | all: 5 | $(CXX) $(CXXFLAGS) *.cpp 6 | ./a.out 7 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Circle.cpp: -------------------------------------------------------------------------------- 1 | #include "Circle.hpp" 2 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Circle.hpp: -------------------------------------------------------------------------------- 1 | // This file demonstrates the importance of "pragma once" in Point.hpp: 2 | #include "Point.hpp" 3 | 4 | class Circle { 5 | Point center; 6 | float radius; 7 | }; 8 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | 3 | CXX=clang++-9 4 | CXXFLAGS=-std=c++2a 5 | 6 | all: a.out 7 | ./a.out 8 | 9 | a.out: main.o Point.o Rectangle.o Triangle.o Circle.o 10 | $(CXX) $(CXXFLAGS) Point.o main.o Rectangle.o Triangle.o Circle.o 11 | 12 | Point.o: Point.cpp Point.hpp 13 | $(CXX) $(CXXFLAGS) --compile Point.cpp -o Point.o 14 | 15 | Rectangle.o: Rectangle.cpp Rectangle.hpp Point.hpp 16 | $(CXX) $(CXXFLAGS) --compile Rectangle.cpp -o Rectangle.o 17 | 18 | Circle.o: Circle.cpp Circle.hpp Point.hpp 19 | $(CXX) $(CXXFLAGS) --compile Circle.cpp -o Circle.o 20 | 21 | Triangle.o: Triangle.cpp Triangle.hpp Point.hpp 22 | $(CXX) $(CXXFLAGS) --compile Triangle.cpp -o Triangle.o 23 | 24 | main.o: main.cpp Point.hpp Rectangle.hpp Circle.hpp Triangle.hpp 25 | $(CXX) $(CXXFLAGS) --compile main.cpp -o main.o 26 | 27 | clean: 28 | rm -f *.o a.out 29 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Point.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.hpp" 2 | 3 | int f(int x) { 4 | return x+4; 5 | } 6 | 7 | 8 | void Point::setX(int newX) { 9 | x = newX; 10 | } 11 | 12 | 13 | void Point::setY(int newY) { 14 | y = newY; 15 | } 16 | 17 | string Point::to_string() { 18 | return "{"+std::to_string(x)+","+std::to_string(y)+"}"; 19 | } 20 | 21 | 22 | Point::Point(int x, int y) { 23 | cout << "Point[int,int]" << endl; 24 | this->x = x; 25 | this->y = y; 26 | } 27 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Point.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | class Point { 7 | private: // Not required. All is private by default. 8 | int x; 9 | int y; 10 | 11 | public: 12 | // methods defined inline: 13 | int getX() { return x; } 14 | int getY() { return y; } 15 | 16 | // methods defined in the CPP file: 17 | void setX(int); 18 | void setY(int); 19 | 20 | string to_string(); 21 | 22 | // // Constructors: 23 | Point() { 24 | cout << "Point()" << endl; 25 | x = 88; y = 12; 26 | } 27 | Point(int z) { 28 | cout << "Point(int)" << endl; 29 | x = y = z; 30 | } 31 | Point(int, int); 32 | }; 33 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Rectangle.cpp: -------------------------------------------------------------------------------- 1 | #include "Rectangle.hpp" 2 | 3 | string Rectangle::to_string() 4 | { 5 | return "Rectangle[" + topleft.to_string() + " , " + bottomright.to_string() + "]"; 6 | } -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Rectangle.hpp: -------------------------------------------------------------------------------- 1 | // This file demonstrates the importance of "pragma once" in Point.hpp: 2 | #include "Point.hpp" 3 | 4 | class Rectangle { 5 | Point topleft, bottomright; 6 | 7 | public: 8 | string to_string(); 9 | }; 10 | 11 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Triangle.cpp: -------------------------------------------------------------------------------- 1 | #include "Triangle.hpp" 2 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/Triangle.hpp: -------------------------------------------------------------------------------- 1 | // This file demonstrates the importance of "pragma once" in Point.hpp: 2 | #include "Point.hpp" 3 | 4 | class Triangle { 5 | Point a, b, c; 6 | }; 7 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/2-makefile/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.hpp" 2 | 3 | 4 | // The following line demonstrates the importance of the "pragma once" in Point.hpp: 5 | #include "Rectangle.hpp" 6 | #include "Circle.hpp" 7 | #include "Triangle.hpp" 8 | 9 | #include 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | Point p1; 15 | // // Calls no-arg constructor IF EXISTS. 16 | // // If there is NO constructor, VALUE IS UNDEFINED. 17 | // // If there is NO no-arg constructor: COMPILATION ERROR. 18 | cout << "p1 = " << p1.to_string() << endl; 19 | 20 | p1.setX(10); 21 | p1.setY(20); 22 | cout << "p1 = " << p1.to_string() << endl; 23 | 24 | Point p2 {11,21}; 25 | cout << "p2 = " << p2.to_string() << endl; 26 | 27 | Rectangle r; 28 | cout << "r = " << r.to_string() << endl; 29 | 30 | // //Point p3(); 31 | // //cout << p3.to_string() << endl; 32 | // // Compilation error! Empty parentheses interpreted as a function declaration! 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/3-static/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | 8 | RM=rm -f 9 | 10 | ifndef MAIN 11 | MAIN=./main.cpp 12 | endif 13 | 14 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 15 | 16 | SOURCES=$(MAIN) points.index.cpp 17 | 18 | all: $(MAINEXECUTABLE) 19 | $(MAINEXECUTABLE) 20 | 21 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 22 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 23 | 24 | clean: 25 | $(RM) *.exe a.out *.class 26 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/3-static/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Demonstrates static members. 4 | * Creates a class "point" with static members for giving a unique index to each object. 5 | */ 6 | 7 | #include "points.index.hpp" 8 | 9 | int main() { 10 | //Point::to_string(); 11 | Point p1; 12 | cout << "p1 = " << p1.to_string() << endl; 13 | 14 | Point p2; 15 | p2.setX(10); 16 | p2.setY(20); 17 | cout << "p2 = " << p2.to_string() << endl; 18 | 19 | cout << "Maximum values: " << endl; 20 | cout << Point::MAXX << "," << Point::MAXY << endl; 21 | Point::showMax(); 22 | 23 | // p1.setX(2000); // exception 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/3-static/points.index.cpp: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Demonstrates static members. 4 | * Creates a class "point" with static members for giving a unique index to each object. 5 | */ 6 | 7 | #include "points.index.hpp" 8 | 9 | // Initialize the static member: 10 | int Point::nextPointIndex = 0; 11 | 12 | Point::Point() { 13 | //nextPointIndex=0; // logic error 14 | this->index = nextPointIndex; 15 | nextPointIndex++; 16 | } 17 | 18 | void Point::setX(int newX) { 19 | if (newX>MAXX) 20 | throw out_of_range("New x is too large! The maximum is "+std::to_string(MAXX)); 21 | //x is equivalent to this->x; 22 | x = newX; 23 | } 24 | 25 | void Point::setY(int newY) { 26 | if (newY>MAXY) 27 | throw out_of_range("New y is too large! The maximum is "+std::to_string(MAXY)); 28 | y = newY; 29 | } 30 | 31 | string Point::to_string() { 32 | return "point #"+std::to_string(index)+": ("+std::to_string(x)+","+std::to_string(y)+")"; 33 | } 34 | 35 | void Point::showMax() { 36 | //cout << this << endl; // compilation error: no this 37 | cout << MAXX << "," << MAXY << endl; 38 | } 39 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/3-static/points.index.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates static members. 3 | * Creates a class "point" with static members for giving a unique index to each object. 4 | */ 5 | 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | class Point { 11 | int x; 12 | int y; 13 | int index; 14 | 15 | // A non-static const must be initialized out-of-line: 16 | static int nextPointIndex; 17 | 18 | public: 19 | Point(); 20 | 21 | static const int MAXX=1366; 22 | static const int MAXY=768; 23 | 24 | void setX(int newX); 25 | void setY(int newY); 26 | string to_string(); 27 | static void showMax(); 28 | }; 29 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/4-constructors/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | 8 | RM=rm -f 9 | 10 | ifndef MAIN 11 | MAIN=./main.cpp 12 | endif 13 | 14 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 15 | 16 | SOURCES=$(MAIN) Point.cpp 17 | 18 | all: $(MAINEXECUTABLE) 19 | $(MAINEXECUTABLE) 20 | 21 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 22 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 23 | 24 | clean: 25 | $(RM) *.exe a.out *.class 26 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/4-constructors/Point.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.hpp" 2 | using namespace std; 3 | 4 | void Point::setX(int newX) { 5 | x = newX; 6 | } 7 | 8 | void Point::setY(int newY) { 9 | y = newY; 10 | } 11 | 12 | string Point::to_string() { 13 | return "["+std::to_string(x)+","+std::to_string(y)+"]"; 14 | } 15 | 16 | 17 | Point::Point(int x, int y) { 18 | cout << "Point(int,int)" << endl; 19 | this->x = x; 20 | this->y = y; 21 | } 22 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/4-constructors/Point.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | class Point { 6 | private: // Not required. All is private by default. 7 | int x; 8 | int y; 9 | 10 | public: 11 | // methods defined inline: 12 | int getX() { return x; } 13 | int getY() { return y; } 14 | 15 | // methods defined in the CPP file: 16 | void setX(int); 17 | void setY(int); 18 | 19 | std::string to_string(); 20 | 21 | // // Constructors: 22 | Point() { 23 | std::cout << "Point()" << std::endl; 24 | x = 88; y = 12; 25 | } 26 | Point(int z) { 27 | std::cout << "Point(int)" << std::endl; 28 | x = y = z; 29 | } 30 | Point(int, int); 31 | }; 32 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/4-constructors/PointTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.hpp" 2 | 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main() { 8 | Point p1; 9 | // // Calls no-arg constructor IF EXISTS. 10 | // // If there is NO constructor, VALUE IS UNDEFINED. 11 | // // If there is NO no-arg constructor: COMPILATION ERROR. 12 | cout << "p1 = " << p1.to_string() << endl; 13 | 14 | p1.setX(10); 15 | p1.setY(20); 16 | cout << "p1 = " << p1.to_string() << endl; 17 | 18 | Point p2 {11,21}; 19 | cout << "p2 = " << p2.to_string() << endl; 20 | 21 | cout << "Point{7} = " << Point{7}.to_string() << endl; 22 | 23 | // //Point p3(); 24 | // //cout << p3.to_string() << endl; 25 | // // Compilation error! Empty parentheses interpreted as a function declaration! 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/4-constructors/WrongProgramWithoutConstructor.cpp: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * This program demonstrates what happens when a class does not have a default constructor. 4 | */ 5 | 6 | 7 | struct A { 8 | public: 9 | A(int) {} 10 | A() = default; 11 | //A(const A&) = default; 12 | //A& operator=(const A&) = default; 13 | //A(int,int) = default; 14 | }; 15 | 16 | 17 | int main() { 18 | A a; 19 | } 20 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/4-constructors/default.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | struct Test { 6 | int a; 7 | char b; 8 | string c; 9 | }; 10 | 11 | class Test2 { 12 | public: 13 | int a; 14 | char b; 15 | string c; 16 | int* d; 17 | }; 18 | 19 | 20 | int main () { 21 | Test2 t{5, 'c', "abc"}; 22 | //Test2 t{"abc", 5, 'c'}; 23 | Test2 t2{5, 'c'}; 24 | // *(t2.d) = 5; 25 | // Test t{5, 'c', "abc", 55}; 26 | } -------------------------------------------------------------------------------- /02-classes-constructors-destructors/5-destructors/Error2DoubleDestruction.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates what happens when an object is destructed twice. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-03 6 | */ 7 | #include "units/IntList.hpp" 8 | 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | cout << "{" << endl; 14 | { 15 | IntList list0(1000); 16 | list0.fill(99); 17 | list0.print(); 18 | cout << "l0.get(5) = " << list0.get(5) << endl; 19 | //list0.~IntList(); 20 | } 21 | cout << "}" << endl; 22 | 23 | cout << "{" << endl; 24 | { 25 | IntList l1(2000); 26 | l1.print(); 27 | l1.fill(999); 28 | cout << "l1.get(5) = " << l1.get(5) << endl; 29 | { 30 | IntList l2 = l1; 31 | l2.print(); 32 | cout << "l2.get(5) = " << l2.get(5) << endl; 33 | } 34 | cout << "l1.get(5) = " << l1.get(5) << endl; 35 | l1.print(); 36 | } 37 | cout << "}" << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/5-destructors/Makefile: -------------------------------------------------------------------------------- 1 | #!make all 2 | # 3 | # A generic makefile for running simple C++ projects. 4 | # 5 | # USAGE: 6 | # 1. Put all your non-main files in subfolder "units". 7 | # 2. Run: make MAIN=.cpp 8 | # It should compile all the cpp files in "units", link them with your main program, and run it. 9 | # 10 | # Based on https://stackoverflow.com/a/2481326/827927 11 | # AUTHOR: Erel Segal-Halevi 12 | 13 | CXX=clang++-9 14 | CXXFLAGS=-std=c++2a 15 | 16 | RM=rm -f 17 | 18 | ifndef MAIN 19 | MAIN=./main.cpp 20 | endif 21 | 22 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 23 | 24 | SOURCES=$(shell ls units/*.cpp) $(MAIN) 25 | HEADERS=$(shell ls units/*.h*) 26 | 27 | all: $(MAINEXECUTABLE) 28 | $(MAINEXECUTABLE) 29 | 30 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 31 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 32 | 33 | valgrind: 34 | valgrind --leak-check=full -v ./$(MAINEXECUTABLE) 35 | 36 | clean: 37 | $(RM) *.exe a.out 38 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/5-destructors/old/destructor-test.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates the time in which a destructor is called. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-03 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | class Test { 11 | public: 12 | int x; 13 | Test(int x) {this->x=x; cout<<"c "< 10 | using namespace std; 11 | 12 | int j=1111; 13 | 14 | int main() { 15 | int i = 1111; 16 | int j = i+2222; 17 | cout << "j = " << j << endl; 18 | { 19 | int j = i+3333; 20 | cout << "j = " << j << endl; 21 | int k = i+4444; 22 | } 23 | cout << "j = " << j << endl; 24 | //cout << k << endl; // Error: k is not defined 25 | } 26 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/5-destructors/units/IntList.cpp: -------------------------------------------------------------------------------- 1 | #include "IntList.hpp" 2 | 3 | void IntList::fill(int value) { 4 | for (uint i=0; inumInts; ++i) 5 | theInts[i] = value; 6 | } 7 | 8 | int IntList::get(uint index) { 9 | return theInts[index]; 10 | } 11 | 12 | void IntList::put(uint index, int value) { 13 | theInts[index] = value; 14 | } 15 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/5-destructors/units/IntList.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | class IntList { 7 | private: 8 | int* theInts; 9 | char* listName; 10 | unsigned int numInts; 11 | static const int NAME_LENGTH=5; 12 | int dummy[NAME_LENGTH]; 13 | 14 | public: 15 | IntList(uint numInts) { 16 | cout << "construct(" << numInts << ")" << endl; 17 | theInts = new int[numInts]; 18 | listName = new char[NAME_LENGTH]; 19 | this->numInts = numInts; 20 | } 21 | 22 | ~IntList() { 23 | cout << "destruct(" << numInts << ")" << endl; 24 | delete[] theInts; 25 | delete[] listName; 26 | // NOTE: no need to delete dummy - it is on the stack 27 | } 28 | 29 | void print() {cout << " theInts=" << theInts << endl; } 30 | 31 | void fill(int value); 32 | int get(uint index); 33 | void put(uint index, int value); 34 | }; 35 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/5-destructors/units/IntList2D.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | class IntList2D { 7 | private: 8 | int** theInts; 9 | char* listName; 10 | unsigned int rows, cols; 11 | 12 | public: 13 | IntList2D(uint rows, uint cols) { 14 | cout << "construct(" << rows << "," << cols << ")" << endl; 15 | theInts = new int*[rows]; 16 | for (int r=0; rrows = rows; 20 | this->cols = cols; 21 | } 22 | 23 | ~IntList2D() { 24 | cout << "destruct(" << rows << "," << cols << ")" << endl; 25 | for (int r=0; r 11 | using namespace std; 12 | 13 | int main() { 14 | cout << "{" << endl; 15 | { 16 | IntList listarray[5] {1,2,3,4,5}; // five constructions 17 | } 18 | cout << "}" << endl; 19 | 20 | IntList* listpointer; 21 | cout << "{" << endl; 22 | { 23 | listpointer = new IntList[5] {6,7,8,9,10}; // five constructions 24 | } 25 | cout << "}" << endl; 26 | delete[] listpointer; // five destructions 27 | // delete listpointer; // one destructor + segmentation fault / memory leak 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/6-destruct-arrays/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | # A generic makefile for running simple C++ projects. 3 | # 4 | # USAGE: 5 | # 1. Put all your non-main files in subfolder "units". 6 | # 2. Run: make MAIN=.cpp 7 | # It should compile all the cpp files in "units", link them with your main program, and run it. 8 | # 9 | # Based on https://stackoverflow.com/a/2481326/827927 10 | # AUTHOR: Erel Segal-Halevi 11 | 12 | CXX=clang++-9 13 | CXXFLAGS=-std=c++2a 14 | 15 | RM=rm -f 16 | 17 | ifndef MAIN 18 | MAIN=./main.cpp 19 | endif 20 | 21 | MAINEXECUTABLE=$(subst .cpp,.exe,$(MAIN)) 22 | 23 | SOURCES=$(shell ls units/*.cpp) $(MAIN) 24 | HEADERS=$(shell ls units/*.h*) 25 | 26 | all: $(MAINEXECUTABLE) 27 | $(MAINEXECUTABLE) 28 | 29 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 30 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 31 | 32 | clean: 33 | $(RM) *.exe a.out 34 | 35 | echo: 36 | echo $(SOURCES) 37 | echo $(HEADERS) 38 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/6-destruct-arrays/units/IntList.cpp: -------------------------------------------------------------------------------- 1 | #include "IntList.hpp" 2 | 3 | void IntList::fill(int value) { 4 | for (uint i=0; inumInts; ++i) 5 | theInts[i] = value; 6 | } 7 | 8 | int IntList::get(uint index) { 9 | return theInts[index]; 10 | } 11 | 12 | void IntList::put(uint index, int value) { 13 | theInts[index] = value; 14 | } 15 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/6-destruct-arrays/units/IntList.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | using namespace std; 5 | 6 | class IntList { 7 | private: 8 | int* theInts; 9 | uint numInts; 10 | 11 | public: 12 | IntList(uint numInts) { 13 | cout << "construct(" << numInts << ")" << endl; 14 | theInts = new int[numInts]; 15 | this->numInts = numInts; 16 | } 17 | 18 | ~IntList() { 19 | cout << "destruct(" << numInts << ")" << endl; 20 | delete[] theInts; 21 | } 22 | 23 | void fill(int value); 24 | int get(uint index); 25 | void put(uint index, int value); 26 | }; 27 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/7-valgrind/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | 8 | RM=rm -f 9 | 10 | # VALGRIND_FLAGS= 11 | VALGRIND_FLAGS=-v --leak-check=full --show-leak-kinds=all --error-exitcode=99 12 | 13 | ifndef MAIN 14 | MAIN=./a_new_without_delete.cpp 15 | endif 16 | 17 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 18 | 19 | SOURCES=$(MAIN) 20 | 21 | valgrind: $(MAINEXECUTABLE) 22 | valgrind --tool=memcheck $(VALGRIND_FLAGS) $(MAINEXECUTABLE) 2>&1 | { egrep "lost| at " || true; } 23 | 24 | $(MAINEXECUTABLE): $(SOURCES) 25 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 26 | 27 | clean: 28 | $(RM) *.exe a.out 29 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/7-valgrind/a_minimal_program.cpp: -------------------------------------------------------------------------------- 1 | int main() { 2 | return 0; 3 | } -------------------------------------------------------------------------------- /02-classes-constructors-destructors/7-valgrind/a_new_without_delete.cpp: -------------------------------------------------------------------------------- 1 | class Point { 2 | int x, y; 3 | void f() { x=5; } 4 | void g() { y=x*x; } 5 | void h() { x=y+x; } 6 | public: 7 | Point () {} 8 | }; 9 | 10 | int main() { 11 | Point array_stack [1000]; 12 | Point* array_heap = new Point[1000]; 13 | // delete[] array_heap; 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/7-valgrind/class_with_destructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class IntArray { 5 | int* ints; 6 | public: 7 | IntArray(int num) { 8 | ints = new int[num]; 9 | } 10 | ~IntArray() { 11 | delete[] ints; 12 | } 13 | void put(int index, int value) { 14 | ints[index] = value; 15 | } 16 | int get(int index) { 17 | return ints[index]; 18 | } 19 | }; 20 | 21 | int main() { 22 | for (int i=0; i<10; ++i) { 23 | IntArray a(1000); 24 | a.put(4, 10); 25 | cout << a.get(4) << endl; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/7-valgrind/class_without_destructor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class IntArray { 5 | int* ints; 6 | public: 7 | IntArray(int num) { 8 | ints = new int[num]; 9 | } 10 | void put(int index, int value) { 11 | ints[index] = value; 12 | } 13 | int get(int index) { 14 | return ints[index]; 15 | } 16 | }; 17 | 18 | int main() { 19 | for (int i=0; i<20; ++i) { 20 | IntArray a(1000); 21 | a.put(4, 10); 22 | cout << a.get(4) << endl; 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/9-tidy/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | CXX=clang++-9 3 | CXXFLAGS=-std=c++2a 4 | 5 | tidy: 6 | clang-tidy *.cpp -checks=bugprone-*,clang-analyzer-*,cppcoreguidelines-*,performance-*,portability-*,readability-* --warnings-as-errors=* -- 7 | 8 | tidycore: 9 | clang-tidy *.cpp -checks=cppcoreguidelines-* -- 10 | 11 | tidyread: 12 | clang-tidy *.cpp -checks=readability-* -- 13 | 14 | run: 15 | $(CXX) $(CXXFLAGS) *.cpp -o main.exe 16 | ./main.exe 17 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/9-tidy/hello.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * This program contains some issues that are detected by clang-tidy. 3 | * 4 | * Run `make tidy` to find out. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | const int NUM_OF_SUBMISSIONS_IN_SEMESTER = 12; 13 | 14 | int main() { 15 | cout << "Each student should submit " << 12 << " exercises." << endl; 16 | 17 | int submission_count = 11; 18 | if (submission_count >= 12) 19 | cout << "OK!" << endl; 20 | else 21 | cout << "Try again." << endl; 22 | 23 | string submission_names[3] = {"a", "b", "c"}; 24 | // array submission_names = {"a", "b", "c"}; // Array with a fixed size, like in C 25 | cout << submission_names[0] << endl; 26 | return 0; 27 | } 28 | 29 | -------------------------------------------------------------------------------- /02-classes-constructors-destructors/slides-0-overloading.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/slides-0-overloading.odp -------------------------------------------------------------------------------- /02-classes-constructors-destructors/slides-0-overloading.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/slides-0-overloading.pdf -------------------------------------------------------------------------------- /02-classes-constructors-destructors/slides-1-classes.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/slides-1-classes.odp -------------------------------------------------------------------------------- /02-classes-constructors-destructors/slides-1-classes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/slides-1-classes.pdf -------------------------------------------------------------------------------- /02-classes-constructors-destructors/slides-2-new-delete.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/slides-2-new-delete.odp -------------------------------------------------------------------------------- /02-classes-constructors-destructors/slides-2-new-delete.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/slides-2-new-delete.pdf -------------------------------------------------------------------------------- /02-classes-constructors-destructors/slides-3-scope.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/slides-3-scope.pdf -------------------------------------------------------------------------------- /02-classes-constructors-destructors/text-0-overloading.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/text-0-overloading.odt -------------------------------------------------------------------------------- /02-classes-constructors-destructors/text-0-overloading.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/text-0-overloading.pdf -------------------------------------------------------------------------------- /02-classes-constructors-destructors/text-1-classes.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/text-1-classes.odt -------------------------------------------------------------------------------- /02-classes-constructors-destructors/text-1-classes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/text-1-classes.pdf -------------------------------------------------------------------------------- /02-classes-constructors-destructors/text-2-new-delete.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/text-2-new-delete.odt -------------------------------------------------------------------------------- /02-classes-constructors-destructors/text-2-new-delete.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/02-classes-constructors-destructors/text-2-new-delete.pdf -------------------------------------------------------------------------------- /03-composition-references/0-composition/Line.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/0-composition/Line.class -------------------------------------------------------------------------------- /03-composition-references/0-composition/Line.java: -------------------------------------------------------------------------------- 1 | public class Line 2 | { 3 | private Point p1, p2; 4 | 5 | public void set(int x1,int y1,int x2,int y2) { 6 | p1.setX(x1); 7 | p1.setY(y1); 8 | p2.setX(x2); 9 | p2.setY(y2); 10 | } 11 | 12 | public void set(Point p1, Point p2) { 13 | this.p1 = p1; 14 | this.p2 = p2; 15 | } 16 | 17 | public String toString() { return "Line from " + p1 + " to " + p2; } 18 | }; 19 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/LineTest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/0-composition/LineTest.class -------------------------------------------------------------------------------- /03-composition-references/0-composition/LineTest.cpp: -------------------------------------------------------------------------------- 1 | #include "units/Point.hpp" 2 | #include "units/Line.hpp" 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main() { 8 | Line l1; 9 | cout << "l1=" << l1.to_string() << endl; 10 | 11 | Line::default_value = 55; 12 | 13 | Line l2; 14 | cout << "l2=" << l2.to_string() << endl; 15 | 16 | // l1.set(1,2,3,4); 17 | // cout << "l1=" << l1.to_string() << endl; 18 | 19 | Line l3(5,6,7,8); 20 | cout << "l3=" << l3.to_string() << endl; 21 | 22 | // Line l4(Point(11,12)); 23 | // cout << "l4=" << l4.to_string() << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/LineTest.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | class LineTest { 4 | public static void main(String[] args) { 5 | Line l1; 6 | ///System.out.println(l1.toString()); // compiler error - l1 not initialized! 7 | l1 = new Line(); 8 | System.out.println(l1.toString()); 9 | l1.set(1,2,3,4); // null pointer exception - p1 and p2 are not initialized! 10 | System.out.println(l1.toString()); 11 | 12 | // Line l2 = new Line(1,2,3,4); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running simple C++ projects. 2 | # 3 | # USAGE: 4 | # 1. Put all your non-main files in subfolder "units". 5 | # 2. Run: make MAIN=.cpp 6 | # It should compile all the cpp files in "units", link them with your main program, and run it. 7 | # 8 | # Based on https://stackoverflow.com/a/2481326/827927 9 | # AUTHOR: Erel Segal-Halevi 10 | 11 | CXX=clang++-9 12 | RM=rm -f 13 | CPPFLAGS=-std=c++2a 14 | 15 | ifndef MAIN 16 | MAIN=./main.cpp 17 | endif 18 | 19 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 20 | 21 | SOURCES=$(wildcard units/*.cpp) $(MAIN) 22 | HEADERS=$(wildcard units/*.h*) 23 | 24 | run: $(MAINEXECUTABLE) 25 | $(MAINEXECUTABLE) 26 | 27 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 28 | $(CXX) $(CPPFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 29 | 30 | clean: 31 | $(RM) *.exe a.out *.class 32 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/Point.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/0-composition/Point.class -------------------------------------------------------------------------------- /03-composition-references/0-composition/Point.java: -------------------------------------------------------------------------------- 1 | public class Point { 2 | public int x; 3 | public int y; 4 | 5 | public void setX(int x) { this.x = x; } 6 | public void setY(int y) { this.y = y; } 7 | public String toString() { return "("+x+","+y+")"; } 8 | }; 9 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/PointTest.cpp: -------------------------------------------------------------------------------- 1 | #include "units/Line.hpp" 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | Point p (3,4); 8 | cout << "p=" << p.to_string() << endl; 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/run-java: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | javac *.java 3 | java LineTest 4 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/units/Line.cpp: -------------------------------------------------------------------------------- 1 | #include "Line.hpp" 2 | 3 | 4 | int Line::default_value = 88; 5 | 6 | int sqr(int x) { 7 | return x*x; 8 | } 9 | 10 | Line::Line(int x1,int y1,int x2,int y2) 11 | : 12 | p1{sqr(x1),sqr(y1)}, 13 | p2{x2,y2} 14 | { 15 | // p1.setX(sqr(x1)); 16 | // p1.setY(sqr(y1)); 17 | // p2.setX(x2); 18 | // p2.setY(y2); 19 | } 20 | 21 | 22 | 23 | void Line::set(int x1,int y1,int x2,int y2) { 24 | p1.setX(x1); 25 | p1.setY(y1); 26 | p2.setX(x2); 27 | p2.setY(y2); 28 | } 29 | 30 | void Line::set(Point p1, Point p2) { 31 | this->p1 = p1; 32 | this->p2 = p2; 33 | } 34 | 35 | string Line::to_string() 36 | { 37 | return "Line from " + p1.to_string() + " to " + p2.to_string(); 38 | } 39 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/units/Line.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "Point.hpp" 5 | 6 | class Line 7 | { 8 | private: 9 | Point p1, p2; 10 | public: 11 | static int default_value; 12 | 13 | Line() 14 | : p1{default_value,default_value}, p2{default_value,default_value} 15 | //: p1{}, p2{} // automatic 16 | { 17 | // p1.setX(0); 18 | // p1.setY(0); 19 | // p2.setX(0); 20 | // p2.setY(0); 21 | } 22 | 23 | Line(int xy1, int xy2): 24 | p1{xy1}, p2{xy2} 25 | { 26 | 27 | 28 | } 29 | Line(int x1,int y1,int x2,int y2); 30 | 31 | Line(Point new_p1): p1(new_p1) {} 32 | 33 | void set(int,int,int,int); 34 | void set(Point,Point); 35 | string to_string(); 36 | }; 37 | 38 | -------------------------------------------------------------------------------- /03-composition-references/0-composition/units/Point.cpp: -------------------------------------------------------------------------------- 1 | #include "Point.hpp" 2 | 3 | #include 4 | using namespace std; 5 | 6 | void Point::setX(int newX) { 7 | cout << "setX(" << newX <<")" < 8 | using namespace std; 9 | 10 | class Point { 11 | private: 12 | int x; 13 | int y; 14 | public: 15 | void setX(int); 16 | void setY(int); 17 | string to_string(); 18 | 19 | Point() { setX(9); setY(9); } 20 | 21 | Point(int z): Point(z,z) {} 22 | // x(z), y(z) { } 23 | //Point(int z) { x=y=z; } 24 | 25 | //Point(int xx, int yy): x(xx), y(yy) { } 26 | Point(int xx, int yy) { { setX(xx); setY(yy); } } 27 | }; 28 | 29 | // #endif 30 | -------------------------------------------------------------------------------- /03-composition-references/1-reference/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | RM=rm -f 7 | CPPFLAGS=-std=c++2a 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CPPFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /03-composition-references/1-reference/lvalue_rvalue_2.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates the difference between l-value and r-value in functions. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-02 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | void test1(int& z) { 12 | z = z + 6; 13 | } 14 | 15 | void test2(const int& z) { 16 | // z = z + 6; 17 | } 18 | 19 | 20 | int main() { 21 | int x = 5; 22 | const int y = 6; 23 | 24 | // RIDDLE: Which of the following is legal? 25 | 26 | // test1(x); 27 | // test1(x+1); 28 | // test1(5); 29 | // test2(x); 30 | // test2(x+1); 31 | // test2(5); 32 | 33 | // test1(y); 34 | // test2(y); 35 | } 36 | -------------------------------------------------------------------------------- /03-composition-references/1-reference/optional/null-reference.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void test (const int& i) { 5 | if (&i) 6 | cout << i << endl; 7 | else 8 | cout << "NULL" << endl; 9 | } 10 | 11 | int main() { 12 | test(5); 13 | int i=6; 14 | test(i); 15 | int* j=nullptr; 16 | test(*j); // This is considered undefined behavior! 17 | // But on my computer, it works and prints NULL. 18 | 19 | 20 | 21 | // Segmentation fault: 22 | int* nullnumptr = nullptr; 23 | int& nullnumref = *nullnumptr; 24 | // int& nullnumref = *nullptr; // compilation error 25 | 26 | nullnumref = 5; 27 | cout << "nullnumptr = " << nullnumptr << endl; 28 | cout << "&nullnumref = " << &nullnumref << endl; 29 | // cout << "(*nullnumptr) = " << (*nullnumptr) << endl; 30 | cout << "nullnumref" << nullnumref << endl; // segmentation fault 31 | } 32 | -------------------------------------------------------------------------------- /03-composition-references/1-reference/optional/reference-from-pointer.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates constructing references from pointers. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-02 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | int* p1 = new int{5}; 12 | int& r1 = *p1; 13 | cout << r1 << endl; 14 | 15 | int& r2 = *new int{6}; 16 | cout << r2 << endl; 17 | 18 | int i3 = *new int{7}; 19 | cout << i3 << endl; 20 | } 21 | -------------------------------------------------------------------------------- /03-composition-references/1-reference/optional/reference-to-pointer.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates converting references to pointers. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-02 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | int i = 5; 11 | int* ip = &i; 12 | 13 | int k = 500; 14 | 15 | int*& f() { 16 | return ip; 17 | } 18 | 19 | int main() { 20 | cout << (*ip) << endl; 21 | (*ip) = 6; 22 | cout << i << endl << endl; 23 | 24 | int* jp = f(); 25 | cout << (*jp) << endl; 26 | (*jp) = 7; 27 | cout << i << endl << endl; 28 | 29 | jp = &k; // ip does not change - it still points to i. 30 | cout << (*jp) << endl; 31 | (*jp) = 501; 32 | cout << i << endl; 33 | cout << (*ip) << endl << endl; 34 | 35 | int*& jpr = f(); // equivalent to: jpr = ip; 36 | cout << (*jpr) << endl; 37 | (*jpr) = 8; 38 | cout << i << endl << endl; 39 | 40 | jpr = &k; // ip changes - it now points to k. 41 | cout << (*jpr) << endl; 42 | (*jpr) = 502; 43 | cout << i << endl; 44 | cout << (*ip) << endl << endl; 45 | } 46 | -------------------------------------------------------------------------------- /03-composition-references/2-reference-return/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | 8 | ifndef MAIN 9 | MAIN=./main.cpp 10 | endif 11 | 12 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 13 | 14 | SOURCES=$(MAIN) 15 | 16 | all: $(MAINEXECUTABLE) 17 | $(MAINEXECUTABLE) 18 | 19 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 20 | $(CXX) $(CPPFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 21 | 22 | clean: 23 | rm -f *.exe a.out *.class 24 | -------------------------------------------------------------------------------- /03-composition-references/2-reference-return/a_referece_as_return_value.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates returning references from functions. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-03 5 | */ 6 | 7 | #include 8 | using namespace std; 9 | 10 | 11 | int globalnum = 11; 12 | 13 | int g1() { 14 | return globalnum; 15 | } 16 | 17 | int& g2() { 18 | return globalnum; 19 | } 20 | 21 | const int& g3() { 22 | return globalnum; 23 | } 24 | 25 | int& g4() { // bug 26 | int localnum = 9999; 27 | return localnum; 28 | } 29 | 30 | 31 | int main() { 32 | cout << "globalnum = " << globalnum << endl; 33 | cout << "g1() = " << g1() << endl; 34 | cout << "g2() = " << g2() << endl; 35 | cout << "g3() = " << g3() << endl; 36 | cout << "g4() = " << g4() << endl; 37 | 38 | // g1()=111; 39 | g2()=222; 40 | // g3()=333; 41 | g4()=444; 42 | 43 | cout << endl << "globalnum = " << globalnum << endl; 44 | cout << "g1() = " << g1() << endl; 45 | cout << "g2() = " << g2() << endl; 46 | cout << "g3() = " << g3() << endl; 47 | cout << "g4() = " << g4() << endl; 48 | } 49 | -------------------------------------------------------------------------------- /03-composition-references/7-const-methods/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | RM=rm -f 7 | CPPFLAGS=-std=c++2a 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CPPFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /03-composition-references/8-friend-functions/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | RM=rm -f 7 | CPPFLAGS=-std=c++2a 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CPPFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | valgrind: 24 | valgrind --tool=memcheck $(MAINEXECUTABLE) 25 | 26 | clean: 27 | $(RM) *.exe a.out *.class 28 | -------------------------------------------------------------------------------- /03-composition-references/slides-1-composition-initialization.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/slides-1-composition-initialization.odp -------------------------------------------------------------------------------- /03-composition-references/slides-1-composition-initialization.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/slides-1-composition-initialization.pdf -------------------------------------------------------------------------------- /03-composition-references/slides-2-reference.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/slides-2-reference.odp -------------------------------------------------------------------------------- /03-composition-references/slides-2-reference.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/slides-2-reference.pdf -------------------------------------------------------------------------------- /03-composition-references/slides-3-const-and-friend.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/slides-3-const-and-friend.odp -------------------------------------------------------------------------------- /03-composition-references/slides-3-const-and-friend.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/slides-3-const-and-friend.pdf -------------------------------------------------------------------------------- /03-composition-references/slides-9-nsdmi.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/slides-9-nsdmi.pptx -------------------------------------------------------------------------------- /03-composition-references/text-1-composition-initialization.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/text-1-composition-initialization.odt -------------------------------------------------------------------------------- /03-composition-references/text-1-composition-initialization.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/text-1-composition-initialization.pdf -------------------------------------------------------------------------------- /03-composition-references/text-2-references.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/text-2-references.odt -------------------------------------------------------------------------------- /03-composition-references/text-2-references.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/text-2-references.pdf -------------------------------------------------------------------------------- /03-composition-references/text-3-const-and-friend.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/text-3-const-and-friend.odt -------------------------------------------------------------------------------- /03-composition-references/text-3-const-and-friend.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/03-composition-references/text-3-const-and-friend.pdf -------------------------------------------------------------------------------- /04-operator-overloading/0-int-string-operators/Makefile: -------------------------------------------------------------------------------- 1 | CXX=clang++-9 2 | CXXFLAGS=-std=c++2a 3 | 4 | all: 5 | $(CXX) $(CXXFLAGS) *.cpp 6 | ./a.out 7 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | RM=rm -f 7 | CPPFLAGS=-std=c++2a 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) Complex.cpp 16 | HEADERS=Complex.hpp 17 | 18 | all: $(MAINEXECUTABLE) 19 | $(MAINEXECUTABLE) 20 | 21 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 22 | $(CXX) $(CPPFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 23 | 24 | valgrind: 25 | valgrind --tool=memcheck $(MAINEXECUTABLE) 26 | 27 | tidy: 28 | clang-tidy $(SOURCES) -checks=bugprone-*,clang-analyzer-*,cppcoreguidelines-*,performance-*,portability-*,readability-*,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-cppcoreguidelines-owning-memory --warnings-as-errors=-* -- 29 | 30 | clean: 31 | $(RM) *.exe a.out *.class 32 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/input.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * A demo of input format errors. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-03 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | 14 | 15 | int main() { 16 | int i=9999; 17 | cout << i << endl; 18 | istringstream input("123 x"); 19 | cout << "Before reading: "; 20 | if (input) {cout <<"input is OK"<> i; 22 | cout << "After reading: "; 23 | if (input) {cout <<"input is OK"<> nextChar; 29 | cout << "next char is: " << nextChar << endl; 30 | } 31 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/ok1.txt: -------------------------------------------------------------------------------- 1 | 3+4i xyz 2 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/ok2.txt: -------------------------------------------------------------------------------- 1 | -2.1+2.4i 2 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/w1.txt: -------------------------------------------------------------------------------- 1 | f 2 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/w2.txt: -------------------------------------------------------------------------------- 1 | 3f+3 2 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/w3.txt: -------------------------------------------------------------------------------- 1 | 3+f 2 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/w4.txt: -------------------------------------------------------------------------------- 1 | 1+1 2 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/w5.txt: -------------------------------------------------------------------------------- 1 | 1f1 2 | -------------------------------------------------------------------------------- /04-operator-overloading/1-arithmetic-operators/inputs/w6.txt: -------------------------------------------------------------------------------- 1 | 3+4f 2 | -------------------------------------------------------------------------------- /04-operator-overloading/2-brackets-operator/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running simple C++ projects. 2 | # 3 | # USAGE: 4 | # 1. Put all your non-main files in subfolder "units". 5 | # 2. Run: make MAIN=.cpp 6 | # It should compile all the cpp files in "units", link them with your main program, and run it. 7 | # 8 | # Based on https://stackoverflow.com/a/2481326/827927 9 | # AUTHOR: Erel Segal-Halevi 10 | 11 | CXX=clang++-9 12 | CXXFLAGS=-std=c++2a 13 | RM=rm -f 14 | 15 | ifndef MAIN 16 | MAIN=./main.cpp 17 | endif 18 | 19 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 20 | 21 | SOURCES=$(shell ls units/*.cpp) $(MAIN) 22 | HEADERS=$(shell ls units/*.h*) 23 | 24 | all: $(MAINEXECUTABLE) 25 | $(MAINEXECUTABLE) 26 | 27 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 28 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 29 | 30 | valgrind: 31 | valgrind --leak-check=full -v ./$(MAINEXECUTABLE) 32 | 33 | clean: 34 | $(RM) *.exe a.out 35 | -------------------------------------------------------------------------------- /04-operator-overloading/2-brackets-operator/units/IntList.cpp: -------------------------------------------------------------------------------- 1 | #include "IntList.hpp" 2 | #include 3 | 4 | void IntList::operator=(int value) { 5 | for (uint i=0; inumInts; ++i) 6 | theInts[i] = value; 7 | } 8 | 9 | const int IntList::operator[](uint index) const { 10 | cout << "c "; 11 | if (index >= numInts) 12 | throw out_of_range("Array index out of bounds: "+to_string(index)); 13 | return theInts[index]; 14 | } 15 | 16 | int& IntList::operator[](uint index) { 17 | cout << "r "; 18 | if (index >= numInts) 19 | throw out_of_range("Array index out of bounds: "+to_string(index)); 20 | return theInts[index]; 21 | } 22 | -------------------------------------------------------------------------------- /04-operator-overloading/4-parentheses-operator/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /04-operator-overloading/4-parentheses-operator/old/printfunctor.old.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demo of the Functor objects. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2017 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | using namespace std; 12 | 13 | class PrintFunctor { 14 | char _x; 15 | public: 16 | PrintFunctor(char x='*'): 17 | _x(x) { } 18 | void operator() () { cout << _x; } 19 | }; 20 | 21 | void newline() { cout << endl; } 22 | 23 | void repeat(int n, PrintFunctor action) { 24 | for (int i=0; i 7 | using namespace std; 8 | 9 | struct A { 10 | 11 | explicit A(int i) { 12 | cout << "Constructing A from int " << i << endl; 13 | } 14 | 15 | A(int i, int j) { 16 | cout << "Constructing A from two ints " << i << "," << j << endl; 17 | } 18 | 19 | A(const A& a) { 20 | cout << "Constructing A from A" << endl; 21 | } 22 | 23 | A& operator=(const A& a) { 24 | cout << "Assigning A from A" << endl; 25 | return *this; 26 | } 27 | 28 | }; 29 | 30 | void f (A a) { 31 | cout << "In f" << endl; 32 | } 33 | 34 | int main() { 35 | //A a1 = 37; // won't compile 36 | cout << "-------------------" << endl; 37 | 38 | A a2(37); // compile ok 39 | cout << "-------------------" << endl; 40 | 41 | //a1 = 67; // won't compile 42 | a2 = A{67}; // compile OK 43 | cout << "-------------------" << endl; 44 | 45 | //f(77); // won't compile 46 | f(A{77}); // compile OK 47 | cout << "-------------------" << endl; 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /05-copy-convert/old/convert_primitives.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Ofir Pele 3 | * @since 2017 4 | */ 5 | 6 | #include 7 | using namespace std; 8 | 9 | // This does not compile! 10 | operator string(int i) { 11 | return to_string(i); 12 | } 13 | 14 | int main() { 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /05-copy-convert/slides.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/05-copy-convert/slides.odp -------------------------------------------------------------------------------- /05-copy-convert/slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/05-copy-convert/slides.pdf -------------------------------------------------------------------------------- /05-copy-convert/text.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/05-copy-convert/text.odt -------------------------------------------------------------------------------- /05-copy-convert/text.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/05-copy-convert/text.pdf -------------------------------------------------------------------------------- /06-inheritance/1-order-of-ctors-and-dtors/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /06-inheritance/4-pure-virtual/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /06-inheritance/4-pure-virtual/pure-virtual.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct A { 5 | virtual void f() = 0; 6 | }; 7 | 8 | // void A::f() { 9 | // cout << "A::f" << endl; 10 | // } 11 | 12 | struct B: public A { 13 | virtual void g() {} 14 | }; 15 | 16 | struct C: public B { 17 | void f() { 18 | cout << "C::f" << endl; 19 | }; 20 | }; 21 | 22 | 23 | int main(){ 24 | // A a; a.f(); 25 | // B b; 26 | C c; 27 | A* a = &c; 28 | a->f(); 29 | } 30 | -------------------------------------------------------------------------------- /06-inheritance/old/2-overriding/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /06-inheritance/old/2-overriding/hiding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | struct A { 6 | virtual void f() { 7 | cout << "A::f" << endl; 8 | } 9 | virtual void f() const { 10 | cout << "A::f const" << endl; 11 | } 12 | }; 13 | 14 | struct B: public A { 15 | void f() const { 16 | cout << "B::f const" << endl; 17 | } 18 | }; 19 | 20 | struct C: public B { 21 | void f() { 22 | cout << "C::f" << endl; 23 | } 24 | void f() const { 25 | cout << "C::f const" << endl; 26 | } 27 | }; 28 | 29 | int main() { 30 | A* ap1 = new A; ap1->f(); // call-virtual f() [runtime: calls A::f] 31 | A* ap2 = new B; ap2->f(); // call-virtual f() [runtime: calls A::f] 32 | A* ap3 = new C; ap3->f(); // call-virtual f() [runtime: calls C::f] 33 | B* bp1 = new B; bp1->f(); // call-static B::f()const 34 | B* bp2 = new C; bp2->f(); // call-static B::f()const 35 | C* cp1 = new C; cp1->f(); // call-virtual f() [runtime: calls C::f] 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /06-inheritance/old/2-overriding/virtual_from_ctor.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Shows that when calling virtual functions from the constructor, 3 | * the version of the current class is called. 4 | * 5 | * CREDIT: Shlomi, 5778 c++ course. 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | struct Base { 12 | Base() { f(); } 13 | virtual void f(){ cout<<"Base"< out.txt 9 | echo --- 10 | ./redirection.exe 2> err.txt 11 | echo --- 12 | ./redirection.exe 1> out.txt 2> err.txt 13 | echo --- 14 | -------------------------------------------------------------------------------- /07-rtti/1-redirection/redirection.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates the differences between cout and cerr, 3 | * the output buffering, 4 | * and the bash output redirection methods. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | const int WAIT_TIME=3; 15 | 16 | int main() { 17 | cout << "Start " << endl; 18 | cout << "The result is 2 "; 19 | this_thread::sleep_for(chrono::seconds(WAIT_TIME)); 20 | cout << endl; 21 | // cout.flush(); 22 | this_thread::sleep_for(chrono::seconds(WAIT_TIME)); 23 | cout << "The result is 3 " << endl; 24 | 25 | cerr << "There is a bug "; 26 | this_thread::sleep_for(chrono::seconds(WAIT_TIME)); 27 | cerr << endl; 28 | // this_thread::sleep_for(chrono::seconds(WAIT_TIME)); 29 | cout << "The result is 4 " << endl; 30 | return 0; 31 | } -------------------------------------------------------------------------------- /07-rtti/3-manipulators-float-precision/Makefile: -------------------------------------------------------------------------------- 1 | #!make -f 2 | 3 | CXX=clang++-9 4 | CXXFLAGS=-std=c++2a -Wunused-comparison 5 | RM=rm -f 6 | 7 | 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | all: 14 | $(CXX) $(CXXFLAGS) $(MAIN) 15 | ./a.out 16 | -------------------------------------------------------------------------------- /07-rtti/3-manipulators-float-precision/boolalpha.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * A simple program demonstrating the use of output manipulators. 3 | * 4 | * See https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt. 5 | * 6 | * @author Erel Segal-Halevi 7 | * @since 2021-04 8 | */ 9 | 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | 16 | int main() { 17 | cout << "3 > 2: " << (3 > 2) << endl 18 | << "3 < 2: " << (3 < 2) << endl 19 | << boolalpha // calls boolalpha(cout) 20 | << "3 > 2: " << (3 > 2) << endl 21 | << "3 < 2: " << (3 < 2) << endl 22 | << noboolalpha 23 | << "3 > 2: " << (3 > 2) << endl 24 | << "3 < 2: " << (3 < 2) << endl; 25 | } 26 | -------------------------------------------------------------------------------- /07-rtti/3-manipulators-float-precision/precision.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * A demo of output formatting and IO manipulators 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-03 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | void printxyz() { 13 | cout << " "; 14 | double x = 1.66, y=5./3., z=y*3.; 15 | cout << x << " " << y << " " << z << endl; 16 | } 17 | 18 | int main() { 19 | printf("%.3f\n",1234.5678); 20 | 21 | cout << 1234.5678 << endl; 22 | cout << 12345678. << endl; 23 | 24 | cout << setprecision(4) 25 | << 1234.5678 << endl 26 | << 12345678. << endl; 27 | 28 | cout << setprecision(100) 29 | << 1234.5678 << endl 30 | << 12345678. << endl; 31 | 32 | ostringstream os; 33 | os << setprecision(4) << 1234.5678; 34 | cout << os.str() << endl; 35 | // Now, in your doctests, you can do: 36 | // CHECK_EQ(os.str(),"1235"); 37 | } 38 | -------------------------------------------------------------------------------- /07-rtti/4-image/.gitignore: -------------------------------------------------------------------------------- 1 | *.ppm -------------------------------------------------------------------------------- /07-rtti/4-image/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /07-rtti/4-image/old/image.simple.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Write a PPM image-file. 3 | * 4 | * From RosettaCode: 5 | * http://rosettacode.org/wiki/Category:C 6 | */ 7 | 8 | #include 9 | #include 10 | 11 | int main(void) 12 | { 13 | const int dimx = 800, dimy = 800; 14 | int i, j; 15 | FILE *imageFile = fopen("c.ppm", "wb"); /* b - binary mode */ 16 | (void) fprintf(imageFile, "P6\n%d %d\n255\n", dimx, dimy); 17 | for (j = 0; j < dimy; ++j) 18 | { 19 | for (i = 0; i < dimx; ++i) 20 | { 21 | static unsigned char color[3]; 22 | color[0] = i % 256; /* red */ 23 | color[1] = j % 256; /* green */ 24 | color[2] = (i * j) % 256; /* blue */ 25 | (void) fwrite(color, 1, 3, imageFile); 26 | } 27 | } 28 | (void) fclose(imageFile); 29 | return EXIT_SUCCESS; 30 | } -------------------------------------------------------------------------------- /07-rtti/4-image/old/image.simple.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Writes a PPM image file. 3 | * 4 | * Author: Erel Segal-Halevi. 5 | * Based on C code in RosettaCode: 6 | * http://rosettacode.org/wiki/Category:C 7 | * 8 | * Since: 2018-04 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int main() { 19 | const int dimx = 800, dimy = 800; 20 | for (int offset=0; offset<800; offset+=1) { 21 | ofstream imageFile("image.simple.ppm"); 22 | imageFile << "P6" << endl << dimx <<" " << dimy << endl << 255 << endl; 23 | for (int j = 0; j < dimy; ++j) { 24 | for (int i = 0; i < dimx; ++i) { 25 | char red = i % 256; 26 | char green = j % 256; 27 | char blue = (offset*i+j)%256;//0;//int(64*sin(i)+256*cos(j))%256; /* blue */ 28 | imageFile << red << green << blue; 29 | } 30 | } 31 | imageFile.close(); 32 | this_thread::sleep_for(chrono::seconds(1)); 33 | } 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /07-rtti/5-casting/1-reinterpret_cast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | struct char8 { 7 | char c[8]; 8 | }; 9 | 10 | 11 | int main() { 12 | int i=5; // 10100000 00000000 00000000 00000000 ???????? ???????? ???????? ???????? 13 | 14 | int* ip = &i; 15 | double* dp = (double*)ip; 16 | double* ep = reinterpret_cast(ip); 17 | cout << "i interpreted as a double: " << setprecision(100) << *dp << " " << *ep << endl; // junk junk 18 | 19 | double d = 5.555; 20 | int& ir = (int&)d; 21 | int& jr = reinterpret_cast(d); 22 | cout << "d interpreted as an int: " << ir << " " << jr << endl; // junk junk 23 | cout << "---" << endl; 24 | 25 | char8* cp = reinterpret_cast(&d); 26 | cout << "d interpreted as chars: "; 27 | for (i=0; i<8; ++i) 28 | cout << cp->c[i]; 29 | 30 | // double dd = reinterpret_cast(i); 31 | // int ii = reinterpret_cast(d); 32 | } 33 | -------------------------------------------------------------------------------- /07-rtti/5-casting/2-static_cast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | struct char8 { 6 | char c[8]; 7 | 8 | // operator int() {return 5;} 9 | }; 10 | 11 | 12 | int main() { 13 | double d=12.45; 14 | int i = (int)d; // C-style 15 | int j = static_cast(d); // C++-style 16 | cout << "d casted as an int: " << i << " " << j << endl; 17 | 18 | double e = (double)i; 19 | double f = static_cast(i); 20 | cout << "i casted as a double: " << e << " " << f << endl; 21 | 22 | char8 c; 23 | 24 | // These require a conversion operator: 25 | // int ic = static_cast(c); 26 | // double dc = static_cast(c); 27 | 28 | // These do not compile at all: 29 | // double* pd = static_cast(&i); 30 | // int* pi = static_cast(&d); 31 | } 32 | -------------------------------------------------------------------------------- /07-rtti/5-casting/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /07-rtti/6-typeid/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /07-rtti/old/2-streams/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /07-rtti/old/2-streams/abc.txt: -------------------------------------------------------------------------------- 1 | 1 2+3i 2 | -------------------------------------------------------------------------------- /07-rtti/old/binary_serialization/test_binary_serialization.cpp: -------------------------------------------------------------------------------- 1 | #include "random_image.hpp" 2 | 3 | #include "gtest/gtest.h" 4 | 5 | TEST(RandomImageBinarySerialization, testEmptyImage) { 6 | 7 | random_image rim; 8 | rim.writeToFile("pic.raw"); 9 | 10 | random_image rim2("pic.raw"); 11 | EXPECT_EQ(rim,rim2); 12 | 13 | } 14 | 15 | 16 | TEST(RandomImageBinarySerialization, testImage1) { 17 | 18 | for (uint8_t b=1; b<3; ++b) { 19 | for (size_t t=0; t<4; ++t) { 20 | random_image rim(100,200,b); 21 | rim.writeToFile("pic.raw"); 22 | 23 | random_image rim2("pic.raw"); 24 | 25 | EXPECT_EQ(rim,rim2); 26 | } 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /07-rtti/old/const_cast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void f (int* i) { 5 | // ... Does not change i ... 6 | } 7 | 8 | int main() 9 | { 10 | const int* ip = new int(2); 11 | //f(ip); 12 | int* jp = const_cast(ip); 13 | f(jp); 14 | //double* jp = const_cast(ip); 15 | } 16 | -------------------------------------------------------------------------------- /07-rtti/old/slides-1-rtti.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/old/slides-1-rtti.odp -------------------------------------------------------------------------------- /07-rtti/old/slides-1-rtti.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/old/slides-1-rtti.pdf -------------------------------------------------------------------------------- /07-rtti/old/slides-iostreams-full.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/old/slides-iostreams-full.odp -------------------------------------------------------------------------------- /07-rtti/old/slides-iostreams-full.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/old/slides-iostreams-full.pdf -------------------------------------------------------------------------------- /07-rtti/old/stringify/test_stringify.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "stringify.hpp" 3 | #include 4 | 5 | TEST(stringifyTest, primitivesTest) { 6 | EXPECT_EQ("42", stringify(42)); 7 | EXPECT_EQ("false", stringify(false)); 8 | EXPECT_EQ("3.14", stringify(3.14)); 9 | EXPECT_EQ("12345000000", stringify(1.2345e+10)); 10 | } 11 | 12 | TEST(stringifyTest, complexTest) { 13 | EXPECT_EQ("(3.14,-42)", stringify(std::complex(3.14, -42))); 14 | EXPECT_EQ("(0,3)", stringify(std::complex(0, 3))); 15 | } 16 | -------------------------------------------------------------------------------- /07-rtti/slides-0-iostreams.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/slides-0-iostreams.odp -------------------------------------------------------------------------------- /07-rtti/slides-0-iostreams.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/slides-0-iostreams.pdf -------------------------------------------------------------------------------- /07-rtti/slides-1-rtti.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/slides-1-rtti.odp -------------------------------------------------------------------------------- /07-rtti/slides-1-rtti.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/slides-1-rtti.pdf -------------------------------------------------------------------------------- /07-rtti/text-0-iostreams.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/text-0-iostreams.odt -------------------------------------------------------------------------------- /07-rtti/text-0-iostreams.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/text-0-iostreams.pdf -------------------------------------------------------------------------------- /07-rtti/text-1-rtti.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/text-1-rtti.odt -------------------------------------------------------------------------------- /07-rtti/text-1-rtti.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/07-rtti/text-1-rtti.pdf -------------------------------------------------------------------------------- /08-templates-iterators/1-functions/2-sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Function template example: sum. 3 | * 4 | * Also illustrates errors in the template definition, 5 | * that are revealed only in the instantiation. 6 | * 7 | * @author Erel Segal-Halevi 8 | * @since 2018-04 9 | */ 10 | 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | 16 | template 17 | T sum (T a, T b) { 18 | // f(a); // compile error 19 | return a + b; 20 | } 21 | 22 | 23 | template 24 | T sumv (const vector& v, T start=T{}) { 25 | T result = start; 26 | for (T element: v) 27 | result += element; 28 | return result; 29 | } 30 | 31 | // int g(int a) { 32 | // f(a); // compile error 33 | // } 34 | 35 | 36 | int main() { 37 | cout << sum(5,6) << endl; 38 | cout << sum(5.0,6.2) << endl; 39 | cout << sum(string("abc"),string("def")) << endl; 40 | cout << sum("abc" , "def") << endl; // compile error 41 | 42 | cout << sumv(vector{1,2,3,4,5},0) << endl; 43 | cout << sumv(vector{"aaa","bbb","ccc","ddd","eee"}) << endl; 44 | 45 | // // With your NumberWithUnits class, you could also do: 46 | // cout << sumv(vector{...}) << endl; 47 | } 48 | -------------------------------------------------------------------------------- /08-templates-iterators/1-functions/3-pi.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Variable template example: pi. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-04 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | template 14 | const T pi = 15 | static_cast(3.1415926535897932385L); // variable template 16 | 17 | template T circular_area(T r) { 18 | return pi * r * r; 19 | } 20 | 21 | int main() { 22 | int i = 5; 23 | float f = 5.0; 24 | double d = 5.0; 25 | cout << setprecision(100); 26 | cout << circular_area(i) << endl; 27 | cout << circular_area(f) << endl; 28 | cout << circular_area(d) << endl; 29 | 30 | cout << pi << endl; 31 | cout << pi << endl; 32 | cout << pi << endl; 33 | } 34 | -------------------------------------------------------------------------------- /08-templates-iterators/1-functions/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /08-templates-iterators/2-Stack/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /08-templates-iterators/2-Stack/sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Illustrates how to call a sum function with iterators. 3 | * 4 | * Author: Erel Segal-Halevi 5 | * Since: 2020-04 6 | */ 7 | 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | // #include "sum.hpp" // Writing this file is left as an exercise :-) 13 | 14 | template 15 | T sum(IT begin, IT end, T start) { 16 | T result = start; 17 | for(IT i=begin; i!=end; ++i) { 18 | result += (*i); 19 | } 20 | return result; 21 | } 22 | 23 | 24 | struct MyClass { 25 | MyClass& operator+=(const MyClass& other) { 26 | // do some adding... 27 | return *this; 28 | } 29 | }; 30 | 31 | int main() { 32 | int arr[4] {1,2,3,4}; 33 | cout << sum(begin(arr),end(arr),0) << endl; 34 | 35 | MyClass arr2[4]; 36 | sum(begin(arr2), end(arr2), MyClass{}); 37 | 38 | vector v {"aa","bb","cc"}; 39 | cout << sum(begin(v), end(v), string("")); 40 | } 41 | -------------------------------------------------------------------------------- /08-templates-iterators/slides-1-function-templates.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/slides-1-function-templates.odp -------------------------------------------------------------------------------- /08-templates-iterators/slides-1-function-templates.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/slides-1-function-templates.pdf -------------------------------------------------------------------------------- /08-templates-iterators/slides-2-class-templates-iterators.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/slides-2-class-templates-iterators.odp -------------------------------------------------------------------------------- /08-templates-iterators/slides-2-class-templates-iterators.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/slides-2-class-templates-iterators.pdf -------------------------------------------------------------------------------- /08-templates-iterators/text-1-function-templates.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/text-1-function-templates.odt -------------------------------------------------------------------------------- /08-templates-iterators/text-1-function-templates.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/text-1-function-templates.pdf -------------------------------------------------------------------------------- /08-templates-iterators/text-2-class-templates-iterators.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/text-2-class-templates-iterators.odt -------------------------------------------------------------------------------- /08-templates-iterators/text-2-class-templates-iterators.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/08-templates-iterators/text-2-class-templates-iterators.pdf -------------------------------------------------------------------------------- /09-specializations-metaprogramming/1-variations/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/1-variations/double-args-pair.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::ostream; 6 | using std::string; 7 | 8 | template 9 | struct pair { 10 | T1 first; 11 | T2 second; 12 | }; 13 | 14 | pair func() { 15 | return {2,'c'}; 16 | // equivalent to: 17 | // return pair{2,'c'}; 18 | } 19 | 20 | template 21 | ostream& operator<< (ostream& out, 22 | const pair& thepair) { 23 | out << thepair.first << "," << thepair.second; 24 | return out; 25 | } 26 | 27 | 28 | int main() { 29 | pair p1{1,'b'}; 30 | auto p2 = func(); 31 | // Equivalent to: 32 | // pair p2 = func(); 33 | cout << "p1=" << p1 << " p2=" << p2 << endl; 34 | } 35 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/1-variations/numeric-args-default-value.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using std::cout; 4 | using std::endl; 5 | using std::ostream; 6 | using std::string; 7 | 8 | template class array { 9 | T vals[n]; 10 | public: 11 | array() { 12 | for (int i=0; i 28 | ostream& operator<< (ostream& out, const array& thearray) { 29 | for (int i=0; i array5; // equivalent to array 37 | cout << array5 << endl; 38 | array5[0] = 99999; 39 | cout << array5 << endl; 40 | cout << "size of array5 = " << array5.size << endl << endl; 41 | 42 | array array7; // equivalent to array 43 | cout << array7 << endl; 44 | } 45 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/2-specialization-swap/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/2-specialization-swap/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Buffer.hpp" 2 | 3 | int main() { 4 | int a,b; 5 | myswap(a,b); 6 | 7 | Buffer buffer1{1000000}; 8 | buffer1[0] = 55; 9 | Buffer buffer2{1000000}; 10 | buffer2[1] = 77; 11 | myswap(buffer1, buffer2); 12 | } -------------------------------------------------------------------------------- /09-specializations-metaprogramming/3-specialization-classes/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/3-specialization-classes/simple-class-specialization.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Simple example of class specialization 3 | * 4 | * @author Geeks for Geeks 5 | * @url https://www.geeksforgeeks.org/template-specialization-c/ 6 | */ 7 | 8 | #include 9 | using namespace std; 10 | 11 | template class Test { 12 | public: 13 | Test() { cout << "General template object " << typeid(T).name() << "\n"; } 14 | }; 15 | 16 | template <> 17 | class Test { 18 | public: 19 | Test() { cout << "Specialized template object \n"; } 20 | }; 21 | 22 | int main() { 23 | Test a; 24 | Test b; 25 | Test c; 26 | Test> d; 27 | Test>> e; 28 | } 29 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/4-specialization-type-info/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/5-decltype/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/5-decltype/decltype.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Using template-class-specialization to control return-type 3 | * 4 | * @author M Salters 5 | * @url https://stackoverflow.com/a/26876959/827927 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | 14 | int main() { 15 | int a; 16 | decltype(a) aa; 17 | // auto aa; // compilation error 18 | 19 | int& b=a; 20 | decltype(b) bb=aa; 21 | decltype(b)& cc=aa; 22 | 23 | auto c = a+5; 24 | // equivalent to: 25 | // decltype(a+5) c = a+5; 26 | } 27 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/5-decltype/return-type.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Using template-class-specialization to control return-type 3 | * 4 | * @author M Salters 5 | * @url https://stackoverflow.com/a/26876959/827927 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | template 13 | struct ReturnType { 14 | static T returned_value; 15 | }; 16 | 17 | template <> struct ReturnType { 18 | static double returned_value; 19 | }; 20 | 21 | template 22 | decltype(ReturnType::returned_value) div10(T number) { 23 | decltype(ReturnType::returned_value) result = number / 10.0; 24 | return result; 25 | } 26 | 27 | 28 | int main() { 29 | decltype(ReturnType::returned_value) aa; 30 | decltype(ReturnType::returned_value) bb; 31 | decltype(ReturnType::returned_value) cc; 32 | 33 | cout << setprecision(100); 34 | cout << "short: " << div10(short{36}) << endl; 35 | cout << "int: " << div10(int{36}) << endl; 36 | cout << "float: " << div10(float{36}) << endl; 37 | cout << "double: " << div10(double{36}) << endl; 38 | // cout << "char*: " << div10("a") << endl; 39 | } 40 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/5-decltype/vector.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Using template-class-specialization to control return-type 3 | * 4 | * @author M Salters 5 | * @url https://stackoverflow.com/a/26876959/827927 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | 14 | 15 | int main() { 16 | vector va; 17 | decltype(*begin(va)) aa = va[0]; 18 | decay::type bb; 19 | } 20 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/6-metaprogramming-derivative/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | HEADERS=derivative.hpp rgb.hpp sincos.hpp 17 | 18 | all: $(MAINEXECUTABLE) 19 | $(MAINEXECUTABLE) 20 | 21 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 22 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 23 | 24 | clean: 25 | $(RM) *.exe a.out *.class 26 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/6-metaprogramming-derivative/animate_demo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #include "rgb.hpp" 9 | 10 | 11 | const int DELAY_IN_MILLISECONDS=200; 12 | const char* PICTURE_FILENAME="func.ppm"; 13 | 14 | template 15 | void animate(Picture& pic, Function f_x_t, 16 | double fromX, double toX, int stepsX, 17 | double fromY, double toY, 18 | double fromT, double toT, int stepsT, 19 | RGB background, RGB foreground) { 20 | double stepT = (toT - fromT)/stepsT; 21 | for (double t=fromT; t<=toT; t+=stepT) { 22 | pic.setBackground(background); 23 | auto f_x = [f_x_t=f_x_t, t=t] 24 | (double x){return f_x_t(x,t);}; 25 | pic.plot(f_x, fromX, toX, stepsX, fromY, toY, foreground); 26 | pic.toFile(PICTURE_FILENAME); 27 | this_thread::sleep_for(chrono::milliseconds(DELAY_IN_MILLISECONDS)); 28 | } 29 | } 30 | 31 | int main() { 32 | Picture pic(300,300); 33 | auto f_x_t = [](double x, double t){ 34 | return std::sin(x*t); 35 | }; 36 | animate(pic, f_x_t, -4.0,4.0,1200, -1.1,1.1, 0.5,2.5,100, {255,255,0}, {0,0,255}); 37 | return 0; 38 | } 39 | 40 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/6-metaprogramming-derivative/func.ppm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/09-specializations-metaprogramming/6-metaprogramming-derivative/func.ppm -------------------------------------------------------------------------------- /09-specializations-metaprogramming/6-metaprogramming-derivative/sincos.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | double SinCos(double x) { 6 | return sin(x) + cos(x); 7 | } 8 | 9 | class SinCosFunctor { 10 | public: 11 | SinCosFunctor(double alpha) : alpha(alpha) {} 12 | double operator() (double x) const { 13 | return sin(alpha * x) + cos(x); 14 | } 15 | private: 16 | double alpha; 17 | }; -------------------------------------------------------------------------------- /09-specializations-metaprogramming/7-metaprogramming-physics/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /09-specializations-metaprogramming/slides.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/09-specializations-metaprogramming/slides.odp -------------------------------------------------------------------------------- /09-specializations-metaprogramming/slides.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/09-specializations-metaprogramming/slides.pdf -------------------------------------------------------------------------------- /09-specializations-metaprogramming/text.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/09-specializations-metaprogramming/text.odt -------------------------------------------------------------------------------- /09-specializations-metaprogramming/text.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/09-specializations-metaprogramming/text.pdf -------------------------------------------------------------------------------- /10-stl-containers/0-tuples/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/1-vectors/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/1-vectors/arrays.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | array numbers = {111, 222, 333}; // Array with a fixed size 8 | cout << numbers[1] << endl; 9 | cout << numbers[4] << endl; // junk 10 | cout << numbers.at(1) << endl; 11 | cout << numbers.at(4) << endl; // exception 12 | 13 | array, 5> strings; 14 | strings[2][3] = "abc"; 15 | cout << strings[2][3] << endl; 16 | } 17 | -------------------------------------------------------------------------------- /10-stl-containers/1-vectors/vector_iterators.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates the vector iterators 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | 15 | template 16 | class iterable_printer { 17 | const T& the_iterable; 18 | public: 19 | iterable_printer(const T& x): the_iterable(x) { 20 | cout << "constructor " << endl; 21 | } 22 | void print() { 23 | for (auto item: the_iterable) { 24 | cout << item << " "; 25 | } 26 | cout << endl; 27 | } 28 | }; 29 | 30 | 31 | int main() { 32 | vector v1; 33 | 34 | // vector v2(v1.begin(), v1.end()); // static assert 35 | 36 | auto v3 = vector({1,2,3,4}); 37 | iterable_printer(v3).print(); 38 | 39 | 40 | auto iter3 = v3.begin(); 41 | *iter3 = 5; 42 | 43 | const vector cv3; 44 | auto citer3 = cv3.begin(); 45 | //*citer3 = 5; 46 | } 47 | -------------------------------------------------------------------------------- /10-stl-containers/2-basic-strings/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/3-sets/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/3-sets/comparators_point.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates how to define and use different comparators. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | 14 | struct Point { 15 | int x, y; 16 | Point(int x, int y): x(x), y(y) {} 17 | }; 18 | 19 | ostream& operator<< (ostream& out, const Point& p) { 20 | return (out << "(" << p.x << "," << p.y << ")"); 21 | } 22 | 23 | struct PointCompare { 24 | bool operator() (Point a, Point b) const { 25 | return a.x < b.x || (a.x == b.x && a.y < b.y); 26 | } 27 | }; 28 | 29 | 30 | int main() { 31 | cout << endl << "Set of points: " << endl; 32 | set sp; 33 | sp.emplace(2,3); 34 | sp.emplace(1,5); 35 | sp.emplace(3,1); 36 | for (auto s: sp) 37 | cout << s << endl; 38 | 39 | cout << endl << "Set of points 2: " << endl; 40 | set sp2; 41 | // sp2.emplace(2,3); // Compile error 42 | // sp2.emplace(1,5); 43 | // sp2.emplace(3,1); 44 | for (auto s: sp2) 45 | cout << s << endl; 46 | } 47 | -------------------------------------------------------------------------------- /10-stl-containers/3-sets/loops.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates iteration on stl sets 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-05 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | set m { 14 | {"abc"}, 15 | {"xyz"}, 16 | }; 17 | m.insert("def"); 18 | m.insert("ghi"); 19 | 20 | // Old version: 21 | for (set::iterator iter = m.begin(); iter!=m.end(); ++iter) 22 | cout << *iter << endl; 23 | cout << endl; 24 | 25 | // C++11 version: 26 | for (const auto& val: m) 27 | cout << val << endl; 28 | cout << endl; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /10-stl-containers/3-sets/set_without_operator_less_than.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates how to use a set on a struct without operator<. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2021-06 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | template 13 | class MyTree { 14 | 15 | struct CompareAddresses { 16 | bool operator() (const T& lhs, const T& rhs) const { 17 | return (&lhs) < (&rhs); 18 | } 19 | }; 20 | 21 | set my_set; 22 | public: 23 | void insert(T item) { 24 | my_set.insert(item); 25 | } 26 | int size() { 27 | return my_set.size(); 28 | } 29 | }; 30 | 31 | 32 | 33 | struct Dummy {}; 34 | 35 | 36 | int main() { 37 | MyTree t_int; 38 | t_int.insert(5); // OK 39 | cout << t_int.size() << endl; 40 | 41 | MyTree t_dummy; 42 | t_dummy.insert(Dummy{}); 43 | cout << t_int.size() << endl; 44 | } 45 | -------------------------------------------------------------------------------- /10-stl-containers/4-maps/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/4-maps/find_bounds.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates iteration on stl maps 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-05 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main() { 13 | multimap m { 14 | {"abc", 2}, 15 | {"def", 4}, 16 | {"xyz", 6}, 17 | {"abc", 22}, 18 | {"def", 44}, 19 | {"xyz", 66}, 20 | }; 21 | // m["abc"]=222; 22 | 23 | for (auto [k,v]: m) 24 | cout << k << "," << v << endl; 25 | cout << endl; 26 | 27 | auto iter = m.find("abc"); 28 | cout << "find: " << iter->first << "," << iter->second << endl; 29 | cout << "count: " << m.count("abc") << endl; 30 | 31 | auto begin_abc = m.lower_bound("abc"); 32 | cout << "lower_bound: " << begin_abc->first << "," << begin_abc->second << endl; 33 | 34 | auto end_abc = m.upper_bound("abc"); 35 | cout << "upper_bound: " << end_abc->first << "," << end_abc->second << endl; 36 | 37 | cout << endl << "all abc in multimap: " << endl; 38 | for (auto i=begin_abc; i!=end_abc; ++i) 39 | cout << i->first << "," << i->second << endl; 40 | } 41 | -------------------------------------------------------------------------------- /10-stl-containers/4-maps/loops.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates iteration on stl maps 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-05 6 | */ 7 | 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | map m { 13 | {"abc", 2}, 14 | {"xyz", 8}, 15 | }; 16 | 17 | int main() { 18 | m["def"] = 4; 19 | m["ghi"] = 6; 20 | 21 | // Old version: 22 | for (map::iterator iter = m.begin(); iter!=m.end(); ++iter) 23 | cout << iter->first << "," << iter->second << endl; 24 | cout << endl; 25 | 26 | // C++11 version: 27 | for (const auto& pair: m) 28 | cout << pair.first << "," << pair.second << endl; 29 | cout << endl; 30 | 31 | // C++17 version: 32 | for (auto [key,value]: m) 33 | cout << key << "," << value << endl; 34 | cout << endl; 35 | } 36 | -------------------------------------------------------------------------------- /10-stl-containers/8-type-traits/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/8-type-traits/is_integral.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | template 9 | void test(Container container, BoolContainer v) { 10 | static_assert(is_integral::value, "BoolContainer must be of integral type"); 11 | } 12 | 13 | int main() { 14 | vector vi(4); 15 | vector vs(4); 16 | cout << is_integral::value << endl; 17 | cout << is_integral::value << endl; 18 | 19 | test(string("abcd"), vi); 20 | // test(string("abcd"), vs); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /10-stl-containers/8-type-traits/is_same.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | template 8 | bool is_almost_same_v = std::is_same_v; 9 | 10 | 11 | 12 | int main() { 13 | cout << boolalpha; 14 | cout << "is_same" << endl; 15 | cout << is_same_v << endl; // false 16 | cout << is_same_v << endl; // true 17 | cout << is_same_v << endl; // false 18 | cout << is_same_v << endl; // false 19 | cout << is_same_v << endl; // false 20 | 21 | cout << "is_almost_same" << endl; 22 | cout << is_almost_same_v << endl; // false 23 | cout << is_almost_same_v << endl; // true 24 | cout << is_almost_same_v << endl; // false 25 | cout << is_almost_same_v << endl; // true 26 | cout << is_almost_same_v << endl; // true 27 | } 28 | -------------------------------------------------------------------------------- /10-stl-containers/8-type-traits/vector_rvalue.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | template 8 | void f(T& t) { 9 | cout << typeid(t).name() << endl; 10 | } 11 | 12 | int main() { 13 | int i; 14 | vector v; 15 | f(i); 16 | f(v); 17 | // f(5); 18 | f(vector{1,2,3}); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /10-stl-containers/9-riddles/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/9-riddles/input.txt: -------------------------------------------------------------------------------- 1 | the quick brown fox jumped over the lazy dog 2 | and then it jumped again 3 | and again 4 | because it enjoyed jumping over the lazy dog 5 | what kind of fox is that if it enjoys jumping over dogs 6 | it's a really sad fox 7 | a pretty sad fox indeed 8 | -------------------------------------------------------------------------------- /10-stl-containers/old/5-valarrays/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/old/5-valarrays/valarray.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main() { 7 | valarray a {1,2,3}; 8 | valarray b {4,5,6}; 9 | valarray c = a+b; 10 | // cout << c << endl; 11 | cout << c[0] << "," << c[1] << "," << c[2] << endl; 12 | } 13 | -------------------------------------------------------------------------------- /10-stl-containers/old/6-static-vector/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /10-stl-containers/old/6-static-vector/static-vector.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | struct MyClass { 6 | static vector myvector; // only a declaration, not a definition 7 | }; 8 | 9 | vector MyClass::myvector{10,20,30}; 10 | 11 | int main() { 12 | MyClass a; 13 | MyClass b; 14 | cout << MyClass::myvector.size() << endl; 15 | cout << MyClass::myvector[0] << endl; 16 | } 17 | -------------------------------------------------------------------------------- /10-stl-containers/slides-containers-looping.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/10-stl-containers/slides-containers-looping.odp -------------------------------------------------------------------------------- /10-stl-containers/slides-containers-looping.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/10-stl-containers/slides-containers-looping.pdf -------------------------------------------------------------------------------- /10-stl-containers/slides-containers.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/10-stl-containers/slides-containers.odp -------------------------------------------------------------------------------- /10-stl-containers/slides-containers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/10-stl-containers/slides-containers.pdf -------------------------------------------------------------------------------- /10-stl-containers/text-containers.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/10-stl-containers/text-containers.odt -------------------------------------------------------------------------------- /10-stl-containers/text-containers.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/10-stl-containers/text-containers.pdf -------------------------------------------------------------------------------- /11-stl-algorithms/01-io-iterators/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/01-io-iterators/istream-iterator.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates IO iterators with STL containers. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2018-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() { 17 | // example of istream_iterator: 18 | istringstream str("0.12 0.2 \n 0.3 0.4"); 19 | // reminder: 20 | // double d; 21 | // str >> d; 22 | // cout << d << endl; 23 | istream_iterator ibegin {str}; 24 | istream_iterator iend {}; 25 | cout << "istream_iterator: "; 26 | for (auto i=ibegin; i!=iend; ++i) 27 | cout << (*i) << endl; 28 | cout << endl; 29 | 30 | // cout << *(find(istream_iterator(str), istream_iterator{} , 0.3)); 31 | 32 | // example of istream_iterator + copy: 33 | istringstream str2("8 5 7 1"); 34 | vector v1(4); 35 | // copy(istream_iterator(cin), istream_iterator{} , v1.begin()); 36 | copy(istream_iterator(str2), istream_iterator{}, v1.begin()); 37 | for (auto i: v1) 38 | cout << i << endl; 39 | } -------------------------------------------------------------------------------- /11-stl-algorithms/03-insert-erase/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/03-insert-erase/insert_one.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates how to insert an item into a container. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-05 5 | */ 6 | 7 | 8 | #include "output_containers.hpp" 9 | using namespace std; 10 | 11 | int main() { 12 | { 13 | vector v {1,2,4,7,11,16,22}; 14 | cout << v << endl; 15 | v.insert(v.begin(),0); 16 | cout << v << endl; 17 | auto it = v.begin() + 4; 18 | cout << "*it==" << *it << endl; 19 | v.insert(it,5); 20 | cout << v << endl; 21 | cout << "*it==" << *it << endl; 22 | v.insert(it,6); 23 | cout << "*it==" << *it << endl; 24 | cout << v << endl; 25 | 26 | //int capacity_before = v.capacity(); 27 | v.emplace_back(99); 28 | v.emplace_back(99); 29 | v.emplace_back(99); 30 | v.emplace_back(99); 31 | v.emplace_back(99); 32 | v.emplace_back(99); 33 | v.emplace_back(99); 34 | //int capacity_after = v.capacity(); 35 | cout << v << endl; 36 | v.insert(it,55); 37 | cout << v << endl; 38 | cout << "end" << endl; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /11-stl-algorithms/03-insert-erase/insert_range.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates how to insert a range of items into a container. 3 | * @author Erel Segal-Halevi 4 | * @since 2018-05 5 | */ 6 | 7 | 8 | #include "output_containers.hpp" 9 | using namespace std; 10 | 11 | template _List_iterator operator+ (_List_iterator it, int inc) { 12 | if (inc<0) { 13 | for (int i=0; i<-inc; ++i) 14 | --it; 15 | } else { 16 | for (int i=0; i v {1,2,4,7,11,16,22}; 26 | list l {99,66,11,44,88,55,33}; 27 | cout << "v: " << v << endl; 28 | cout << "l: " << l << endl; 29 | 30 | l.insert(l.begin(), v.begin()+2, v.begin()+5); 31 | cout << "l: " << l << endl; 32 | } 33 | { 34 | vector v {1,2,4,7,11,16,22}; 35 | list l {99,66,11,44,88,55,33}; 36 | cout << "v: " << v << endl; 37 | cout << "l: " << l << endl; 38 | 39 | v.insert(v.begin(), l.begin()+2, l.begin()+5); 40 | cout << "v: " << v << endl; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /11-stl-algorithms/03-insert-erase/loop-past-the-end.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates what happens when looping past the end of a container. 3 | * @author Erel Segal-Halevi 4 | * @since 2019-06 5 | */ 6 | 7 | #include 8 | #include "output_containers.hpp" 9 | using namespace std; 10 | 11 | int main() { 12 | vector v {1,2,4}; 13 | auto i = v.begin(); cout << *i << endl; // 1 14 | cout << " capacity == " << v.capacity() << endl; 15 | ++i; cout << *i << endl; // 2 16 | ++i; cout << *i << endl; // 4 17 | ++i; cout << *i << endl; // undefined, but sometimes prints 0 18 | for (int j=0; j<1000; ++j) { 19 | cout << j << " "; 20 | ++i; cout << *i << endl; // undefined, but sometimes prints 0 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /11-stl-algorithms/03-insert-erase/output_containers.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace std; 8 | 9 | 10 | template 11 | ostream& operator<< (ostream& out, const vector& c) { 12 | for (int i: c) 13 | out << i << ' '; 14 | return out; 15 | } 16 | template 17 | ostream& operator<< (ostream& out, const set& c) { 18 | for (int i: c) 19 | out << i << ' '; 20 | return out; 21 | } 22 | 23 | template 24 | ostream& operator<< (ostream& out, const list& c) { 25 | for (int i: c) 26 | out << i << ' '; 27 | return out; 28 | } 29 | 30 | template 31 | ostream& operator<< (ostream& out, const deque& c) { 32 | for (int i: c) 33 | out << i << ' '; 34 | return out; 35 | } 36 | -------------------------------------------------------------------------------- /11-stl-algorithms/10-queries/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/10-queries/two_range_queries.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL two-range query algorithms. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "output_containers.hpp" 15 | using namespace std; 16 | 17 | 18 | 19 | int main() { 20 | cout << boolalpha; 21 | vector v1{10,11,12,13,14,15,16,17,18,19}; 22 | list v2{10,11,12,18,14,15,16,17,13,19}; 23 | cout << "equal(v1,v2): " << equal(begin(v1),end(v1),begin(v2),end(v2)) << endl; 24 | cout << "is_permutation(v1,v2): " << is_permutation(begin(v1),end(v1),begin(v2),end(v2)) << endl; 25 | cout << "lexicographical_compare(v1,v2): " << lexicographical_compare(begin(v1),end(v1),begin(v2),end(v2)) << endl; // is v1 < v2? 26 | cout << "lexicographical_compare(v2,v1): " << lexicographical_compare(begin(v2),end(v2),begin(v1),end(v1)) << endl; // is v2 < v1? 27 | 28 | auto [iter1,iter2] = mismatch(begin(v1)+4,end(v1),++++++++begin(v2),end(v2)); 29 | cout << "mismatch-1: " << *iter1 << endl; 30 | cout << "mismatch-2: " << *iter2 << endl; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /11-stl-algorithms/11-set-algorithms/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/11-set-algorithms/file1.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | abc 10 | -------------------------------------------------------------------------------- /11-stl-algorithms/11-set-algorithms/file2.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 3 3 | 5 4 | 7 5 | 9 6 | 11 7 | 13 8 | -------------------------------------------------------------------------------- /11-stl-algorithms/11-set-algorithms/union_file.txt: -------------------------------------------------------------------------------- 1 | 1 2 | 1 3 | 2 4 | 3 5 | 4 6 | 5 7 | 6 8 | 7 9 | 9 10 | 11 11 | 13 12 | abc 13 | -------------------------------------------------------------------------------- /11-stl-algorithms/12-permutations/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/12-permutations/permutations.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates "next_permutation", "prev_permutation" and "reverse". 3 | */ 4 | 5 | #include "output_containers.hpp" 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | int main() { 12 | string v {"abdc"}; 13 | next_permutation(v.begin(),v.end()); 14 | cout << v << endl; 15 | sort(begin(v), end(v)); 16 | cout << "Permutations in increasing order:" << endl; 17 | do { 18 | cout << v << endl; 19 | } while ( next_permutation(v.begin(),v.end()) ); 20 | 21 | reverse(begin(v), end(v)); 22 | cout << endl << "Permutations in decreasing order:" << endl; 23 | do { 24 | cout << v << endl; 25 | } while ( prev_permutation(v.begin(),v.end()) ); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /11-stl-algorithms/12-permutations/rotate.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL rotate algorithms. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "output_containers.hpp" 14 | using namespace std; 15 | 16 | 17 | 18 | int main() { 19 | vector v{9,8,6,7,4,5,2,0,3,1}; // OK 20 | cout << "start: " << v << endl; 21 | 22 | rotate(begin(v), begin(v)+3, end(v)); 23 | cout << "rotated(3): " << v << endl; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /11-stl-algorithms/12-permutations/shuffle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL shuffle algorithm. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "output_containers.hpp" 15 | using namespace std; 16 | 17 | 18 | 19 | int main() { 20 | vector v{0,1,2,3,4,5,6,7,8,9}; 21 | cout << "start: " << v << endl; 22 | 23 | auto v2 = v; 24 | 25 | shuffle(begin(v), end(v), default_random_engine{1}); 26 | cout << "shuffled(1): " << v << endl; 27 | 28 | shuffle(begin(v2), end(v2), default_random_engine{1}); 29 | cout << "shuffled(1): " << v2 << endl; 30 | 31 | shuffle(begin(v), end(v), default_random_engine{2}); 32 | cout << "shuffled(2): " << v << endl; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /11-stl-algorithms/13-movers/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/13-movers/sample.cpp: -------------------------------------------------------------------------------- 1 | // example from https://en.cppreference.com/w/cpp/algorithm/sample 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() 11 | { 12 | string in = "abcdefgh", out; 13 | sample(in.begin(), in.end(), back_inserter(out), 5, default_random_engine(1)); 14 | cout << "five random letters out of " << in << " : " << out << '\n'; 15 | sample(in.begin(), in.end(), begin(out), 5, default_random_engine(7)); 16 | cout << "five random letters out of " << in << " : " << out << '\n'; 17 | 18 | sample(in.begin(), in.end(), ostream_iterator(cout), 5, default_random_engine(9)); 19 | } -------------------------------------------------------------------------------- /11-stl-algorithms/13-movers/swap.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL swap_ranges algorithm 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "output_containers.hpp" 14 | using namespace std; 15 | 16 | 17 | 18 | int main() { 19 | cout << boolalpha; 20 | vector v{4,6,8,5,2,0,3,1,9,7}; 21 | vector v2{1,1,1,1,1}; 22 | cout << "v: " << v << endl; 23 | cout << "v2: " << v2 << endl; 24 | cout << v2[5] << " " << v2[6] << " " << v2[7] << " " << v2[8] << " " << v2[9] << endl; 25 | swap_ranges(begin(v), end(v), begin(v2)); 26 | cout << "v: " << v << endl; 27 | cout << "v2: " << v2 << endl; 28 | cout << v2[5] << " " << v2[6] << " " << v2[7] << " " << v2[8] << " " << v2[9] << endl; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /11-stl-algorithms/14-value-modifiers/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/14-value-modifiers/fill_iota.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL fill and iota algorithms. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "output_containers.hpp" 15 | using namespace std; 16 | 17 | struct Person { 18 | int age; 19 | 20 | Person& operator++() { 21 | age++; 22 | return *this; 23 | } 24 | }; 25 | 26 | 27 | int main() { 28 | vector v(10); 29 | cout << "v: " << v << endl; 30 | fill(begin(v), end(v), 42); 31 | cout << "fill(42): " << v << endl; 32 | iota(begin(v), end(v), 42); 33 | cout << "iota(42): " << v << endl << endl; 34 | 35 | vector vc(10); 36 | fill(begin(vc), end(vc), 'b'); 37 | cout << "fill(b): " << vc << endl; 38 | iota(begin(vc), end(vc), 'b'); 39 | cout << "iota(b): " << vc << endl; 40 | 41 | // iota(ostream_iterator(cout), ostream_iterator{}, 52); 42 | 43 | vector vp(10); 44 | iota(begin(vp), end(vp), Person{42}); 45 | cout << vp[2].age << endl; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /11-stl-algorithms/14-value-modifiers/replace.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL replace algorithm. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include "output_containers.hpp" 15 | using namespace std; 16 | 17 | 18 | 19 | int main() { 20 | vector v {1,8,4,6,1,9,3,5,1,6}; 21 | cout << "v: " << v << endl; 22 | replace(begin(v), end(v), 1, 1111); 23 | cout << "replace 1 by 1111: " << v << endl; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /11-stl-algorithms/15-runes/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/15-runes/X_if.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL _if rune. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "output_containers.hpp" 14 | using namespace std; 15 | 16 | 17 | 18 | int main() { 19 | vector v{4,6,8,5,4,1,3,1,4,0}; 20 | cout << "v = " << v << endl; 21 | cout << "count_if(even): " << count_if(begin(v),end(v), [](int x){return x%2==0;}) << endl; 22 | cout << "count_if(odd): " << count_if(begin(v),end(v), [](int x){return x%2!=0;}) << endl << endl; 23 | 24 | vector v_even; 25 | copy_if(begin(v), end(v), back_inserter(v_even), [](int x){return x%2==0;}); 26 | cout << "copy_if(even): " << v_even << endl << endl; 27 | 28 | replace_if(begin(v), end(v), [](int x){return x%2==0;}, 999); 29 | cout << "replace_if(even): " << v << endl; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /11-stl-algorithms/16-structure-changers/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/16-structure-changers/unique.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL unique algorithm. 3 | * 4 | * @author Erel Segal-Halevi 5 | * @since 2019-05 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "output_containers.hpp" 16 | using namespace std; 17 | 18 | 19 | 20 | int main() { 21 | vector v {11,8,5,6,11,11,11,9,3,5,5,11,6}; 22 | auto v2 = v; 23 | cout << "v: " << v << endl; 24 | auto new_end = unique(begin(v), end(v)); 25 | 26 | cout << "v after unique: " << v << endl; 27 | v.erase(new_end, end(v)); 28 | cout << "v after erase: " << v << endl; 29 | 30 | vector v3; 31 | unique_copy(begin(v2), end(v2), back_inserter(v3)); 32 | cout << "v2 after unique_copy: " << v2 << endl; 33 | cout << "v3 after unique_copy: " << v3 << endl; 34 | 35 | cout << "\n unique_copy using unordered_set: " << endl; 36 | unordered_set vs; 37 | vector vsv; 38 | copy(begin(v), end(v), inserter(vs, vs.end())); 39 | copy(begin(vs), end(vs), back_inserter(vsv)); 40 | cout << "v after unordered_set: " << vsv << endl; 41 | } 42 | 43 | -------------------------------------------------------------------------------- /11-stl-algorithms/17-foreach-transform/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /11-stl-algorithms/17-foreach-transform/for_each.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL for_each algorithms. 3 | * From here: https://en.cppreference.com/w/cpp/algorithm/for_each 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | 13 | int main() { 14 | vector nums{3, 4, 2, 8, 15, 267}; 15 | 16 | auto print = [](int n) { cout << " " << n; }; 17 | 18 | cout << "before:"; 19 | for_each(nums.begin(), nums.end(), print); 20 | cout << '\n'; 21 | 22 | for_each(nums.begin(), nums.end(), [](int &n){ n++; }); 23 | 24 | cout << "after: "; 25 | for_each(nums.begin(), nums.end(), print); 26 | } -------------------------------------------------------------------------------- /11-stl-algorithms/17-foreach-transform/transform.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates STL transform algorithms. 3 | * From here: https://en.cppreference.com/w/cpp/algorithm/transform 4 | */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main() 13 | { 14 | // transform with one sequence: 15 | string s("hello"); 16 | transform( 17 | s.begin(), s.end(), 18 | s.begin(), 19 | [](unsigned char c) { return toupper(c); }); 20 | 21 | vector ordinals; 22 | transform( 23 | s.begin(), s.end(), 24 | back_inserter(ordinals), 25 | [](unsigned char c) { return (int)c; }); 26 | 27 | cout << s << ':'; 28 | for (auto ord : ordinals) 29 | cout << ' ' << ord; 30 | cout << endl << endl; 31 | 32 | // transform with two sequences: 33 | vector v1{1,2,3,4,5,6}; 34 | vector v2{6,5,4,3,2,1}; 35 | vector v3; 36 | transform(begin(v1), end(v1), begin(v2), back_inserter(v3), 37 | [](int x, int y){return x*y;}); 38 | for (auto i : v3) 39 | cout << i << " "; 40 | cout << endl; 41 | 42 | } -------------------------------------------------------------------------------- /11-stl-algorithms/slides-iterators-validity.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/slides-iterators-validity.odp -------------------------------------------------------------------------------- /11-stl-algorithms/slides-iterators-validity.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/slides-iterators-validity.pdf -------------------------------------------------------------------------------- /11-stl-algorithms/slides-iterators.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/slides-iterators.odp -------------------------------------------------------------------------------- /11-stl-algorithms/slides-iterators.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/slides-iterators.pdf -------------------------------------------------------------------------------- /11-stl-algorithms/slides-stl-algorithms.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/slides-stl-algorithms.odp -------------------------------------------------------------------------------- /11-stl-algorithms/slides-stl-algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/slides-stl-algorithms.pdf -------------------------------------------------------------------------------- /11-stl-algorithms/text-iterators.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/text-iterators.odt -------------------------------------------------------------------------------- /11-stl-algorithms/text-iterators.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/text-iterators.pdf -------------------------------------------------------------------------------- /11-stl-algorithms/text-stl-algorithms.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/text-stl-algorithms.odt -------------------------------------------------------------------------------- /11-stl-algorithms/text-stl-algorithms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/11-stl-algorithms/text-stl-algorithms.pdf -------------------------------------------------------------------------------- /12-smart-pointers/1-musician/AutoPointer.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * A demo implementation of an automatic pointer. 3 | * 4 | * Author: Eran Kaufmann 5 | * Since: 2020-06 6 | */ 7 | 8 | template class AutoPointer { 9 | T* ptr = nullptr; 10 | 11 | public: 12 | AutoPointer()=default; 13 | 14 | // Convert from C pointer: 15 | AutoPointer(T* new_ptr): 16 | ptr(new_ptr) {} 17 | 18 | AutoPointer& operator=(T* new_ptr) { 19 | if (ptr!=nullptr) delete ptr; 20 | ptr = new_ptr; 21 | return *this; 22 | } 23 | 24 | // Delete when out of scope: 25 | ~AutoPointer() { 26 | if (ptr!=nullptr) delete ptr; 27 | } 28 | 29 | // Behave like C pointer: 30 | T* operator->(){ 31 | return ptr; 32 | } 33 | 34 | T& operator*(){ 35 | return *ptr; 36 | } 37 | 38 | // Do not allow to duplicate: 39 | AutoPointer(const AutoPointer& other) = delete; 40 | AutoPointer& operator=(const AutoPointer& other) = delete; 41 | AutoPointer(AutoPointer& other) = delete; 42 | AutoPointer& operator=(AutoPointer& other) = delete; 43 | }; 44 | -------------------------------------------------------------------------------- /12-smart-pointers/1-musician/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) musician.cpp 16 | HEADERS=AutoPointer.hpp UniquePointer.hpp SharedPointer.hpp musician.hpp 17 | 18 | all: $(MAINEXECUTABLE) 19 | $(MAINEXECUTABLE) 20 | 21 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 22 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 23 | 24 | clean: 25 | $(RM) *.exe a.out *.class 26 | -------------------------------------------------------------------------------- /12-smart-pointers/1-musician/musician.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "musician.hpp" 3 | 4 | using namespace std; 5 | 6 | const int Musician::M_MAX_CONCURRENTLY_PLAYING (5); 7 | int Musician::m_currentPlayingMusicians (0); 8 | 9 | Musician::Musician (string name): name(name) { 10 | cout << "Hello, I am " << name << ", a new musician!" << endl; 11 | } 12 | 13 | Musician::~Musician () { 14 | cout << "Musician " << name << " is deleted..." << endl; 15 | } 16 | 17 | void Musician::play() const //throw (TooNoisy) 18 | { 19 | if (++m_currentPlayingMusicians > M_MAX_CONCURRENTLY_PLAYING) 20 | throw TooNoisy (m_currentPlayingMusicians); 21 | } 22 | -------------------------------------------------------------------------------- /12-smart-pointers/1-musician/musician.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "tooNoisy.h" 4 | 5 | class Musician { 6 | std::string name; 7 | public: 8 | Musician (std::string name); 9 | ~Musician (); 10 | void play () const; 11 | private: 12 | static const int M_MAX_CONCURRENTLY_PLAYING; 13 | static int m_currentPlayingMusicians; 14 | }; 15 | -------------------------------------------------------------------------------- /12-smart-pointers/1-musician/old/smartPointer.cpp: -------------------------------------------------------------------------------- 1 | #include "smartPointer.h" 2 | 3 | MusicianSmartPointer::MusicianSmartPointer (Musician* pMusician) 4 | : m_pMusician (pMusician) 5 | {} 6 | 7 | MusicianSmartPointer::~MusicianSmartPointer () 8 | { 9 | delete m_pMusician; 10 | } 11 | 12 | -------------------------------------------------------------------------------- /12-smart-pointers/1-musician/old/smartPointer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "musician.h" 5 | 6 | using namespace std; 7 | 8 | class MusicianSmartPointer 9 | { 10 | public: 11 | MusicianSmartPointer (Musician* pMusician = 0); 12 | ~MusicianSmartPointer (); 13 | 14 | inline MusicianSmartPointer& operator= (Musician* pMusician); 15 | 16 | inline Musician& operator*() const; 17 | inline Musician* operator->() const; 18 | inline Musician* get() const; 19 | 20 | private: 21 | Musician* m_pMusician; 22 | // Preventing copy and assignment operations 23 | MusicianSmartPointer (const MusicianSmartPointer&); 24 | MusicianSmartPointer& operator = (const MusicianSmartPointer&); 25 | }; 26 | 27 | 28 | inline MusicianSmartPointer& MusicianSmartPointer::operator = (Musician* pMusician) 29 | { 30 | m_pMusician = pMusician; 31 | return *this; 32 | } 33 | 34 | inline Musician& MusicianSmartPointer::operator*() const 35 | { 36 | return *m_pMusician; 37 | } 38 | 39 | inline Musician* MusicianSmartPointer::operator->() const 40 | { 41 | return m_pMusician; 42 | } 43 | 44 | inline Musician* MusicianSmartPointer::get() const 45 | { 46 | return m_pMusician; 47 | } 48 | -------------------------------------------------------------------------------- /12-smart-pointers/1-musician/tooNoisy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class TooNoisy { 4 | public: 5 | inline TooNoisy (int numMusicians); 6 | inline int getNumMusicians(); 7 | private: 8 | int m_numMusicians; 9 | }; 10 | 11 | 12 | inline TooNoisy::TooNoisy (int numMusicians) : m_numMusicians (numMusicians) 13 | {} 14 | 15 | inline int TooNoisy::getNumMusicians() 16 | { 17 | return m_numMusicians; 18 | } 19 | -------------------------------------------------------------------------------- /12-smart-pointers/2-move-rvalue-reference/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /12-smart-pointers/2-move-rvalue-reference/move_demo.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates the difference between lvalue and rvalue-reference, 3 | * and between copy-semantics and move-semantics. 4 | * 5 | * Author: Erel Segal-Halevi 6 | * Since: 2020-06 7 | * 8 | * For more details see https://stackoverflow.com/a/3109981/827927 by Fred Overflow 9 | */ 10 | 11 | #include 12 | using namespace std; 13 | 14 | void f(int& i) { 15 | cout << "by reference" << endl; 16 | } 17 | 18 | void f(const int& i) { 19 | cout << "by const reference" << endl; 20 | } 21 | 22 | void f(int&& i) { 23 | cout << "by rvalue reference" << endl; 24 | } 25 | 26 | 27 | int main() { 28 | int x; 29 | const int y = 5; 30 | 31 | cout << "f(x): "; 32 | f(x); 33 | 34 | cout << "f(y): "; 35 | f(y); 36 | 37 | cout << "f(x+y): "; 38 | f(x+y); 39 | 40 | cout << "f(move(x)): "; 41 | f(move(x)); 42 | 43 | cout << "f(move(y)): "; 44 | f(move(y)); 45 | 46 | cout << "f(move(x+y)): "; 47 | f(move(x+y)); 48 | } 49 | -------------------------------------------------------------------------------- /12-smart-pointers/2-move-rvalue-reference/move_demo_operators.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates the difference between lvalue and rvalue-reference, 3 | * and between copy-semantics and move-semantics. 4 | * 5 | * Author: Erel Segal-Halevi 6 | * Since: 2020-06 7 | * 8 | * For more details see https://stackoverflow.com/a/3109981/827927 by Fred Overflow 9 | */ 10 | 11 | #include 12 | using namespace std; 13 | 14 | class Test { 15 | public: 16 | Test() = default; 17 | Test(int i) { cout << "conversion from int" << endl; } 18 | Test& operator=(const Test& other) { 19 | cout << "deep copy" << endl; 20 | return *this; 21 | } 22 | Test& operator=(Test&& other) { 23 | cout << "shallow copy (should nullify other)" << endl; 24 | return *this; 25 | } 26 | }; 27 | 28 | Test make_test() { 29 | return Test{}; 30 | } 31 | 32 | 33 | int main() { 34 | Test x; 35 | Test y; 36 | 37 | y = x; 38 | y = Test{}; 39 | y = make_test(); 40 | y = move(make_test()); 41 | y = move(x); 42 | 43 | int i; 44 | y = i; 45 | } 46 | -------------------------------------------------------------------------------- /12-smart-pointers/3-stack/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /12-smart-pointers/4-weak_ptr/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /12-smart-pointers/4-weak_ptr/employee-manager.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates the need for weak_ptr. 3 | * 4 | * Author: Eran Kaufmann. 5 | * Since: 2020. 6 | */ 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | 16 | struct Employee; 17 | 18 | struct Manager{ 19 | vector> employees; 20 | // vector> employees; 21 | void ask_for_vacation() {} 22 | ~Manager() { 23 | // for(auto& emp:employees) 24 | // emp->manager=nullptr; 25 | {cout << "Manager I'm done \n";}; 26 | } 27 | }; 28 | 29 | struct Employee{ 30 | 31 | int id; 32 | string name; 33 | weak_ptr manager; 34 | // shared_ptr manager; 35 | 36 | void take_vacation(){ 37 | if(auto p = manager.lock()) 38 | if(!manager.expired()){ 39 | auto p = manager.lock(); 40 | p -> ask_for_vacation(); 41 | } 42 | } 43 | 44 | ~Employee(){cout << "Employee I'm done \n";}; 45 | 46 | }; 47 | 48 | 49 | 50 | int main(){ 51 | auto bob = make_shared(); 52 | auto kate = make_shared(); 53 | bob->employees.push_back(kate); 54 | kate->manager=bob; 55 | } 56 | -------------------------------------------------------------------------------- /12-smart-pointers/5-playground/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /12-smart-pointers/5-playground/playground1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int main(){ 9 | unique_ptr ptr;//(new std::string("hello")); 10 | unique_ptr ptr2 (new std::string("hello")); 11 | ptr = move(ptr2); 12 | vector> v; 13 | v.push_back(move(ptr)); 14 | 15 | shared_ptr p(new std::string("hello")); 16 | 17 | vector> vs; 18 | { 19 | shared_ptr p2; 20 | p2=p; 21 | 22 | } 23 | vs.push_back(p); 24 | p=nullptr; 25 | vs.clear(); 26 | 27 | auto sp = make_shared("hi"); 28 | } 29 | -------------------------------------------------------------------------------- /12-smart-pointers/slides-eran.odp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/12-smart-pointers/slides-eran.odp -------------------------------------------------------------------------------- /12-smart-pointers/slides-eran.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/12-smart-pointers/slides-eran.pdf -------------------------------------------------------------------------------- /12-smart-pointers/slides-eran.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/12-smart-pointers/slides-eran.pptx -------------------------------------------------------------------------------- /12-smart-pointers/text.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/12-smart-pointers/text.odt -------------------------------------------------------------------------------- /12-smart-pointers/text.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/12-smart-pointers/text.pdf -------------------------------------------------------------------------------- /13-python-integration/0-prerequisites/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # AUTHOR: Erel Segal-Halevi 3 | 4 | CXX=clang++-9 5 | CXXFLAGS=-std=c++2a 6 | RM=rm -f 7 | 8 | ifndef MAIN 9 | MAIN=./main.cpp 10 | endif 11 | 12 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 13 | 14 | SOURCES=$(MAIN) 15 | 16 | all: $(MAINEXECUTABLE) 17 | ./$(MAINEXECUTABLE) 18 | 19 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 20 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 21 | 22 | clean: 23 | $(RM) *.exe a.out *.class 24 | -------------------------------------------------------------------------------- /13-python-integration/0-prerequisites/string_view.cpp: -------------------------------------------------------------------------------- 1 | // Test the existence of the string_view library 2 | 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | string_view world("world"); 10 | cout << "hello " << world << endl; 11 | } 12 | -------------------------------------------------------------------------------- /13-python-integration/1-python-introduction/__pycache__/factorial.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/1-python-introduction/__pycache__/factorial.cpython-35.pyc -------------------------------------------------------------------------------- /13-python-integration/1-python-introduction/factorial.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | def factorial(n): 4 | """ 5 | Return n! - the factorial of n. 6 | 7 | >>> factorial(1) 8 | 1 9 | >>> factorial(0) 10 | 1 11 | >>> factorial(3) 12 | 6 13 | 14 | """ 15 | if n<=0: 16 | return 0 17 | elif n==1: 18 | return 1 19 | else: 20 | return n*factorial(n-1) 21 | 22 | 23 | if __name__ == "__main__": 24 | import doctest 25 | (failures,tests) = doctest.testmod(report=True) 26 | print ("{} failures, {} tests".format(failures,tests)) 27 | 28 | x=5 29 | y=7 30 | x,y = y,x 31 | print("x={} y={}".format(x,y)) 32 | -------------------------------------------------------------------------------- /13-python-integration/1-python-introduction/factorial_demo.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | import factorial 4 | 5 | def test(x:int, y:int)->tuple: 6 | return (x-y,x+y) 7 | 8 | 9 | print(factorial.factorial(5)) 10 | #print(test(y="abc",x="xyz")) 11 | print(test(5,7)) 12 | # print(factorial.factorial("abc")) 13 | -------------------------------------------------------------------------------- /13-python-integration/1-python-introduction/hello.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | # single-line string: 4 | print("hello") 5 | 6 | # multi-line strings: 7 | print(""" 8 | w 9 | o 10 | r 11 | l 12 | d 13 | """ 14 | ) 15 | 16 | print("abc"*3) 17 | 18 | a = [1,2,3,4] 19 | print(a*3) 20 | -------------------------------------------------------------------------------- /13-python-integration/2-cppyy-introduction/hello.hpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void say_hello() { 4 | std::cout << "Hello Python!" << std::endl; 5 | } 6 | -------------------------------------------------------------------------------- /13-python-integration/2-cppyy-introduction/hello.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | import cppyy 4 | 5 | cppyy.cppdef(""" 6 | void say_hello() { 7 | std::cout << "Hello Python!" << std::endl; 8 | } 9 | """) 10 | 11 | cppyy.gbl.say_hello() 12 | 13 | print("Hello C++!") 14 | -------------------------------------------------------------------------------- /13-python-integration/2-cppyy-introduction/include.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | import cppyy 4 | cppyy.include("hello.hpp") 5 | cppyy.gbl.say_hello() 6 | print("Hello C++!") 7 | 8 | -------------------------------------------------------------------------------- /13-python-integration/3-permutations/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CXXFLAGS=-std=c++2a -O3 -ffp-contract=off -fno-expensive-optimizations 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CXXFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /13-python-integration/3-permutations/__pycache__/count.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/3-permutations/__pycache__/count.cpython-35.pyc -------------------------------------------------------------------------------- /13-python-integration/3-permutations/__pycache__/count_permutations.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/3-permutations/__pycache__/count_permutations.cpython-35.pyc -------------------------------------------------------------------------------- /13-python-integration/3-permutations/__pycache__/traveling_salesman.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/3-permutations/__pycache__/traveling_salesman.cpython-35.pyc -------------------------------------------------------------------------------- /13-python-integration/3-permutations/__pycache__/traveling_salesman.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/3-permutations/__pycache__/traveling_salesman.cpython-38.pyc -------------------------------------------------------------------------------- /13-python-integration/3-permutations/count_compare.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | import time 4 | 5 | import count_permutations 6 | 7 | import cppyy 8 | cppyy.include("count_permutations.cpp") 9 | 10 | use_cpp = True 11 | 12 | if use_cpp: 13 | print("Using C++ implementation") 14 | else: 15 | print("Using Python implementation") 16 | 17 | for N in range(5,14): 18 | print ("Permutations of 1..{}:".format(N), flush=True) 19 | start = time.time() 20 | if use_cpp: 21 | count = cppyy.gbl.count_permutations(N) 22 | else: 23 | count = count_permutations.count_permutations(N) 24 | end = time.time() 25 | duration_in_seconds = end-start 26 | print(" {} permutations calculated in {} seconds".format(count, duration_in_seconds), flush=True) 27 | -------------------------------------------------------------------------------- /13-python-integration/3-permutations/count_permutations.cpp: -------------------------------------------------------------------------------- 1 | #include "output_containers.hpp" 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int count_permutations(int N) { 7 | vector v(N); 8 | iota(v.begin(), v.end(), 1); // fill the vector with 1,...,N 9 | int count=0; 10 | do { 11 | ++count; 12 | } while ( next_permutation(v.begin(),v.end()) ); 13 | return count; 14 | } 15 | 16 | int main() { 17 | for (int N=1; N<14; ++N) { 18 | cout << "Permutations of 1.." << N << ":" << endl; 19 | auto begin = time(NULL); 20 | auto count = count_permutations(N); 21 | auto end = time(NULL); 22 | auto duration = end-begin; 23 | cout << " " << count << " permutations calculated in " << duration << " seconds" << endl; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /13-python-integration/3-permutations/count_permutations.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | import itertools 4 | import time 5 | 6 | def count_permutations(N:int)->int: 7 | count=0 8 | for p in itertools.permutations(range(1,N+1)): 9 | count += 1 10 | # print(p) 11 | return count 12 | 13 | if __name__=="__main__": 14 | for N in range(1,13): 15 | print ("Permutations of 1..{}:".format(N), flush=True) 16 | start = time.time() 17 | count = count_permutations(N) 18 | end = time.time() 19 | duration_in_seconds = end-start 20 | print(" {} permutations calculated in {} seconds".format(count, duration_in_seconds), flush=True) 21 | 22 | -------------------------------------------------------------------------------- /13-python-integration/3-permutations/demo.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Demonstrates "next_permutation" and "iota". 3 | */ 4 | 5 | #include "output_containers.hpp" 6 | #include 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | { 12 | vector v(4); 13 | iota(v.begin(), v.end(), 1); // fill the vector with 1,...,n 14 | cout << "Permutations of iota:" << endl; 15 | do { 16 | cout << v << endl; 17 | } while ( next_permutation(v.begin(),v.end()) ); 18 | } 19 | { 20 | string v {"abcd"}; 21 | cout << "Permutations of string:" << endl; 22 | do { 23 | cout << v << endl; 24 | } while ( next_permutation(v.begin(),v.end()) ); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /13-python-integration/3-permutations/demo.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | print("Permutations or range:") 4 | for p in itertools.permutations(range(1,5)): 5 | print(p) 6 | 7 | print("Permutations or string:") 8 | for p in itertools.permutations("abcd"): 9 | print("".join(p)) 10 | 11 | -------------------------------------------------------------------------------- /13-python-integration/3-permutations/traveling_compare.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | import time 4 | 5 | import traveling_salesman 6 | 7 | use_cpp = True 8 | 9 | if use_cpp: 10 | print("Using C++ implementation") 11 | import cppyy 12 | cppyy.include("traveling_salesman.cpp") 13 | find_shortest_path = cppyy.gbl.find_shortest_path 14 | else: 15 | print("Using Python implementation") 16 | find_shortest_path = traveling_salesman.find_shortest_path 17 | 18 | distances = [ 19 | [1,2,3], 20 | [4,5,6], 21 | [7,8,9] 22 | ] 23 | print(find_shortest_path(distances), flush=True) 24 | 25 | 26 | distances = [ 27 | [1,0,3,4,5,6,7,8,9,10,11], 28 | [1,2,3,4,5,6,7,0,9,10,11], 29 | [1,2,3,0,5,6,7,8,9,10,11], 30 | [1,2,0,4,5,6,7,8,9,10,11], 31 | [1,2,3,4,5,6,7,8,0,10,11], 32 | [1,2,3,4,5,0,7,8,9,10,11], 33 | [1,2,3,4,5,6,7,8,9,0,11], 34 | [1,2,3,4,0,6,7,8,9,10,11], 35 | [0,2,3,4,5,6,7,8,9,10,11], 36 | [1,2,3,4,5,6,7,8,0,10,11], 37 | [1,2,3,4,5,6,7,8,2,8,11], 38 | ] 39 | start = time.process_time() 40 | print(find_shortest_path(distances), flush=True) 41 | end = time.process_time() 42 | duration_in_seconds = end-start 43 | print("Shortest path calculated in {} seconds".format(duration_in_seconds), flush=True) 44 | -------------------------------------------------------------------------------- /13-python-integration/4-cppyy-types/callers.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | 4 | 5 | import cppyy 6 | cppyy.include("functions.hpp") 7 | 8 | print("Call a C++ function that expects a std::vector:") 9 | print(cppyy.gbl.sum([1,2,3,4,5,6,7,8,9,10])) 10 | print(cppyy.gbl.sum(range(1,11))) 11 | print(cppyy.gbl.sum([[1,2,3],[4,5,6],[7,8,9,10]])) 12 | print(cppyy.gbl.sum.__doc__) 13 | 14 | 15 | print("\nCall a C++ function that expects a std::set:") 16 | # This returns a compilation error: 17 | # print(cppyy.gbl.sumset(set(range(1,11)))) 18 | 19 | # We have to explicitly create a C++ set (not a Python set). 20 | # See https://stackoverflow.com/a/56350681/827927 21 | cppset = cppyy.gbl.std.set[int]() 22 | for i in range(1,11): 23 | cppset.insert(i) 24 | print(cppyy.gbl.sumset(cppset)) 25 | print(cppyy.gbl.sumset.__doc__) 26 | 27 | #print("\nCall a C++ function that expects a std::map:") 28 | #print(cppyy.gbl.sumset(list(range(1,11)))) 29 | #print(cppyy.gbl.summap({55:1,66:2,77:3})) 30 | #print(cppyy.gbl.summap(list({55:1,66:2,77:3}))) 31 | 32 | -------------------------------------------------------------------------------- /13-python-integration/__pycache__/factorial.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/__pycache__/factorial.cpython-35.pyc -------------------------------------------------------------------------------- /13-python-integration/etc/4-mandelbrot/Makefile: -------------------------------------------------------------------------------- 1 | # A generic makefile for running single-file C++ projects. 2 | # 3 | # AUTHOR: Erel Segal-Halevi 4 | 5 | CXX=clang++-9 6 | CPPFLAGS=-std=c++2a -O3 -ffp-contract=off -fno-expensive-optimizations 7 | RM=rm -f 8 | 9 | ifndef MAIN 10 | MAIN=./main.cpp 11 | endif 12 | 13 | MAINEXECUTABLE=$(subst .cpp,,$(MAIN)).exe 14 | 15 | SOURCES=$(MAIN) 16 | 17 | all: $(MAINEXECUTABLE) 18 | $(MAINEXECUTABLE) 19 | 20 | $(MAINEXECUTABLE): $(SOURCES) $(HEADERS) 21 | $(CXX) $(CPPFLAGS) $(SOURCES) -o $(MAINEXECUTABLE) 22 | 23 | clean: 24 | $(RM) *.exe a.out *.class 25 | -------------------------------------------------------------------------------- /13-python-integration/etc/5-shapley-value/shapley_value.cppyy.py: -------------------------------------------------------------------------------- 1 | #!python3 2 | 3 | 4 | import cppyy 5 | cppyy.include("shapley_value.cpp") 6 | 7 | def map_subset_to_cost_f(subset): 8 | return 1000*max(subset) 9 | 10 | 11 | if __name__=="__main__": 12 | map_subset_to_cost = { 13 | frozenset(): 0, 14 | frozenset({1}): 1000, 15 | frozenset({2}): 2000, 16 | frozenset({1,2}): 2000, 17 | frozenset({3}): 3000, 18 | frozenset({1,3}): 3000, 19 | frozenset({2,3}): 3000, 20 | frozenset({1,2,3}): 3000, 21 | } 22 | sv = cppyy.gbl.shapley_values_f(3, map_subset_to_cost_f) 23 | print("Shapley values: ", sv, flush=True) 24 | 25 | -------------------------------------------------------------------------------- /13-python-integration/text.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/text.odt -------------------------------------------------------------------------------- /13-python-integration/text.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/13-python-integration/text.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Erel Segal-Halevi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | # תכנות מערכות ב 4 | ## אוניברסיטת אריאל, המחלקה למדעי המחשב, סמסטר א ה'תשפ"א 5 | 6 | מטרת הקורס היא לאמן אתכם בתיכנות בשפה קשה ומסובכת במיוחד - שפת 7 | ++C. 8 | שפה הכוללת פרדיגמות שונות - תיכנות פרוצדורלי, מונחה-עצמים ופונקציונלי. 9 | שפה הדורשת ניהול ידני של משאבי הזיכרון. 10 | בנוסף, הקורס מאמן אתכם בכישורים כלליים החיוניים לכל מתכנת, כגון: עבודה בסביבות לינוקס 11 | (Linux), כולל סקריפטים (bash), גיט (git), הצגת קוד בפני הכיתה, עמידה בלוחות זמנים, והתמודדות עם תקלות ושינויים לא צפויים. 12 | 13 | לפרטים ראו: 14 | * [סילבוס](syllabus.pdf) 15 | * [איך להתכונן לקורס](preparations.md) 16 | * [המטלות וכללי חישוב הציון בקורס](grade-rules.md) 17 | * [נוהל הגשת מטלות באיחור](late-submissions.md) 18 | * [בחינות משנים קודמות](https://github.com/erelsgl-at-ariel/cpp-course) 19 | 20 | הגשת המטלות היא אישית. מותר להתייעץ ולקבל עזרה, אבל יש לדווח בכתב על כל עזרה שקיבלתם, בהתאם ל[תקנון היושר של המחלקה](https://www.ariel.ac.il/wp/cs/wp-content/uploads/sites/88/2020/08/Guidelines-for-Academic-Integrity.pdf). 21 | 22 | ## תודות 23 | 24 | * חלק מהמצגות מבוססות על מצגות של אופיר פלא, מירי בן-ניסן, וערן קאופמן. 25 |
-------------------------------------------------------------------------------- /late-submissions.md: -------------------------------------------------------------------------------- 1 |
2 | 3 | # הגשת מטלות באיחור 4 | 5 | אחת ממטרות הקורס היא לאמן אתכם לעמוד בזמנים. 6 | זמני הגשת המטלות ידועים מראש, ויש לתכנן מראש את זמנכם כך שתספיקו לעמוד בהם - לא לחכות לרגע האחרון. 7 | 8 | עם זאת, לפעמים יש סיבות מוצדקות להגשה באיחור. 9 | כדי לטפל בנושא בצורה יעילה, ולחסוך זמן לכם ולנו, 10 | אנחנו מאפשרים לכם מראש להגיש באיחור 11 | **שתי פעמים**, 12 | מסיבה מוצדקת בלבד, 13 | בלי לבקש אישור מאיתנו. 14 | 15 | אם בחרתם להגיש באיחור, עליכם לבדוק לעצמכם את המטלה 16 | בעזרת `bash grade` 17 | ולחשב את הציון שלכם. 18 | לאחר מכן, עליכם להגיש את המטלה שלכם בתיבת-הגשה מיוחדת שנפתחה במודל: 19 | "הגשה באיחור מסיבה מוצדקת - פעם ראשונה" 20 | או 21 | "הגשה באיחור מסיבה מוצדקת - פעם שניה". 22 | 23 | בקשות להגשה באיחור שלוש פעמים או יותר יתקבלו רק במקרים חריגים ובהתאם לנהלי האוניברסיטה, 24 | כגון: מילואים או מחלה במשך שלושה שבועות רצופים (עם אישור בכתב). 25 | במקרים חריגים מסוג זה, יש לשלוח את כל האישורים הדרושים לבודק המטלות ולבקש שיבדוק לכם את המטלה באיחור. 26 | 27 | להתראות 28 |
-------------------------------------------------------------------------------- /old/homework-instructions.odt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/old/homework-instructions.odt -------------------------------------------------------------------------------- /old/homework-instructions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/old/homework-instructions.pdf -------------------------------------------------------------------------------- /syllabus.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/syllabus.docx -------------------------------------------------------------------------------- /syllabus.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpp-at-ariel/cpp-5781/498d16c806a283e68b9e7dc48803f2d054106257/syllabus.pdf --------------------------------------------------------------------------------