diff --git a/1-ProceduralProgramming/3-Types.ipynb b/1-ProceduralProgramming/3-Types.ipynb index 8592b58b8f20816b61f286f14dfa79f7e479b6cc..958f435f61e4960d73f22ec7e1e1aef2c7478c3b 100644 --- a/1-ProceduralProgramming/3-Types.ipynb +++ b/1-ProceduralProgramming/3-Types.ipynb @@ -41,7 +41,7 @@ "bool undefined; // UNDEFINED !! \n", "if (undefined)\n", " std::cout << \"This text might appear or not - it's truly undefined and may vary from \"\n", - " \"one run/compiler/architecture/etc... to another!\" << std::endl;`" + " \"one run/compiler/architecture/etc... to another!\" << std::endl;" ] }, { @@ -438,7 +438,6 @@ "source": [ "{\n", " float f = 1.12345678901234567890;\n", - " double d = 2.12345678901234567890;\n", " double d_f { f }; // OK\n", "}" ] diff --git a/2-ObjectProgramming/1-Introduction.ipynb b/2-ObjectProgramming/1-Introduction.ipynb index 735c557830272cbc5e856fe97e872df2d6419c4f..0a972ce94e6d71ade83a60b33435c2bc35f7c9ff 100644 --- a/2-ObjectProgramming/1-Introduction.ipynb +++ b/2-ObjectProgramming/1-Introduction.ipynb @@ -43,7 +43,7 @@ "double norm(double v_x, double v_y, double v_z) \n", "{ \n", " return std::sqrt( v_x * v_x + v_y * v_y + v_z * v_z ); \n", - "};\n", + "}\n", "\n", "{\n", " double v1_x, v1_y, v1_z;\n", @@ -158,6 +158,41 @@ "Let's also highlight the `.` syntax which allows to access the attributes of an object (e.g `v1.x`).\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The semicolon at the end of a `struct`\n", + "This comes historically from the C, where a `struct` could be defined and initialized at the same time (or should - Xeus-cling doesn't manage it... As usual you may check a full-fledged compiler accepts it [@Coliru](http://coliru.stacked-crooked.com/a/3b77606ea8082485)):" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "// Xeus-cling issue (at least circa May 2021)\n", + "\n", + "struct Vector\n", + "{\n", + " double x;\n", + " double y;\n", + " double z; \n", + "} v1; // Here the struct is declared and at the same time an object v1 is created\n", + "\n", + "v1.x = 1.;\n", + "v1.y = 5.;\n", + "v1.z = -2.;" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is absolutely **not** encouraged in C++, but it may help you to remember always closing a `struct` (or later a `class`) with a semicolon." + ] + }, { "cell_type": "markdown", "metadata": {}, diff --git a/2-ObjectProgramming/2-Member-functions.ipynb b/2-ObjectProgramming/2-Member-functions.ipynb index 4aabce658df709ea4a468a78941d379b357c954b..f561176087dc1a18888191fb81c1a6b5a80e42b4 100644 --- a/2-ObjectProgramming/2-Member-functions.ipynb +++ b/2-ObjectProgramming/2-Member-functions.ipynb @@ -97,7 +97,7 @@ "\n", "In most cases, it might be altogether removed; we have to put it explicitly here solely because we named the `init` parameters with the same name as the data attribute. If not, we could have avoided to mention it completely.\n", "\n", - "An usual convention is to suffix data attributes with a `_`; doing so remove the need to the explicit `this`:" + "An usual convention is to suffix data attributes with a `_` (**be careful**, attributes prefixed with a `_` is reserved by the C++ standard); doing so remove the need to the explicit `this`:" ] }, { diff --git a/2-ObjectProgramming/3-constructors-destructor.ipynb b/2-ObjectProgramming/3-constructors-destructor.ipynb index e74207f077160221f66b73ae7f8ce4fee4dd9f4f..be00709721509248144988be4581d6aef1ac9fdc 100644 --- a/2-ObjectProgramming/3-constructors-destructor.ipynb +++ b/2-ObjectProgramming/3-constructors-destructor.ipynb @@ -478,7 +478,7 @@ "source": [ "struct BadlyInitialized\n", "{\n", - " int a;\n", + " int a_;\n", "};" ] }, @@ -492,7 +492,7 @@ "\n", "{\n", " BadlyInitialized my_object;\n", - " std::cout << \"Undefined behaviour: no guarantee for the value of the data attribute!: \" << my_object.a << std::endl;\n", + " std::cout << \"Undefined behaviour: no guarantee for the value of the data attribute!: \" << my_object.a_ << std::endl;\n", "}" ] }, diff --git a/2-ObjectProgramming/4-encapsulation.ipynb b/2-ObjectProgramming/4-encapsulation.ipynb index f4f6ed143587c612cde3a51f454346cc56ea094c..93c4f06d34447f35479203857236c0c7c4f010ee 100644 --- a/2-ObjectProgramming/4-encapsulation.ipynb +++ b/2-ObjectProgramming/4-encapsulation.ipynb @@ -93,7 +93,7 @@ " \n", " private:\n", " \n", - " int a_ = -9999999; // stupid default value.\n", + " int a_ { -9999999 }; // stupid default value.\n", " \n", " void SetValue(int a);\n", " \n", @@ -458,8 +458,8 @@ "\n", " private:\n", " \n", - " double length_ = -1.e20; // a stupid value which at least is deterministically known...\n", - " double width_ = -1.e20;\n", + " double length_ { -1.e20 }; // a stupid value which at least is deterministically known...\n", + " double width_ { -1.e20 };\n", "};" ] }, @@ -591,9 +591,9 @@ " \n", " private:\n", " \n", - " double area_ = -1.e20; // a stupid value which at least is deterministic...\n", - " double length_ = -1.20; \n", - " double width_ = -1.20;\n", + " double area_ { -1.e20 }; // a stupid value which at least is deterministic...\n", + " double length_ { -1.20 }; \n", + " double width_ { -1.20 };\n", "};" ] }, diff --git a/2-ObjectProgramming/5-static.ipynb b/2-ObjectProgramming/5-static.ipynb index 91722c30b2fc1218ef55ada596acc248ce97e80f..2ca0f1abe9d2d2017c8ec90d6bc989329e6ef6c1 100644 --- a/2-ObjectProgramming/5-static.ipynb +++ b/2-ObjectProgramming/5-static.ipynb @@ -75,9 +75,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "It might be used for instance if you need to initialize something on the very first call of a function:\n", - "\n", - "````\n", + "It might be used for instance if you need to initialize something on the very first call of a function:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "void FunctionWithStatic()\n", "{\n", " static bool is_first_call = true;\n", @@ -85,14 +91,12 @@ " if (is_first_call)\n", " {\n", " // Init stuff here on first call only\n", - " ... \n", + " // ... \n", " is_first_call = false;\n", " }\n", " \n", - " ... // code executed at each call\n", - "}\n", - "\n", - "````" + " // ... code executed at each call\n", + "}" ] }, { @@ -239,8 +243,10 @@ ] }, { - "cell_type": "raw", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "#include <iostream>\n", "#include <vector>\n", @@ -400,7 +406,7 @@ "To understand better the possible issue and the fix proposed, you may have a look at:\n", "\n", "* Item 26 of \\cite{Meyers1995}\n", - "* The dedicated item on [Parashift FAQ](https://isocpp.org/wiki/faq/ctors#static-init-order)\n" + "* The dedicated item on [isocpp FAQ](https://isocpp.org/wiki/faq/ctors#static-init-order)\n" ] }, { diff --git a/2-ObjectProgramming/6-inheritance.ipynb b/2-ObjectProgramming/6-inheritance.ipynb index 1e9f6dcde4c504642c02fd9e75143d12c8474752..5c962d9f7013ed90b0e517b80736bf9c108e0003 100644 --- a/2-ObjectProgramming/6-inheritance.ipynb +++ b/2-ObjectProgramming/6-inheritance.ipynb @@ -132,7 +132,7 @@ "\n", "### Multiple layer of inheritance\n", "\n", - "A child class may also be the parent of another class (unless `final` is used - see later)\n", + "A child class may also be the parent of another class (unless [`final`](6-inheritance.ipynb#final-keyword) is used - see later)\n", "\n" ] }, @@ -347,9 +347,9 @@ " \n", " private:\n", " \n", - " double width_ = -1.e20; // Stupid value that would play havoc if not properly initialized\n", + " double width_ { -1.e20 }; // Stupid value that would play havoc if not properly initialized\n", " // - std::optional (C++17) would probably a better choice.\n", - " double length_ = -1.e20; \n", + " double length_ { -1.e20 }; \n", "};" ] }, @@ -1847,7 +1847,7 @@ "source": [ "So you could devise a way to identify which is the dynamic type of your `PolymorphicVehicle` pointer and cast it dynamically to the rightful type so that extended API offered by the derived class is accessible.\n", "\n", - "If you find this clunky, you are not alone: by experience if you really need to resort to **dynamic_cast** it's probably your data architecture needs some revision. But maybe the mileage vary for other developers, and it's useful to know the possibility exist." + "If you find this clunky, you are not alone: by experience if you really need to resort to **dynamic_cast** it's probably your data architecture needs some revision. But maybe the mileage vary for other developers, and it's useful to know the possibility exists." ] }, { @@ -1856,7 +1856,7 @@ "source": [ "## `final` keyword\n", "\n", - "If you need to specify a class can't be derived, you may stick a `final` keyword in its declaration (from C++11 onward):\n" + "If you need to specify a class that can't be derived, you may stick a `final` keyword in its declaration (from C++11 onward):\n" ] }, { @@ -2100,7 +2100,7 @@ "source": [ "struct DerivedClass4 : public BaseClass4\n", "{\n", - " DerivedClass4() = default;; \n", + " DerivedClass4() = default; \n", " \n", " virtual std::string ClassName() const;\n", "};" @@ -2192,7 +2192,7 @@ "source": [ "struct DerivedClass5 : public BaseClass5\n", "{\n", - " DerivedClass5() = default;; \n", + " DerivedClass5() = default; \n", " \n", " virtual std::string ClassName() const;\n", "};" diff --git a/bibliography.ipynb b/bibliography.ipynb index 2dae2a024f5807d70d0df4483a1a023b2c6c4962..5fda2241e27fedc184d60460e26eb3895eea877b 100644 --- a/bibliography.ipynb +++ b/bibliography.ipynb @@ -77,7 +77,7 @@ "\n", "Provides a dictionary entry with the API explained for every functions/algorithms/class/you name it from the language and the standard library. Usually you will find them upfront if you Google a term from C++ (e.g. `std::vector`). Explanation might be a bit terse, but there is now in most cases an example powered by Coliru which present a concrete (albeit simple - but you're accustomed to that if you followed this tutorial!) use case.\n", "\n", - "### [Parashift](https://isocpp.org/faq)\n", + "### [isocpp](https://isocpp.org/faq)\n", "\n", "A gigantic FAQ that covers many aspects of C++; this site is in fact a merge from two previously independant FAQ, one of which was maintained by Bjarne Stroustrup, creator of the language.\n", "\n",