{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  Hello World"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hi, i'm yingshaoxo!"
     ]
    }
   ],
   "source": [
    "#include <iostream>\n",
    "\n",
    "std::cout << \"hi, i'm yingshaoxo!\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## hardcoded literal interger"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### interger"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "83\n",
      "255\n"
     ]
    }
   ],
   "source": [
    "#include <cstdio>\n",
    "\n",
    "unsigned short a = 0b1111; //binary\n",
    "printf(\"%hu\\n\", a);\n",
    "\n",
    "int b = 0123; //octal\n",
    "printf(\"%d\\n\", b);\n",
    "    \n",
    "unsigned long long d = 0xFF; //hex\n",
    "printf(\"%llu\\n\", d);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hex value os 255 is ff\n",
      "unsignd interger is 255, octal intger is 377"
     ]
    }
   ],
   "source": [
    "unsigned int e = 255;\n",
    "printf(\"hex value os %d is %x\\n\", e, e);\n",
    "printf(\"unsignd interger is %u, octal intger is %o\", e, e);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200.00000"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "double f = 2e2;\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6.022141e+23, 602214090000000006225920.000000, 6.02214e+23\n",
      "6.022141e+23, 602214090000000006225920.000000, 6.02214e+23\n",
      "\n",
      "9.750000e+00, 9.750000, 9.75\n",
      "9.750000e+00, 9.750000, 9.75\n"
     ]
    }
   ],
   "source": [
    "double g = 6.0221409e23;\n",
    "printf(\"%le, %lf, %lg\\n\", g, g, g);\n",
    "printf(\"%e, %f, %g\\n\", g, g, g);\n",
    "\n",
    "printf(\"\\n\");\n",
    "\n",
    "float h = 9.75;\n",
    "printf(\"%le, %lf, %lg\\n\", h, h, h);\n",
    "printf(\"%e, %f, %g\\n\", h, h, h);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Character"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "M, Z"
     ]
    }
   ],
   "source": [
    "#include <cstdio>\n",
    "char x = 'M';\n",
    "wchar_t y = L'Z';\n",
    "printf(\"%c, %lc\", x, y);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Bool"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, 0"
     ]
    }
   ],
   "source": [
    "bool b1 = true;\n",
    "bool b2 = false;\n",
    "printf(\"%d, %d\", b1, b2);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "false"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(!b1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "false"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(b1 && b2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(b1 || b2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### iterate by hand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <cstddef> //c standard definations\n",
    "#include <cstdio> // c standard input and output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "unsigned long int maximum = 0;\n",
    "unsigned long int values[] = {10, 50, 20, 40, 0};"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The length of values is 5 \n"
     ]
    }
   ],
   "source": [
    "printf(\"The length of values is %zd \\n\", sizeof(values)/sizeof(unsigned long int));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The maximum value is 50"
     ]
    }
   ],
   "source": [
    "for(size_t i=0; i<5;i++) {\n",
    "    if (values[i]>maximum) maximum = values[i];\n",
    "}\n",
    "printf(\"The maximum value is %lu\", maximum);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### iterate by machine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "50\n",
      "20\n",
      "40\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "for (unsigned long int value : values) {\n",
    "    printf(\"%lu\\n\", value); //%lu means unsigned long int\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "char english[] = \"Do it\";\n",
    "char16_t chinese[] = u\"干它\";"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Do it\n"
     ]
    }
   ],
   "source": [
    "printf(\"%s\\n\", english);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "//ASCII: American Standard Code for Information Interchange"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## struct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### just data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "struct Book{\n",
    "    char name[256];\n",
    "    int year;\n",
    "    int pages;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "Book how_to_speak;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "this book was published at 1998."
     ]
    }
   ],
   "source": [
    "how_to_speak.year = 1998;\n",
    "printf(\"this book was published at %d.\", how_to_speak.year);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### with methods"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "struct MyDay {\n",
    "    int day;\n",
    "    void add_a_day() {\n",
    "        day++;\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "MyDay oneday;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oneday.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "oneday.add_a_day()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oneday.day"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### with privite paramaters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "struct MyDay2 {\n",
    "    private:\n",
    "        int day;\n",
    "    public:\n",
    "        int hi;\n",
    "    void add_a_day() {\n",
    "        day++;\n",
    "        hi = day;\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "MyDay2 oneday2;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "//oneday2.day // you can't access this outside of the struct MyDay2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oneday2.hi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "oneday2.add_a_day()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oneday2.hi"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "// All you have to do is replace struct to class"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyDay3 {\n",
    "    private:\n",
    "        int day;\n",
    "    public:\n",
    "        int hi;\n",
    "        void add_a_day() {\n",
    "            day++;\n",
    "            hi = day;\n",
    "        }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1"
     ]
    }
   ],
   "source": [
    "MyDay3 oneday3;\n",
    "//oneday2.day // you can't access this outside of the struct MyDay2\n",
    "oneday3.hi;\n",
    "oneday3.add_a_day();\n",
    "printf(\"%d\", oneday3.hi);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### constructor"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similar to javascript's constructor, or python's __init__ function.\n",
    "\n",
    "It has to have the same name of the class or struct. Here, the function name and class name are both 'GoToHell'."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <cstdio>\n",
    "\n",
    "class GoToHell {\n",
    "    bool yes = false;\n",
    "    public:\n",
    "        GoToHell() {\n",
    "            yes = true;\n",
    "        }\n",
    "        void show_it() {\n",
    "            printf(\"%d\", yes);\n",
    "        }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1"
     ]
    }
   ],
   "source": [
    "GoToHell hell;\n",
    "hell.show_it();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### constructor with paramaters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <cstdio>\n",
    "\n",
    "class GoToHell2 {\n",
    "    private:\n",
    "        bool yes = false;\n",
    "        int who;\n",
    "    public:\n",
    "        GoToHell2(int i) {\n",
    "            yes = true;\n",
    "            who = i;\n",
    "        };\n",
    "        void show_it() {\n",
    "            printf(\"%d, go to hell!\", who);\n",
    "        };\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55, go to hell!"
     ]
    }
   ],
   "source": [
    "GoToHell2 hell2{ 55 };\n",
    "hell2.show_it();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### multiple constructors with paramaters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Printer {\n",
    "    public:\n",
    "        Printer() {\n",
    "            printf(\"hi! this is made by yingshaoxo!\");\n",
    "        }\n",
    "        Printer(int a) {\n",
    "            printf(\"%d\", a);\n",
    "        }\n",
    "        Printer(int a, int b) {\n",
    "            printf(\"%d\", a+b);\n",
    "        }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hi! this is made by yingshaoxo!"
     ]
    }
   ],
   "source": [
    "Printer printer;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2"
     ]
    }
   ],
   "source": [
    "Printer printer2 = { 2 };"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10"
     ]
    }
   ],
   "source": [
    "Printer printer3 = {5, 5};"
   ]
  }
 ],
 "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
}
