{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Static variable or Global variable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <cstdio>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "static int a_global_variable = 100;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150"
     ]
    }
   ],
   "source": [
    "void add_50 () {\n",
    "    a_global_variable += 50;\n",
    "}\n",
    "\n",
    "add_50();\n",
    "printf(\"%d\", a_global_variable);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Static members or Static classes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "By using this tech, you can use the class everywhere without instantiation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "Interpreter Error",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Interpreter Error: "
     ]
    }
   ],
   "source": [
    "/*\n",
    "#include <cstdio>\n",
    "\n",
    "struct Go {\n",
    "    static int power;\n",
    "    static void power_up(int value) {\n",
    "        power += value;\n",
    "        printf(\"%d\\n\", power);\n",
    "    }\n",
    "};\n",
    "\n",
    "int Go::power = 100;\n",
    "\n",
    "int main(){\n",
    "    Go::power_up(20);\n",
    "    Go::power_up(80);\n",
    "}\n",
    "*/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Thread safe code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Mutable global variables are the source of many thread safety issues.\n",
    "\n",
    "To solve this problem, we'd better let each thread use its own variables."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "/*\n",
    "static thread_local int power;\n",
    "*/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dynamic Storage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "int* my_int_pointer = new int;\n",
    "int* my_int_pointer2 = new int{42};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "*my_int_pointer2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "delete my_int_pointer;\n",
    "delete my_int_pointer2;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Dynamic Arrays"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "int* my_int_array_pointer = new int[100];\n",
    "delete[] my_int_array_pointer;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## construction and deconstruction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "add '~' before the class name to have a destructor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "struct Tracer {\n",
    "    Tracer(const char* name): name {name} {\n",
    "        printf(\"%s constructed.\\n\", this->name);\n",
    "    }\n",
    "    ~Tracer() {\n",
    "        printf(\"%s destructed.\\n\", this->name);\n",
    "    }\n",
    "    private:\n",
    "        const char* const name;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this object constructed.\n"
     ]
    }
   ],
   "source": [
    "const auto* tracer = new Tracer{ \"this object\" };"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this object destructed.\n"
     ]
    }
   ],
   "source": [
    "delete tracer;"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  try and catch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <stdexcept>\n",
    "#include <cstdio>\n",
    "\n",
    "struct TryMan {\n",
    "    void right(int x) {\n",
    "        if (x < 0) {\n",
    "            throw std::runtime_error {\"You shouldn't be so negative!\"};\n",
    "        }\n",
    "        printf(\"Your number is %d.\\n\", x);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your number is 8.\n",
      "This is the exception message: You shouldn't be so negative!\n"
     ]
    }
   ],
   "source": [
    "TryMan tryman;\n",
    "try {\n",
    "    tryman.right(8);\n",
    "    tryman.right(-10);\n",
    "} catch (const std::runtime_error& e) {\n",
    "    printf(\"This is the exception message: %s\\n\", e.what());\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Sometimes, you want to catch any errors. You can use the following code:\n",
    "\n",
    "```cpp\n",
    "try {\n",
    "    // normal codes\n",
    "} catch (...) {\n",
    "    // exception handler\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> Normally, you'll see something like this:\n",
    "\n",
    "```cpp\n",
    "try {\n",
    "} catch (const std::logic_error& ex) {\n",
    "    // log exception and terminate the program; there is a programming error!\n",
    "} catch (const std::runtime_error& ex) {\n",
    "    // do our best to recover gracefully\n",
    "} catch (const std::exception& ex) {\n",
    "    // This will handle any exception that derives from std:exception\n",
    "} catch (...) {\n",
    "    // Panic; an undoreseen exception type was thrown\n",
    "}\n",
    "```\n",
    "> Don't throw any error at destructor, by the way."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Stacks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Last in, first out."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
