{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e0e9c863-8720-40a4-a22c-00af773497fe",
   "metadata": {},
   "source": [
    "## Classes and Objects\n",
    "\n",
    "Let's try to implement our first class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bc685489-cf28-4a15-b0ee-fef2e6143f28",
   "metadata": {},
   "outputs": [],
   "source": [
    "class aggregate {\n",
    "    short s = 4;\n",
    "    unsigned int i = 1u<<16;\n",
    "    double x = 2.0;\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4ada47eb-7158-4caf-aa60-0eb8df7e4769",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[1minput_line_9:3:32: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1m's' is a private member of '__cling_N52::aggregate'\u001b[0m\n",
      "    std::cout << \"s = \" << agg.s << std::endl;\n",
      "\u001b[0;1;32m                               ^\n",
      "\u001b[0m\u001b[1minput_line_7:2:11: \u001b[0m\u001b[0;1;30mnote: \u001b[0mimplicitly declared private here\u001b[0m\n",
      "    short s = 4;\n",
      "\u001b[0;1;32m          ^\n",
      "\u001b[0m\u001b[1minput_line_9:4:32: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1m'i' is a private member of '__cling_N52::aggregate'\u001b[0m\n",
      "    std::cout << \"i = \" << agg.i << std::endl;\n",
      "\u001b[0;1;32m                               ^\n",
      "\u001b[0m\u001b[1minput_line_7:3:18: \u001b[0m\u001b[0;1;30mnote: \u001b[0mimplicitly declared private here\u001b[0m\n",
      "    unsigned int i = 1u<<16;\n",
      "\u001b[0;1;32m                 ^\n",
      "\u001b[0m"
     ]
    },
    {
     "ename": "Interpreter Error",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Interpreter Error: "
     ]
    }
   ],
   "source": [
    "#include <iostream>\n",
    "\n",
    "int main() {\n",
    "    aggregate agg;\n",
    "    std::cout << \"s = \" << agg.s << std::endl;\n",
    "    std::cout << \"i = \" << agg.i << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4035a6b6-f751-44fa-a579-a1166df4eed7",
   "metadata": {},
   "source": [
    "That did not work. Why?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "159ef323-6f0a-49af-ac06-98804334e5d1",
   "metadata": {},
   "source": [
    "Because object-oriented programming is also about **encapsulation**. By making certain attributes (data members) and methods (member functions) invisible to the outside, we\n",
    "  - can enhance safety: better control over the state of an object\n",
    "  - make sure that the internal implementation of the class can be changed without breaking outside code  \n",
    "\n",
    "In C++ we have three levels of access:\n",
    "  - **public** = any one has access \n",
    "  - **private** = only the class itself has access\n",
    "  - **protected** = the class and its children have access\n",
    "  \n",
    "The difference between a struct and a class lies in their default setting\n",
    "\n",
    "|            | class | struct |\n",
    "|:----------:|:-----:|:------:|\n",
    "| default is:|private| public |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7ea6dfba-43fe-4730-b845-4bd49c9b3e3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class aggregate {\n",
    "  \n",
    "  double x;         // private by default\n",
    "    \n",
    "public:\n",
    "    \n",
    "  short s = 4;\n",
    "  unsigned int i = 1u<<16;\n",
    "\n",
    "};\n",
    "\n",
    "struct combo {\n",
    "  \n",
    "  int a;            // public by default\n",
    "    \n",
    "private:\n",
    "  int internal;     // not accessible from outside\n",
    "\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6b4cd5c9-0ec3-451b-95b3-e9ad857eea83",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s = 4\n",
      "i = 65536\n"
     ]
    }
   ],
   "source": [
    "#include <iostream>\n",
    "\n",
    "int main() {\n",
    "  aggregate agg;\n",
    "  std::cout << \"s = \" << agg.s << std::endl;\n",
    "  std::cout << \"i = \" << agg.i << std::endl;\n",
    "\n",
    "  combo s;\n",
    "  s.a = 7;\n",
    "}\n",
    "\n",
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c9241eb-1530-42db-8f50-542b8b74bbb8",
   "metadata": {},
   "source": [
    "An **object** is an instance of a class; different objects are different entities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2f93bb55-528a-4bac-8336-ccf98cc18af0",
   "metadata": {},
   "outputs": [],
   "source": [
    "int main() {\n",
    "    \n",
    "    aggregate a, b, c;\n",
    "    \n",
    "    a.s = 1;\n",
    "    b.s = 2;\n",
    "    c.s = 3;\n",
    "    \n",
    "    std::cout << \"a's short has a value of \" << a.s << '\\n'\n",
    "              << \"b's short has a value of \" << b.s << '\\n'\n",
    "              << \"c's short has a value of \" << c.s << std::endl;\n",
    "    \n",
    "    a = c;\n",
    "    std::cout << \"now a.s = \" << a.s << std::endl;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e5ca8f44-4f3f-4142-8b5e-f5a3c31fee7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a's short has a value of 1\n",
      "b's short has a value of 2\n",
      "c's short has a value of 3\n",
      "now a.s = 3\n"
     ]
    }
   ],
   "source": [
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe29c0f5-6967-403d-b796-779fe1ccf4be",
   "metadata": {},
   "source": [
    "### Constructors\n",
    "\n",
    "- Our class *aggregate* used *default member initializers* for setting initial values for its data members.\n",
    "- This is one possibility. The other is to use a *constructor* and a *member initializer list*.\n",
    "- Constructors are also required, if we want to run some start-up code as part of object creation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4c961b3c-ba83-46e6-aab1-08f1ad4102e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "\n",
    "class myClass{\n",
    "    \n",
    "    public:\n",
    "        myClass() : memb_(0) {\n",
    "            std::cout << \"An instance of myClass was generated.\"\n",
    "                      << \" memb_ = \" << memb_ << std::endl;\n",
    "        }\n",
    "    \n",
    "    private:\n",
    "        int memb_; // some people adhere to the convention of marking data members by a trailing \"_\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f501dce2-ef23-4345-8072-45ea85cb8c17",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of myClass was generated. memb_ = 0\n"
     ]
    }
   ],
   "source": [
    "int main() {\n",
    "    myClass myObj;\n",
    "}\n",
    "\n",
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a5ded92-91fc-4a2f-8628-d9531cd84ea2",
   "metadata": {},
   "source": [
    "#### Multiple Constructors\n",
    "C++ allows to overload functions. One can use this to implement different constructors."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3f4cf1f6-8c50-4057-ab71-259c1046222a",
   "metadata": {},
   "outputs": [],
   "source": [
    "class myClass{\n",
    "    \n",
    "    public:\n",
    "        myClass() : memb_(0) {\n",
    "            std::cout << \"An instance of myClass was generated.\"\n",
    "                      << \" memb_ = \" << memb_ << std::endl;\n",
    "        }\n",
    "    \n",
    "        myClass( int member ) : memb_(member) {\n",
    "            std::cout << \"An instance of myClass was generated.\"\n",
    "                      << \" memb_ = \" << memb_ << std::endl;\n",
    "        }\n",
    "    \n",
    "    private:\n",
    "        int memb_;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "51015ed7-9f4e-40c0-bfa9-2b9f98cd0e1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of myClass was generated. memb_ = 0\n",
      "An instance of myClass was generated. memb_ = 5\n"
     ]
    }
   ],
   "source": [
    "int main() {\n",
    "    myClass myObj1;\n",
    "    myClass myObj2( 5 );\n",
    "}\n",
    "\n",
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee95a999-1b5a-4c1b-b2a3-d7dc9a6b306f",
   "metadata": {},
   "source": [
    "#### Constructor Delegation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b977d057-634f-4027-a177-6866bf88160b",
   "metadata": {},
   "source": [
    "In the example above the body of both constructors is identical. Such a code duplication is, of course, not nice and in violation of the **D**on't **R**epeat **Y**ourself principle. We can avoid this by using **constructor delegation**."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8eed6ec9-9ad6-48ab-b0e2-ab0ea35b9b57",
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "\n",
    "class myClass{\n",
    "    \n",
    "    public:\n",
    "        myClass( int member ) : memb_(member) {\n",
    "            std::cout << \"An instance of myClass was generated.\"\n",
    "                      << \" memb_ = \" << memb_ << std::endl;\n",
    "        }\n",
    "\n",
    "        myClass() : myClass(0) {}\n",
    "        \n",
    "    private:\n",
    "        int memb_;\n",
    "};"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa5b47ea-8d12-4f55-8efe-c08588b81dbc",
   "metadata": {},
   "source": [
    "#### Cleaning up: Destructors\n",
    "When an object goes out of scope and is destroyed we might want or need to do some housekeeping. For this we can implement a destructor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e456ce69-71e5-4169-b3e8-f302c3f72242",
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "\n",
    "class VerboseClass{\n",
    "    \n",
    "    public:\n",
    "        // c'tor\n",
    "        VerboseClass() {\n",
    "            std::cout << \"An instance of VerboseClass was generated.\" << std::endl;\n",
    "        }\n",
    "\n",
    "        // d'tor\n",
    "        ~VerboseClass() {\n",
    "            std::cout << \"An instance of VerboseClass was destroyed.\\n\" << std::endl;\n",
    "        }\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8fe56e9c-1842-4c29-84eb-1b20b3f7704a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of VerboseClass was generated.\n",
      "An instance of VerboseClass was destroyed.\n",
      "\n",
      "An instance of VerboseClass was generated.\n",
      "An instance of VerboseClass was destroyed.\n",
      "\n",
      "An instance of VerboseClass was generated.\n",
      "An instance of VerboseClass was destroyed.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "int main() {\n",
    "    for( int k = 0; k < 3; k++ ) {\n",
    "        VerboseClass talky;\n",
    "    }\n",
    "}\n",
    "\n",
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dffe4043-6959-4169-b0df-203a50d36e0f",
   "metadata": {},
   "source": [
    "#### Small Project: A Vector Class for Linear Algebra\n",
    "Tasks:\n",
    "- We want to implement a class that represents a vector entity from linear algebra.\n",
    "- The size of the vector should be selectable at object creation -> need to allocate memory dynamically.\n",
    "- Access to vector entries should be 1-based -> operator overloading.\n",
    "- Implement simple methods, such as\n",
    "  - scaling vector with a constant\n",
    "  - adding two vectors together\n",
    "  - compute the Euclidean inner product of two vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f33e5eef-b396-4bfc-8031-f21b33839611",
   "metadata": {},
   "outputs": [],
   "source": [
    "// --------------------------------------\n",
    "//  Start with the skeleton of the class\n",
    "// --------------------------------------\n",
    "\n",
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass{\n",
    "    \n",
    "    public:\n",
    "    \n",
    "        // User should specify dimension at object creation\n",
    "        VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "            // don't allow vectors with zero dimension\n",
    "            assert( !dim_ == 0 );\n",
    "\n",
    "            // allocate memory (will throw an exception, if it fails)\n",
    "            vec_ = new double[ dim_ ];\n",
    "            \n",
    "            // be talkative ;-)\n",
    "            std::cout << \"An instance of VectorClass was generated. dim_ = \"\n",
    "                      << dim_ << std::endl;\n",
    "        }\n",
    "\n",
    "        // Don't allow the default constructor\n",
    "        // [prior to C++11 you would have solved this by making it private]\n",
    "        VectorClass() = delete;\n",
    "\n",
    "    private:\n",
    "        unsigned int dim_;   // dimension of vector\n",
    "        double* vec_;        // entries of vector\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e2f2ac16-a0df-4f6c-816a-cb47420f4989",
   "metadata": {},
   "outputs": [],
   "source": [
    "int main() {\n",
    "    VectorClass myVec( 420 );\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7c06f924-8412-449c-a5b5-a56b1df2ff2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of VectorClass was generated. dim_ = 420\n"
     ]
    }
   ],
   "source": [
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "935b6f8d-d347-4edb-9807-f2ed3563dcaf",
   "metadata": {},
   "source": [
    "Our implementation is **missing an essential piece**? What could that be?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "102897af-957e-43b7-a959-eb6ee468f35d",
   "metadata": {},
   "outputs": [],
   "source": [
    "// --------------------------------------\n",
    "//  Start with the skeleton of the class\n",
    "// --------------------------------------\n",
    "\n",
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass{\n",
    "    \n",
    "    public:\n",
    "    \n",
    "        // User should specify dimension at object creation\n",
    "        VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "            // don't allow vectors with zero dimension\n",
    "            assert( dim_ > 0 );\n",
    "\n",
    "            // allocate memory (will throw an exception, if it fails)\n",
    "            vec_ = new double[ dim_ ];\n",
    "            \n",
    "            // be talkative ;-)\n",
    "            std::cout << \"An instance of VectorClass was generated. dim_ = \"\n",
    "                      << dim_ << std::endl;\n",
    "        }\n",
    "\n",
    "        // Don't allow the default constructor\n",
    "        // [prior to C++11 you would have solved this by making it private]\n",
    "        VectorClass() = delete;\n",
    "    \n",
    "        // We need to implement a destructor to free the dynamic memory again,\n",
    "        // otherwise we easily produce memory leaks\n",
    "        ~VectorClass() {\n",
    "            delete[] vec_;\n",
    "            std::cout << \"An instance of VectorClass was destroyed and \"\n",
    "                      << dim_ * sizeof(double)\n",
    "                      << \" bytes freed.\" << std::endl;\n",
    "        }\n",
    "\n",
    "    private:\n",
    "        unsigned int dim_;   // dimension of vector\n",
    "        double* vec_;        // entries of vector\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "adcac578-0bca-4aed-8d13-4514826dd0e7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of VectorClass was generated. dim_ = 10\n",
      "An instance of VectorClass was destroyed and 80 bytes freed.\n"
     ]
    }
   ],
   "source": [
    "int main() {\n",
    "    VectorClass myVec( 10 );\n",
    "}\n",
    "\n",
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a74105b-5453-49ab-aeed-e0371809fc52",
   "metadata": {},
   "source": [
    "Our VectorClass is currently pretty useless, since we cannot manipulate the values of the vector entries. Making vec_ public would break encapsulation. Instead let's\n",
    "- overload the [ ] for providing one-based access\n",
    "- add a getter method for the dimension\n",
    "- add a member function to pretty-print the vector's entries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a32eb08a-822a-4b83-b3d0-6de281acffba",
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass{\n",
    "    \n",
    "    public:\n",
    "    \n",
    "        // User should specify dimension at object creation\n",
    "        VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "            // don't allow vectors with zero dimension\n",
    "            assert( dim_ > 0 );\n",
    "\n",
    "            // allocate memory (will throw an exception, if it fails)\n",
    "            vec_ = new double[ dim_ ];\n",
    "            \n",
    "            // be talkative ;-)\n",
    "            std::cout << \"An instance of VectorClass was generated. dim_ = \"\n",
    "                      << dim_ << std::endl;\n",
    "        }\n",
    "\n",
    "        // Don't allow the default constructor\n",
    "        // [prior to C++11 you would have solved this by making it private]\n",
    "        VectorClass() = delete;\n",
    "    \n",
    "        // We need to implement a destructor to free the dynamic memory again,\n",
    "        // otherwise we easily produce memory leaks\n",
    "        ~VectorClass() {\n",
    "            delete[] vec_;\n",
    "            std::cout << \"An instance of VectorClass was destroyed and \"\n",
    "                      << dim_ * sizeof(double)\n",
    "                      << \" bytes freed.\" << std::endl;\n",
    "        }\n",
    "\n",
    "        // provide access to the vector's dimension\n",
    "        unsigned int getDim() { return dim_; }\n",
    "    \n",
    "        // overload the [] for accessing individual entries\n",
    "        double& operator[] ( unsigned int index ) {\n",
    "            assert( index != 0 && index <= dim_ );\n",
    "            return vec_[index-1];\n",
    "        }\n",
    "\n",
    "        // pretty print vector to given output stream\n",
    "        void print( std::ostream& os ) {\n",
    "            os << \"(\";\n",
    "            for( unsigned int k = 0; k < dim_-1; k++ ) {\n",
    "                os << vec_[k] << \", \";\n",
    "            }\n",
    "            os << vec_[dim_-1] << \")\" << std::endl;\n",
    "        }\n",
    "    \n",
    "    private:\n",
    "        unsigned int dim_;   // dimension of vector\n",
    "        double* vec_;        // entries of vector\n",
    "\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "4eb50e73-21f5-4669-9464-32f11d958732",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of VectorClass was generated. dim_ = 3\n",
      "Our vector has dimension 3 and entries: (3, 2, 1)\n",
      "An instance of VectorClass was destroyed and 24 bytes freed.\n"
     ]
    }
   ],
   "source": [
    "// Let's test our extended class\n",
    "\n",
    "int main() {\n",
    "    \n",
    "    VectorClass myVec( 3 );\n",
    "    \n",
    "    myVec[ 1 ] = 3.0;\n",
    "    myVec[ 2 ] = 2.0;\n",
    "    myVec[ 3 ] = 1.0;\n",
    "    \n",
    "    std::cout << \"Our vector has dimension \" << myVec.getDim()\n",
    "              << \" and entries: \";\n",
    "    myVec.print( std::cout );\n",
    "}\n",
    "\n",
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8af8c97-2855-41f9-bda2-9d5ae341b7c0",
   "metadata": {},
   "source": [
    "**What is left on our todo-list?**\n",
    "Implement simple methods, such as\n",
    "  - scaling vector with a constant\n",
    "  - adding two vectors together\n",
    "  - compute the Euclidean inner product of two vectors"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "cdfde2f9-fafc-4a9f-99b7-4952720e4521",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting VectorClass.cpp\n"
     ]
    }
   ],
   "source": [
    "%%file VectorClass.cpp\n",
    "\n",
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass{\n",
    "    \n",
    "    public:\n",
    "    \n",
    "        // User should specify dimension at object creation\n",
    "        VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "            // don't allow vectors with zero dimension\n",
    "            assert( dim_ > 0 );\n",
    "\n",
    "            // allocate memory (will throw an exception, if it fails)\n",
    "            vec_ = new double[ dim_ ];\n",
    "            \n",
    "            // be talkative ;-)\n",
    "            std::cout << \"An instance of VectorClass was generated. dim_ = \"\n",
    "                      << dim_ << std::endl;\n",
    "        }\n",
    "\n",
    "        // Don't allow the default constructor\n",
    "        // [prior to C++11 you would have solved this by making it private]\n",
    "        VectorClass() = delete;\n",
    "    \n",
    "        // We need to implement a destructor to free the dynamic memory again,\n",
    "        // otherwise we easily produce memory leaks\n",
    "        ~VectorClass() {\n",
    "            delete[] vec_;\n",
    "            std::cout << \"An instance of VectorClass was destroyed and \"\n",
    "                      << dim_ * sizeof(double)\n",
    "                      << \" bytes freed.\" << std::endl;\n",
    "        }\n",
    "\n",
    "        // provide access to the vector's dimension\n",
    "        unsigned int getDim() { return dim_; }\n",
    "    \n",
    "        // overload the [] for accessing individual entries\n",
    "        double& operator[] ( unsigned int index ){\n",
    "            assert( index != 0 && index <= dim_ );\n",
    "            return vec_[index-1];\n",
    "        }\n",
    "\n",
    "        // pretty print vector to given output stream (default will be std::cout)\n",
    "        void print( std::ostream &os = std::cout ) {\n",
    "            os << \"(\";\n",
    "            for( unsigned int k = 0; k < dim_-1; k++ ) {\n",
    "                os << vec_[k] << \", \";\n",
    "            }\n",
    "            os << vec_[dim_-1] << \")\" << std::endl;\n",
    "        }\n",
    "    \n",
    "        // scale vector with a constant\n",
    "        void scale( double factor ) {\n",
    "            // leave that to students\n",
    "        }\n",
    "    \n",
    "        // add two vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n",
    "        // for the moment leave it with adding another vector to the one on which the method is called;\n",
    "        // let us try the following, and determine, why it will fail ;-)\n",
    "        void add( VectorClass other ) {\n",
    "\n",
    "            // make sure that input vector has correct length\n",
    "            assert( other.getDim() == dim_ );\n",
    "            for( unsigned int k = 0; k < dim_; k++ ) {\n",
    "                vec_[k] += other[k+1];\n",
    "            }\n",
    "            //for( unsigned int k = 1; k <= dim_; k++ ) {\n",
    "            //    this->operator[](k) += other[k];\n",
    "            //}\n",
    "        }\n",
    "    \n",
    "    private:\n",
    "        unsigned int dim_;   // dimension of vector\n",
    "        double* vec_;        // entries of vector\n",
    "\n",
    "};\n",
    "\n",
    "// Modify driver a little bit\n",
    "int main() {\n",
    "\n",
    "    const unsigned int dim = 10;\n",
    "  \n",
    "    // set up 1st vector\n",
    "    VectorClass myVec( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      myVec[ idx ] = static_cast<double>( dim - idx + 1u );\n",
    "    }\n",
    "    myVec.print( std::cout );\n",
    "    \n",
    "    // set up 2nd vector\n",
    "    VectorClass second( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      second[ idx ] = static_cast<double>( idx );\n",
    "    }\n",
    "    second.print( std::cout );\n",
    "    \n",
    "    // add the 2nd to the 1st\n",
    "    myVec.add( second );\n",
    "    myVec.print();\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "f9331dc3-19ff-44a2-a8f5-6e258f9266d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "!g++ -g VectorClass.cpp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4f4d196d-761d-40e1-9d08-5df87307476b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of VectorClass was generated. dim_ = 10\n",
      "(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)\n",
      "An instance of VectorClass was generated. dim_ = 10\n",
      "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n",
      "An instance of VectorClass was destroyed and 80 bytes freed.\n",
      "(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)\n",
      "free(): double free detected in tcache 2\n",
      "Aborted (core dumped)\n"
     ]
    }
   ],
   "source": [
    "!./a.out"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e04234e-4f33-44c4-9adb-0bde893019df",
   "metadata": {},
   "source": [
    "**Note**: The adding itself seems to have worked correctly, but somehow we still have a problem. So what's wrong here?"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7551ddc3-e449-47f1-bdc8-9a8080acdaf9",
   "metadata": {},
   "source": [
    "The answer to this is multifaceted:\n",
    "- Our implementation of *add()* used **call-by-copy**\n",
    "- Since we pass an object, its **copy constructor** is invoked\n",
    "- We haven't implemented one, but the compiler did automatically (thank's for that ;-)\n",
    "- Thus, the copy we get of **other** is a **flat** one!"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02714f7e-52c4-4890-8a87-05ca91d651e5",
   "metadata": {},
   "source": [
    "***\n",
    "We can check on that by making the destructor tell us which block it is going to deallocate and run our code through valgrind:\n",
    "***\n",
    "**==> valgrind ./a.out**\n",
    "<div>\n",
    "==28488== Memcheck, a memory error detector</br>\n",
    "==28488== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.</br>\n",
    "==28488== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info</br>\n",
    "==28488== Command: ./a.out</br>\n",
    "==28488== </br>\n",
    "An instance of VectorClass was generated. dim_ = 10</br>\n",
    "(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)</br>\n",
    "An instance of VectorClass was generated. dim_ = 10</br>\n",
    "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)</br>\n",
    "<font color=\"red\">Going to delete memory starting at address 0x4d9b150</font></br>\n",
    "An instance of VectorClass was destroyed and 80 bytes freed.</br>\n",
    "(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)</br>\n",
    "<font color=\"red\">Going to delete memory starting at address 0x4d9b150</font></br>\n",
    "==28488== Invalid free() / delete / delete[] / realloc()</br>\n",
    "==28488==    at 0x483758B: operator delete[](void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n",
    "==28488==    by 0x109500: VectorClass::~VectorClass() (VectorClass3.cpp:30)</br>\n",
    "==28488==    by 0x109341: main (VectorClass3.cpp:93)</br>\n",
    "==28488==  Address 0x4d9b150 is 0 bytes inside a block of size 80 free'd</br>\n",
    "==28488==    at 0x483758B: operator delete[](void*) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n",
    "==28488==    by 0x109500: VectorClass::~VectorClass() (VectorClass3.cpp:30)</br>\n",
    "==28488==    by 0x109322: main (VectorClass3.cpp:100)</br>\n",
    "==28488==  Block was alloc'd at</br>\n",
    "==28488==    at 0x483650F: operator new[](unsigned long) (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)</br>\n",
    "==28488==    by 0x109450: VectorClass::VectorClass(unsigned int) (VectorClass3.cpp:15)</br>\n",
    "==28488==    by 0x109291: main (VectorClass3.cpp:93)</br>\n",
    "==28488== </br>\n",
    "An instance of VectorClass was destroyed and 80 bytes freed.</br>\n",
    "Going to delete memory starting at address 0x4d9ac80</br>\n",
    "An instance of VectorClass was destroyed and 80 bytes freed.</br>\n",
    "==28488== </br>\n",
    "==28488== HEAP SUMMARY:</br>\n",
    "==28488==     in use at exit: 0 bytes in 0 blocks</br>\n",
    "==28488==   total heap usage: 4 allocs, 5 frees, 73,888 bytes allocated</br>\n",
    "==28488== </br>\n",
    "==28488== All heap blocks were freed -- no leaks are possible</br>\n",
    "==28488== </br>\n",
    "==28488== For counts of detected and suppressed errors, rerun with: -v</br>\n",
    "==28488== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)</br>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48d75c49-dd79-420e-9e00-c75865245b5c",
   "metadata": {},
   "source": [
    "Another way to check on this is to remove the automatic copy constructor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "def80c0f-89b6-4e00-995a-14b8e800975c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting VectorClass.cpp\n"
     ]
    }
   ],
   "source": [
    "%%file VectorClass.cpp\n",
    "\n",
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass{\n",
    "    \n",
    "    public:\n",
    "    \n",
    "        // User should specify dimension at object creation\n",
    "        VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "            // don't allow vectors with zero dimension\n",
    "            assert( dim_ > 0 );\n",
    "\n",
    "            // allocate memory (will throw an exception, if it fails)\n",
    "            vec_ = new double[ dim_ ];\n",
    "            \n",
    "            // be talkative ;-)\n",
    "            std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n",
    "        }\n",
    "\n",
    "        // Don't allow the default constructor\n",
    "        // [prior to C++11 you would solve this by making it private]\n",
    "        VectorClass() = delete;\n",
    "\n",
    "        // Don't allow the following default copy constructor either\n",
    "        VectorClass( VectorClass &other ) = delete;\n",
    "    \n",
    "        // We need to implement a destructor to free the dynamic memory again,\n",
    "        // otherwise we easily produce memory leaks\n",
    "        ~VectorClass() {\n",
    "            delete[] vec_;\n",
    "            std::cout << \"An instance of VectorClass was destroyed and \"\n",
    "                      << dim_ * sizeof(double)\n",
    "                      << \" bytes freed.\" << std::endl;\n",
    "        }\n",
    "\n",
    "        // provide access to the vector's dimension\n",
    "        unsigned int getDim() { return dim_; }\n",
    "    \n",
    "        // overload the [] for accessing individual entries\n",
    "        double& operator[] ( unsigned int index ){\n",
    "            assert( index != 0 && index <= dim_ );\n",
    "            return vec_[index-1];\n",
    "        }\n",
    "\n",
    "        // pretty print vector to given output stream (default will be std::cout)\n",
    "        void print( std::ostream &os = std::cout ) {\n",
    "            os << \"(\";\n",
    "            for( unsigned int k = 0; k < dim_-1; k++ ) {\n",
    "                os << vec_[k] << \", \";\n",
    "            }\n",
    "            os << vec_[dim_-1] << \")\" << std::endl;\n",
    "        }\n",
    "    \n",
    "        // scale vector with a constant\n",
    "        void scale( double factor ) {\n",
    "            // leave that to students\n",
    "        }\n",
    "    \n",
    "        // add two vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n",
    "        // for the moment leave it with adding another vector to the one on which the method is called;\n",
    "        // let us try the following, and determine, why it will fail ;-)\n",
    "        void add( VectorClass other ) {\n",
    "\n",
    "            // make sure that input vector has correct length\n",
    "            assert( other.getDim() == dim_ );\n",
    "            // for( unsigned int k = 0; k < dim_; k++ ) {\n",
    "            //     vec_[k] += other[k+1];\n",
    "            // }\n",
    "            for( unsigned int k = 1; k <= dim_; k++ ) {\n",
    "                this->operator[](k) += other[k];\n",
    "            }\n",
    "        }\n",
    "    \n",
    "    private:\n",
    "        unsigned int dim_;   // dimension of vector\n",
    "        double* vec_;        // entries of vector\n",
    "\n",
    "};\n",
    "\n",
    "// Modify driver a little bit\n",
    "int main() {\n",
    "\n",
    "    const unsigned int dim = 10;\n",
    "  \n",
    "    // set up 1st vector\n",
    "    VectorClass myVec( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n",
    "    }\n",
    "    myVec.print( std::cout );\n",
    "    \n",
    "    // set up 2nd vector\n",
    "    VectorClass other( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      other[ idx ] = static_cast<double>( idx );\n",
    "    }\n",
    "    other.print( std::cout );\n",
    "    \n",
    "    // add the 2nd to the 1st\n",
    "    myVec.add( other );\n",
    "    myVec.print();\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "473de9eb-b04c-4087-8444-8d4b3c4c9fb8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "VectorClass.cpp: In function ‘int main()’:\n",
      "VectorClass.cpp:102:22: error: use of deleted function ‘VectorClass::VectorClass(VectorClass&)’\n",
      "     myVec.add( other );\n",
      "                      ^\n",
      "VectorClass.cpp:27:9: note: declared here\n",
      "         VectorClass( VectorClass &other ) = delete;\n",
      "         ^~~~~~~~~~~\n",
      "VectorClass.cpp:64:14: note:   initializing argument 1 of ‘void VectorClass::add(VectorClass)’\n",
      "         void add( VectorClass other ) {\n",
      "              ^~~\n"
     ]
    }
   ],
   "source": [
    "!g++ VectorClass.cpp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91724843-2e97-4862-888a-291fda71444a",
   "metadata": {},
   "source": [
    "#### Solution?\n",
    "How should we resolve the issue?\n",
    "***\n",
    "For the current case the best solution is to change the interface of our `add()` method to be\n",
    "\n",
    "`void add( const VectorClass& other )`\n",
    "\n",
    "The `const` is no must, but makes it clear to other programmers and the compiler that we are not going to change the object\n",
    "other inside add()."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "30eccd22-8c5e-4062-a1d1-8006981aae99",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[1minput_line_8:61:45: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mno viable overloaded operator[] for type 'const VectorClass'\u001b[0m\n",
      "                this->operator[](k) += other[k];\n",
      "\u001b[0;1;32m                                       ~~~~~^~\n",
      "\u001b[0m\u001b[1minput_line_8:32:17: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function not viable: 'this' argument has type 'const VectorClass', but method is not marked const\u001b[0m\n",
      "        double& operator[] ( unsigned int index ){\n",
      "\u001b[0;1;32m                ^\n",
      "\u001b[0m\u001b[1minput_line_8:71:13: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mfunction definition is not allowed here\u001b[0m\n",
      " int main() {\n",
      "\u001b[0;1;32m            ^\n",
      "\u001b[0m"
     ]
    },
    {
     "ename": "Interpreter Error",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Interpreter Error: "
     ]
    }
   ],
   "source": [
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass{\n",
    "    \n",
    "    public:\n",
    "    \n",
    "        // User should specify dimension at object creation\n",
    "        VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "            // don't allow vectors with zero dimension\n",
    "            assert( dim_ > 0 );\n",
    "\n",
    "            // allocate memory (will throw an exception, if it fails)\n",
    "            vec_ = new double[ dim_ ];\n",
    "            \n",
    "            // be talkative ;-)\n",
    "            std::cout << \"An instance of VectorClass was generated. dim_ = \" << dim_ << std::endl;\n",
    "        }\n",
    "\n",
    "        // Don't allow the default constructor [prior to C++11 you would solve this by making it private]\n",
    "        VectorClass() = delete;\n",
    "\n",
    "        // Don't allow the following default copy constructor either\n",
    "        VectorClass( VectorClass &other ) = delete;\n",
    "    \n",
    "        // We need to implement a destructor to free the dynamic memory again, otherwise we easily produce\n",
    "        // memory leaks\n",
    "        ~VectorClass() {\n",
    "            delete[] vec_;\n",
    "            std::cout << \"An instance of VectorClass was destroyed and \" << dim_ * sizeof(double)\n",
    "                      << \" bytes freed.\" << std::endl;\n",
    "        }\n",
    "\n",
    "        // provide access to the vector's dimension\n",
    "        unsigned int getDim() { return dim_; }\n",
    "    \n",
    "        // overload the [] for accessing individual entries\n",
    "        double& operator[] ( unsigned int index ){\n",
    "            assert( index != 0 && index <= dim_ );\n",
    "            return vec_[index-1];\n",
    "        }\n",
    "\n",
    "        // pretty print vector to given output stream (default will be std::cout)\n",
    "        void print( std::ostream &os = std::cout ) {\n",
    "            os << \"(\";\n",
    "            for( unsigned int k = 0; k < dim_-1; k++ ) {\n",
    "                os << vec_[k] << \", \";\n",
    "            }\n",
    "            os << vec_[dim_-1] << \")\" << std::endl;\n",
    "        }\n",
    "    \n",
    "        // scale vector with a constant\n",
    "        void scale( double factor ) {\n",
    "            // leave that to students\n",
    "        }\n",
    "    \n",
    "        // add two vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n",
    "        // for the moment leave it with adding another vector to the one on which the method is called.\n",
    "    \n",
    "        // let us try the following, and determine, why it will fail ;-)\n",
    "        void add( const VectorClass& other ) {\n",
    "\n",
    "            // make sure that input vector has correct length\n",
    "            assert( other.getDim() == dim_ );\n",
    "            // for( unsigned int k = 0; k < dim_; k++ ) {\n",
    "            //     vec_[k] += other[k+1];\n",
    "            // }\n",
    "            for( unsigned int k = 1; k <= dim_; k++ ) {\n",
    "                this->operator[](k) += other[k];\n",
    "            }\n",
    "        }\n",
    "    \n",
    "    private:\n",
    "        unsigned int dim_;   // dimension of vector\n",
    "        double* vec_;        // entries of vector\n",
    "\n",
    "};\n",
    "\n",
    "// Modify driver a little bit\n",
    "int main() {\n",
    "\n",
    "    const unsigned int dim = 10;\n",
    "  \n",
    "    // set up 1st vector\n",
    "    VectorClass myVec( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n",
    "    }\n",
    "    myVec.print( std::cout );\n",
    "    \n",
    "    // set up 2nd vector\n",
    "    VectorClass other( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      other[ idx ] = static_cast<double>( idx );\n",
    "    }\n",
    "    other.print( std::cout );\n",
    "    \n",
    "    // add the 2nd to the 1st\n",
    "    myVec.add( other );\n",
    "    myVec.print();\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6bfec6d-9eba-4e34-9ec8-e74bfa485664",
   "metadata": {},
   "source": [
    "***\n",
    "Ah, okay. So the `const` was a good idea, but requires a little bit of extra work!\n",
    "\n",
    "We need to implement a second version of the operator overloading that returns a const reference!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "15b02a70-a496-4098-a446-9a60ee30b116",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[1minput_line_10:41:23: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mfunctions that differ only in their return type cannot be overloaded\u001b[0m\n",
      "        const double& operator[] ( unsigned int index ) {\n",
      "\u001b[0;1;32m              ~~~~~~~ ^\n",
      "\u001b[0m\u001b[1minput_line_10:35:17: \u001b[0m\u001b[0;1;30mnote: \u001b[0mprevious definition is here\u001b[0m\n",
      "        double& operator[] ( unsigned int index ){\n",
      "\u001b[0;1;32m        ~~~~~~~ ^\n",
      "\u001b[0m\u001b[1minput_line_10:15:23: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1muse of overloaded operator '<<' is ambiguous (with operand types\n",
      "      'basic_ostream<char, std::char_traits<char> >' and 'unsigned int')\u001b[0m\n",
      "                      << dim_ << std::endl;\n",
      "\u001b[0;1;32m                      ^  ~~~~\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:166:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:174:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(bool __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:178:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(short __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:181:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned short __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:189:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(int __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:201:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:205:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:224:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(float __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:232:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long double __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:517:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:511:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _CharT = char, _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:523:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:528:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1minput_line_10:28:23: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1muse of overloaded operator '<<' is ambiguous (with operand types\n",
      "      'basic_ostream<char, std::char_traits<char> >' and 'unsigned long')\u001b[0m\n",
      "                      << dim_ * sizeof(double)\n",
      "\u001b[0;1;32m                      ^  ~~~~~~~~~~~~~~~~~~~~~\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:166:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:174:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(bool __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:178:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(short __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:181:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned short __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:189:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(int __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:201:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:205:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:224:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(float __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:232:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long double __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:517:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:511:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _CharT = char, _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:523:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:528:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n",
      "\u001b[0;1;32m    ^\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[0m\u001b[1minput_line_10:50:20: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1muse of overloaded operator '<<' is ambiguous (with operand types 'std::ostream'\n",
      "      (aka 'basic_ostream<char>') and 'double')\u001b[0m\n",
      "                os << vec_[k] << \", \";\n",
      "\u001b[0;1;32m                ~~ ^  ~~~~~~~\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:166:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:174:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(bool __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:178:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(short __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:181:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned short __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:189:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(int __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:201:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:205:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:224:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(float __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:232:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long double __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:517:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:511:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _CharT = char, _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:523:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:528:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1minput_line_10:52:16: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1muse of overloaded operator '<<' is ambiguous (with operand types 'std::ostream'\n",
      "      (aka 'basic_ostream<char>') and 'double')\u001b[0m\n",
      "            os << vec_[dim_-1] << \")\" << std::endl;\n",
      "\u001b[0;1;32m            ~~ ^  ~~~~~~~~~~~~\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:166:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:174:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(bool __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:178:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(short __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:181:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned short __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:189:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(int __n);\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:201:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:205:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(unsigned long long __n)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:224:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(float __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:232:7: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function\u001b[0m\n",
      "      operator<<(long double __f)\n",
      "\u001b[0;1;32m      ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:517:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:511:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _CharT = char, _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:523:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, signed char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:528:5: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function [with _Traits = std::char_traits<char>]\u001b[0m\n",
      "    operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)\n",
      "\u001b[0;1;32m    ^\n",
      "\u001b[0m\u001b[1minput_line_10:70:45: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mno viable overloaded operator[] for type 'const VectorClass'\u001b[0m\n",
      "                this->operator[](k) += other[k];\n",
      "\u001b[0;1;32m                                       ~~~~~^~\n",
      "\u001b[0m\u001b[1minput_line_10:35:17: \u001b[0m\u001b[0;1;30mnote: \u001b[0mcandidate function not viable: 'this' argument has type 'const VectorClass', but\n",
      "      method is not marked const\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "        double& operator[] ( unsigned int index ){\n",
      "\u001b[0;1;32m                ^\n",
      "\u001b[0m\u001b[1minput_line_10:80:13: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mfunction definition is not allowed here\u001b[0m\n",
      " int main() {\n",
      "\u001b[0;1;32m            ^\n",
      "\u001b[0mIn file included from input_line_5:1:\n",
      "In file included from /home/mohr/local/miniconda3/envs/cling/include/xeus/xinterpreter.hpp:17:\n",
      "In file included from /home/mohr/local/miniconda3/envs/cling/include/xeus/xcomm.hpp:19:\n",
      "In file included from /home/mohr/local/miniconda3/envs/cling/include/nlohmann/json.hpp:42:\n",
      "In file included from /home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/iterator:64:\n",
      "\u001b[1m/home/mohr/local/miniconda3/envs/cling/bin/../lib/gcc/x86_64-conda-linux-gnu/9.3.0/../../../../x86_64-conda-linux-gnu/include/c++/9.3.0/ostream:568:8: \u001b[0m\u001b[0;1;31merror: \u001b[0m\u001b[1mno member named 'setstate' in 'std::basic_ostream<char>'\u001b[0m\n",
      "        __out.setstate(ios_base::badbit);\n",
      "\u001b[0;1;32m        ~~~~~ ^\n",
      "\u001b[0m\u001b[1minput_line_10:14:23: \u001b[0m\u001b[0;1;30mnote: \u001b[0min instantiation of function template specialization\n",
      "      'std::operator<<<std::char_traits<char> >' requested here\u001b[0m\n",
      "            std::cout << \"An instance of VectorClass was generated. dim_ = \"\n",
      "\u001b[0;1;32m                      ^\n",
      "\u001b[0m"
     ]
    },
    {
     "ename": "Interpreter Error",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Interpreter Error: "
     ]
    }
   ],
   "source": [
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass{\n",
    "    \n",
    "    public:\n",
    "    \n",
    "        // User should specify dimension at object creation\n",
    "        VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "            // don't allow vectors with zero dimension\n",
    "            assert( dim_ > 0 );\n",
    "\n",
    "            // allocate memory (will throw an exception, if it fails)\n",
    "            vec_ = new double[ dim_ ];\n",
    "            \n",
    "            // be talkative ;-)\n",
    "            std::cout << \"An instance of VectorClass was generated. dim_ = \"\n",
    "                      << dim_ << std::endl;\n",
    "        }\n",
    "\n",
    "        // Don't allow the default constructor\n",
    "        // [prior to C++11 you would solve this by making it private]\n",
    "        VectorClass() = delete;\n",
    "\n",
    "        // Don't allow the following default copy constructor either\n",
    "        VectorClass( VectorClass &other ) = delete;\n",
    "    \n",
    "        // We need to implement a destructor to free the dynamic memory again,\n",
    "        // otherwise we easily produce memory leaks\n",
    "        ~VectorClass() {\n",
    "            delete[] vec_;\n",
    "            std::cout << \"An instance of VectorClass was destroyed and \"\n",
    "                      << dim_ * sizeof(double)\n",
    "                      << \" bytes freed.\" << std::endl;\n",
    "        }\n",
    "\n",
    "        // provide access to the vector's dimension\n",
    "        unsigned int getDim() { return dim_; }\n",
    "    \n",
    "        // overload the [] for accessing individual entries\n",
    "        double& operator[] ( unsigned int index ){\n",
    "            assert( index != 0 && index <= dim_ );\n",
    "            return vec_[index-1];\n",
    "        }\n",
    "    \n",
    "        // overload the [] for accessing individual entries\n",
    "        const double& operator[] ( unsigned int index ) {\n",
    "            assert( index != 0 && index <= dim_ );\n",
    "            return vec_[index-1];\n",
    "        }\n",
    "    \n",
    "        // pretty print vector to given output stream (default will be std::cout)\n",
    "        void print( std::ostream &os = std::cout ) {\n",
    "            os << \"(\";\n",
    "            for( unsigned int k = 0; k < dim_-1; k++ ) {\n",
    "                os << vec_[k] << \", \";\n",
    "            }\n",
    "            os << vec_[dim_-1] << \")\" << std::endl;\n",
    "        }\n",
    "    \n",
    "        // scale vector with a constant\n",
    "        void scale( double factor ) {\n",
    "            // leave that to students\n",
    "        }\n",
    "    \n",
    "        // add to vectors together (that's actually a little bit tricky, due to the question \"where to put the result?\"\n",
    "        // for the moment leave it with adding another vector to the one on which the method is called;\n",
    "        // let us try the following, and determine, why it will fail ;-)\n",
    "        void add( const VectorClass& other ) {\n",
    "\n",
    "            // make sure that input vector has correct length\n",
    "            assert( other.getDim() == dim_ );\n",
    "            // for( unsigned int k = 0; k < dim_; k++ ) {\n",
    "            //     vec_[k] += other[k+1];\n",
    "            // }\n",
    "            for( unsigned int k = 1; k <= dim_; k++ ) {\n",
    "                this->operator[](k) += other[k];\n",
    "            }\n",
    "        }\n",
    "    \n",
    "    private:\n",
    "        unsigned int dim_;   // dimension of vector\n",
    "        double* vec_;        // entries of vector\n",
    "\n",
    "};\n",
    "\n",
    "// Modify driver a little bit\n",
    "int main() {\n",
    "\n",
    "    const unsigned int dim = 10;\n",
    "  \n",
    "    // set up 1st vector\n",
    "    VectorClass myVec( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n",
    "    }\n",
    "    myVec.print( std::cout );\n",
    "    \n",
    "    // set up 2nd vector\n",
    "    VectorClass other( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      other[ idx ] = static_cast<double>( idx );\n",
    "    }\n",
    "    other.print( std::cout );\n",
    "    \n",
    "    // add the 2nd to the 1st\n",
    "    myVec.add( other );\n",
    "    myVec.print();\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a78d290-77dc-43a5-93f5-15fa2c650ddb",
   "metadata": {},
   "source": [
    "Shoot, we have a problem with the **signature** now!\n",
    "\n",
    "But, fortunately, there is a way out of this:\n",
    "- We can tell the compiler that a member function does not alter its object! This will also be part of the signature. So we can implement our overloading as\n",
    "`const double& operator[] ( unsigned int index ) const`\n",
    "- We might want to do this also for any other member function that does not change the object, like e.g. `print()`\n",
    "\n",
    "Additionally, let us make add() nicer by using the fact that objects of the same class have access to their private data members!\n",
    "\n",
    "If we do both, we end up with the final version:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0b2a852d-2a07-4f2a-85d2-7833051474d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "#include <cassert>\n",
    "\n",
    "class VectorClass {\n",
    "    \n",
    "public:\n",
    "    \n",
    "  // User should specify dimension at object creation\n",
    "  VectorClass( unsigned int dim ) : dim_(dim) {\n",
    "            \n",
    "    // don't allow vectors with zero dimension\n",
    "    assert( dim_ > 0 );\n",
    "\n",
    "    // allocate memory (will throw an exception, if it fails)\n",
    "    vec_ = new double[ dim_ ];\n",
    "            \n",
    "    // be talkative ;-)\n",
    "    std::cout << \"An instance of VectorClass was generated. dim_ = \" \n",
    "              << dim_ << std::endl;\n",
    "  }\n",
    "\n",
    "  // Don't allow the default constructor\n",
    "  // [prior to C++11 you would solve this by making it private]\n",
    "  VectorClass() = delete;\n",
    "\n",
    "  // Don't allow the following default copy constructor either    \n",
    "  VectorClass( VectorClass &other ) = delete;\n",
    " \n",
    "  // We need to implement a destructor to free the dynamic memory again,\n",
    "  // otherwise we easily produce memory leaks\n",
    "  ~VectorClass() {\n",
    "    std::cout << \"Going to delete memory starting at address \"\n",
    "              << vec_ << std::endl;\n",
    "          \n",
    "    delete[] vec_;\n",
    "    std::cout << \"An instance of VectorClass was destroyed and \"\n",
    "              << dim_ * sizeof(double)\n",
    "              << \" bytes freed.\" << std::endl;\n",
    "  }\n",
    "\n",
    "  // provide access to the vector's dimension\n",
    "  unsigned int getDim() const { return dim_; }\n",
    "    \n",
    "  // overload the [] for accessing individual entries\n",
    "  double& operator[] ( unsigned int index ) {\n",
    "    assert( index != 0 && index <= dim_ );\n",
    "    return vec_[index-1];\n",
    "  }\n",
    "\n",
    "  const double& operator[] ( unsigned int index ) const {\n",
    "    assert( index != 0 && index <= dim_ );\n",
    "    return vec_[index-1];\n",
    "  }\n",
    "\n",
    "  // pretty print vector to given output stream (default will be std::cout)\n",
    "  void print( std::ostream &os = std::cout ) const {\n",
    "    os << \"(\";\n",
    "    for( unsigned int k = 0; k < dim_-1; k++ ) {\n",
    "      os << vec_[k] << \", \";\n",
    "    }\n",
    "    os << vec_[dim_-1] << \")\" << std::endl;\n",
    "  }\n",
    "    \n",
    "  // scale vector with a constant\n",
    "  void scale( double factor ) {\n",
    "    // leave that to students\n",
    "  }\n",
    "    \n",
    "  // add to vectors together (that's actually a little bit tricky,\n",
    "  // due to the question \"where to put the result?\";\n",
    "  // for the moment leave it with adding another vector to the one on which\n",
    "  // the method is called.\n",
    "  void add( const VectorClass& other ) {\n",
    "\n",
    "    // make sure that input vector has correct length\n",
    "    assert( other.dim_ == dim_ );\n",
    "    for( unsigned int k = 0; k < dim_; k++ ) {\n",
    "        vec_[k] += other.vec_[k];\n",
    "    }\n",
    "  }\n",
    "    \n",
    "private:\n",
    "  unsigned int dim_;   // dimension of vector\n",
    "  double* vec_;        // entries of vector\n",
    "\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "81aeb13c-be8f-4e15-b7d7-acffb4a575eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "int main() {\n",
    "\n",
    "    const unsigned int dim = 10;\n",
    "  \n",
    "    // set up 1st vector\n",
    "    VectorClass myVec( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      myVec[ idx ] = static_cast<double>( dim - idx + 1 );\n",
    "    }\n",
    "    myVec.print( std::cout );\n",
    "    \n",
    "    // set up 2nd vector\n",
    "    VectorClass other( dim );\n",
    "    for( unsigned int idx = 1; idx <= dim; idx ++ ) {\n",
    "      other[ idx ] = static_cast<double>( idx );\n",
    "    }\n",
    "    other.print( std::cout );\n",
    "    \n",
    "    // add the 2nd to the 1st\n",
    "    myVec.add( other );\n",
    "    myVec.print();\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "77177a38-424a-4558-8836-9c6f95bdd7c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "An instance of VectorClass was generated. dim_ = 10\n",
      "(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)\n",
      "An instance of VectorClass was generated. dim_ = 10\n",
      "(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\n",
      "(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)\n",
      "Going to delete memory starting at address 0x561e481ef440\n",
      "An instance of VectorClass was destroyed and 80 bytes freed.\n",
      "Going to delete memory starting at address 0x561e480f55f0\n",
      "An instance of VectorClass was destroyed and 80 bytes freed.\n"
     ]
    }
   ],
   "source": [
    "main();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56fb8027-e846-451f-bcda-c28c6e805530",
   "metadata": {},
   "source": [
    "***\n",
    "#### Separating Implementation and Interface\n",
    "\n",
    "- Assume that we have the definition of a `class A` in one source file and want to use the class in another source file `B.cpp`.\n",
    "- For this the compiler needs to known at least that `class A` exists:  \n",
    "  This can be accomplished by a **forward declaration** of the form  `class A;`\n",
    "- However, if we also want to access data or function members of A, the compiler needs further info.\n",
    "- There are three possibilities:\n",
    "  1. Put the complete definition of A into a header file `A.hpp` and include that into the source file `B.cpp`\n",
    "  1. Split up the declaration of the member functions and their implementation into two parts. A file `A.hpp` with the class declaration and a file `A.cpp` with the implementation of the member functions.\n",
    "  1. Use a combination of both, e.g. to allow inlining of certain short methods.\n",
    "- Each of the approaches has pros and cons. So the decision depends on the concrete scenario.\n",
    "***\n",
    "Brief example on the splitting approach:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "867de208-5492-44c0-bf04-7ac9f868908f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting splitA.hpp\n"
     ]
    }
   ],
   "source": [
    "%%file splitA.hpp\n",
    "\n",
    "#include <string>\n",
    "\n",
    "class A {\n",
    "\n",
    "  std::string name_;\n",
    "\n",
    "  public:\n",
    "  void setName( const char* name );\n",
    "  void printName();\n",
    "\n",
    "};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "639efb26-6150-4cdf-aef4-b962ba86a6a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting splitA.cpp\n"
     ]
    }
   ],
   "source": [
    "%%file splitA.cpp\n",
    "\n",
    "// Necessary, because otherwise compiler does not know a class A and a member function setName exists\n",
    "#include \"splitA.hpp\"\n",
    "\n",
    "// Need to prefix the member function name with the class name\n",
    "void A::setName( const char* name ) {\n",
    "  name_ = name;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3ed47b3b-3b45-4fc3-ab31-655915b41251",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting splitB.cpp\n"
     ]
    }
   ],
   "source": [
    "%%file splitB.cpp\n",
    "\n",
    "#include \"splitA.hpp\"\n",
    "\n",
    "int main( void ) {\n",
    "\n",
    "  A obj;\n",
    "  obj.setName( \"An object\" );\n",
    "\n",
    "  // Note: We have not implemented A::printName();\n",
    "  //       However, if it is not used, that's not an issue.\n",
    "  // obj.printName();\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2c897fb0-b3a2-4617-8b92-79dbaaff6b8b",
   "metadata": {},
   "outputs": [],
   "source": [
    "!g++ splitA.cpp splitB.cpp\n",
    "./a.out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "782da53e-ca04-42d3-a318-a1501b17b654",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++14",
   "language": "C++14",
   "name": "xcpp14"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}