{"id":18428952,"url":"https://github.com/akshaybabloo/cpp-notes","last_synced_at":"2025-07-14T06:39:34.857Z","repository":{"id":70617197,"uuid":"50878825","full_name":"akshaybabloo/CPP-Notes","owner":"akshaybabloo","description":"Introduction to C++","archived":false,"fork":false,"pushed_at":"2020-06-08T02:22:27.000Z","size":1016,"stargazers_count":12,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-09T00:05:36.825Z","etag":null,"topics":["cpp","cpp-notes"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/akshaybabloo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-01T23:21:49.000Z","updated_at":"2024-09-16T16:31:05.000Z","dependencies_parsed_at":null,"dependency_job_id":"2848514b-bfbe-4287-b0c2-8c1f84e592b8","html_url":"https://github.com/akshaybabloo/CPP-Notes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/akshaybabloo/CPP-Notes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FCPP-Notes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FCPP-Notes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FCPP-Notes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FCPP-Notes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/akshaybabloo","download_url":"https://codeload.github.com/akshaybabloo/CPP-Notes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/akshaybabloo%2FCPP-Notes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265253400,"owners_count":23735094,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","cpp-notes"],"created_at":"2024-11-06T05:15:04.456Z","updated_at":"2025-07-14T06:39:34.846Z","avatar_url":"https://github.com/akshaybabloo.png","language":"C++","readme":"# C++ Notes\nC++ Notes\n\n**Table of Contents**\n\n- [1 Requirements](#1-requirements)\n  - [1.1 Mac](#11-mac)\n  - [1.2 Windows](#12-windows)\n  - [CMake](#cmake)\n- [2 Basics](#2-basics)\n  - [2.1 Identifiers](#21-identifiers)\n  - [2.2 Defining Variables](#22-defining-variables)\n  - [2.3 Pointer and Reference](#23-pointer-and-reference)\n  - [2.4 Arrays and Strings](#24-arrays-and-strings)\n  - [2.5 Conditions](#25-conditions)\n  - [2.6 Switch Case](#26-switch-case)\n  - [2.7 While Loop](#27-while-loop)\n  - [2.8 For Loop](#28-for-loop)\n  - [2.9 Range base `For` loop](#29-range-base-for-loop)\n  - [2.10 Using stdout](#210-using-stdout)\n- [3 Functions](#3-functions)\n  - [3.1 Defining a function](#31-defining-a-function)\n  - [3.2 Passing values to a function](#32-passing-values-to-a-function)\n  - [3.3 Automatic variables vs. Static variables](#33-automatic-variables-vs-static-variables)\n  - [3.4 Return a function call](#34-return-a-function-call)\n  - [3.5 Function pointer](#35-function-pointer)\n  - [3.6 Overloading function names](#36-overloading-function-names)\n  - [3.7 Overloading operators with function](#37-overloading-operators-with-function)\n  - [3.8 Variable number of arguments](#38-variable-number-of-arguments)\n  - [3.9 Recursive function](#39-recursive-function)\n- [4 Preprocessors](#4-preprocessors)\n  - [4.1 Macro constants](#41-macro-constants)\n  - [4.2 Including a file](#42-including-a-file)\n  - [4.3 Conditions in preprocessor](#43-conditions-in-preprocessor)\n  - [4.4 Macro expansion](#44-macro-expansion)\n  - [4.5 Problems with Macro's](#45-problems-with-macros)\n  - [4.6 Line continuation](#46-line-continuation)\n  - [4.6 Include guard](#46-include-guard)\n- [5 Classes and Objects in C++](#5-classes-and-objects-in-c)\n  - [5.1 Defining Classes and Objects](#51-defining-classes-and-objects)\n  - [5.2 Data members](#52-data-members)\n  - [5.3 Function members](#53-function-members)\n  - [5.4 Constructors and Destructors](#54-constructors-and-destructors)\n  - [5.5 Implicit and Explicit conversion](#55-implicit-and-explicit-conversion)\n  - [5.6 Namespaces](#56-namespaces)\n  - [5.7 Using `this`](#57-using-this)\n  - [5.8 Operator overload: Member function](#58-operator-overload-member-function)\n  - [5.9 Operator overload: Non-member function](#59-operator-overload-non-member-function)\n  - [5.10 Conversion operator](#510-conversion-operator)\n  - [5.11 Using new and delete](#511-using-new-and-delete)\n- [5.6 File IO](#56-file-io)\n  - [5.6.1 Reading Files](#561-reading-files)\n  - [5.6.2 Writing File](#562-writing-file)\n- [6. Data Structures](#6-data-structures)\n  - [6.1 Structs](#61-structs)\n\n## 1 Requirements\n\nThis tutorial is based on C++11. Make sure you are using the latest IDE's - XCode (Mac) or Visual Studio (Windows). You can use other IDE's but make sure it supports the latest C++ version.\n\nIDE's that I think are good:\n\n1. [Visual Studio 2019 Community](https://www.visualstudio.com/) - Cross-platform\n2. [XCode](https://developer.apple.com/xcode/) - Mac\n3. [CLion](https://www.jetbrains.com/clion) - Cross-platform\n4. [NetBeans](https://netbeans.org/downloads/index.html) - Cross-platform\n5. [Eclipse](https://eclipse.org/downloads/) - Cross-platform\n\n### 1.1 Mac\n\n**Adding files to Xcode**\n\n1. Download `XCode` from the App Store.\n2. Open Xcode application and click on `File \u003e New \u003e Workspace...`.\n3. Name the `Workspace` as `CPP-Notes`.\n4. Click on the `+` sign and then click on `Add Files to \"CPP-Notes\"...`\n  ![Add files to workspace](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/xcode-workspace.png)\n\n**Creating Project**\n\n1. Click on the `+` sign and then click on `New Project...`\n  ![Creating a Project to Workspace](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/xcode-create-project.png)\n2. Under `OSX \u003e Application` select `Command Line Tool` and click next. In that, type in the name of the product and make sure you select the language as `C++`.\n  ![Selecting command line tools](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/xcode-create-project-1.png)\n3. Save it where every you want.\n\n### 1.2 Windows\n\n1. Go to [https://www.visualstudio.com/en-us/visual-studio-homepage-vs.aspx](https://www.visualstudio.com/en-us/visual-studio-homepage-vs.aspx) and download `Visual Studio Community` version. Make sure `Visual C++` package is selected and continue the installation.\n2. Once installed, Click on `File \u003e New \u003e Project` or use the shortcut \u003ckbd\u003eCtrl\u003c/kbd\u003e + \u003ckbd\u003eShift\u003c/kbd\u003e + \u003ckbd\u003eN\u003c/kbd\u003e.\n\n  ![Visual Studio New Project](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/vs-new-project.png)\n\n3. We are adding the preprocessing variable because of the way Microsoft has written its c++ compiler.  For that to happen, we are creating a C++ file. Under `Solution Explorer` right click on `Source Files \u003e Add \u003e New Item...` name it as `test.cpp` and type int he following:\n\n  ```cpp\n  #include \u003ccstdio\u003e\n\n  using namespace std;\n\n  int main(int argc, char ** argv) {\n      puts(\"hello\");\n      return 0;\n  }  \n  ```\n\n  From the menubar click on `Build \u003e Build Solution`\n4. Under Solution Explorer, right click on `CPP \u003e Properties`\n  ![Visual Studio Project Property](https://raw.githubusercontent.com/akshaybabloo/CPP-Notes/master/Screenshots/vs-project-property.png)\n  As shown in the image above click on `\u003cEdit...\u003e`. In the text box type in the following:\n\n  ```\n  _CRT_SECURE_NO_WARNINGS\n  _HAS_ITERATOR_DEBUGGING=0\n  ```\n\n  Click on `OK`\n\n**Running the CPP files**\n\n1. Open your command prompt, by doing \u003ckbd\u003eWINDOWS\u003c/kbd\u003e + \u003ckbd\u003eR\u003c/kbd\u003e and type in `cmd`.\n2. To execute the built `test.cpp`, You would have to go to the project folder `CPP \u003e Build \u003e CPP.exe`. Drag and drop `CPP.exe` on the command prompt the press `Enter`, this will output `hello`.\n\n### CMake\n\nCMake is a build system for C++. See their website - [CMake](https://cmake.org) - for more information.\n\n## 2 Basics\n\nC++ inherits most of its code style from C language, but both are very different from each other. Let's consider an example:\n\nSee [2_1_HelloWorld.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_1_HelloWorld.cpp)\n\n```cpp\n// This is a comment\n\n/*\n  This is a block code.\n*/\n#include \u003ccstdio\u003e //\u003c- Libraries\n#include \u003ciostream\u003e\n\nusing namespace std; //\u003c- scope to identifiers\n\nint main(int argc, char ** argv) {\n  puts(\"hello\"); // this statement outputs hello with a new line\n  printf(\"hello\\n\"); // this is similar to puts but doesn't end with new line\n  cout \u003c\u003c \"hello\\n\"; // more complex way to output without new line\n  return 0; // 0 means success\n}\n```\n\nA C++ program can also be written like this (though I wouldn't recommend it):\n\n```cpp\n#include \u003ccstdio\u003e\nusing namespace std;\n\nint\nmain\n(\nint\nargc,\nchar\n**\nargv) {\nputs(\"\nhello\")\n  ;\n  return 0;\n}\n```\n\n**Things to remember**\n\n1. A statement should always end with `;`.\n2. `#Include` should always be in single line without any space followed by `\u003c\u003e` or `\"\"`.\n\n### 2.1 Identifiers\n\nC++ follows the following standards\n\n* [ISO Latin Alphabets](https://en.wikipedia.org/wiki/ISO_basic_Latin_alphabet)\n\n| A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z |\n|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|\n| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |\n\n* [Western arabic numbers](https://en.wikipedia.org/wiki/Arabic_numerals)\n\n| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |\n|---|---|---|---|---|---|---|---|---|---|\n\n* [ASCII Underscore](http://www.theasciicode.com.ar/ascii-printable-characters/underscore-understrike-underbar-low-line-ascii-code-95.html)\n\n| _ |\n|---|\n\n* These identifiers cannot conflict with C++ 86 keywords (which includes 11 tokens)\n\n|    |   |   |\n|-------------------------|------------------------|-----------------------------|\n| alignas (since C++11)   | explicit               | signed                      |\n| alignof (since C++11)   | export(1)              | sizeof                      |\n| and                     | extern                 | static                      |\n| and_eq                  | FALSE                  | static_assert (since C++11) |\n| asm                     | float                  | static_cast                 |\n| auto(1)                 | for                    | struct                      |\n| bitand                  | friend                 | switch                      |\n| bitor                   | goto                   | template                    |\n| bool                    | if                     | this                        |\n| break                   | inline                 | thread_local (since C++11)  |\n| case                    | int                    | throw                       |\n| catch                   | long                   | TRUE                        |\n| char                    | mutable                | try                         |\n| char16_t (since C++11)  | namespace              | typedef                     |\n| char32_t (since C++11)  | new                    | typeid                      |\n| class                   | noexcept (since C++11) | typename                    |\n| compl                   | not                    | union                       |\n| concept (concepts TS)   | not_eq                 | unsigned                    |\n| const                   | nullptr (since C++11)  | using(1)                    |\n| constexpr (since C++11) | operator               | virtual                     |\n| const_cast              | or                     | void                        |\n| continue                | or_eq                  | volatile                    |\n| decltype (since C++11)  | private                | wchar_t                     |\n| default(1)              | protected              | while                       |\n| delete(1)               | public                 | xor                         |\n| do                      | register               | xor_eq                      |\n| double                  | reinterpret_cast       |                             |\n| dynamic_cast            | requires (concepts TS) |                             |\n| else                    | return                 |                             |\n| enum                    | short                  |                             |\n\n* Identifiers are case sensitive.\n\n### 2.2 Defining Variables\n\nSee [2_2_DefineVariables.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_2_DefineVariables.cpp)\n\nIdentifiers (or variables) can be initialized by using the following syntax:\n\n```\nDataType VariableName = \"String\" or number;\n```\n\nYou can also define a read-only variable or a constant in C++ by using the keyword `const`.\n\n### 2.3 Pointer and Reference\n\nsee [2_3_Pointers_Reference.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_3_Pointers_Reference.cpp)\n\nA variable can be called in two ways; `call by value` and `call by reference`.\n\nA pointer and reference are a type of data type, which is commonly used in C/C++ programming. It is a very powerful and confusing concept. It will take some time to understand.\n\n* A pointer can take reference of another variable or a real value\n\nLets understand how identifiers work. When we say `int a = 10;`, an integer variable `a` has a value of `10` in the memory. When we say `int b = a;`, an integer variable `b` has a copy of variable `a`\n\n```cpp\nint a = 10;\nint b = a; // b = 10\n```\n\n**Pointers**\n\nSo, When I say `int *c = \u0026a`, it means that pointer `c` points to the reference of `a`.\n\n```cpp\nint a = 10;\nint *b = \u0026a;\nprintf(\"%d\\n\", *b); // this will print the reference value of a, which is 10\n```\n\n**Reference**\n\nReferences are the address of value in the memory. The pointer points to this address while calling.\n\n* A reference can only call a variable which is already initialized.\n\nSo, when I say `int \u0026d = b` the address if `b` is stored in `d`.\n\n```cpp\nint b = 20;\nint \u0026d = b;\nprintf(\"%d\\n\", d);\n```\n\n### 2.4 Arrays and Strings\n\nSee [2_4_1_Arrays.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_4_Arrays_Strings/2_4_1_Arrays.cpp) and [2_4_2_String.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_4_Arrays_Strings/2_4_2_String.cpp)\n\nThere are two types of Arrays and String in C++, one using C style Arrays \u0026 String and the second one using Standard Library (STL), which will be covered later.\n\n**Arrays**\n\nThe syntax of a C-based array is\n\n```cpp\nint a[5] = {1,2,3,4,5}; // array[SizeOfArray] = {'contents'};\nprintf(\"%d\\n\", a[0]); // 1\n```\n**Strings**\n\nA string is an array of characters terminated string or also called as null terminated string. A string is always terminated with 0.\n\n```cpp\nchar a[6] = {'a', 'b', 'c', 'd', 'e', 0};\nprintf(\"%s\\n\", a); // abcde\n```\n\n### 2.5 Conditions\n\nSee [2_5_Conditions.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_5_Conditions.cpp)\n\nThere are two ways to use conditional operators.\n\n1. Traditional conditional operator.\n2. Ternary conditional operator.\n\n**Traditional conditional operator**\n\n`if..else..` are the common type of conditional statements.\n\n```cpp\nint a = 10;\nint b = 20;\n\nif (a \u003e b) {\n  puts(\"a\u003eb\");\n} else {\n  puts(\"b\u003ea\");\n}\n```\n\n**Ternary conditional operator**\n\nIts a one liner conditional operator\n\n```cpp\nint a = 10;\nint b = 20;\n\nprintf(\"%d\\n\", a \u003e b ? a : b); // if a is greater than b, print a else print b\n```\n\n### 2.6 Switch Case\n\nSee [2_6_Switch_Case.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_6_Switch_Case.cpp)\n\nIt is a conditional statement, which requires an expression which should satisfy a condition. If a condition is not satisfied, then it jumps to `default`. An expression should always be a constant of integer or a character. Syntax looks something like this\n\n```cpp\nswitch (/* expression */) {\n  case /* value */:\n    /* statement */\n}\n```\n\n### 2.7 While Loop\n\nSee [2_7_While_Loop.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_7_While_Loop.cpp)\n\nThere are two types of `While` loop in C++\n\n1. `While` loop\n\n  ```cpp\n  while (/* condition */) {\n    /* code */\n  }\n  ```\n\n2. `do.. While..` loop\n\n  ```cpp\n  do {\n    /* code */\n  } while(/* condition */);\n  ```\n\n### 2.8 For Loop\n\nSee [2_8_For_Loop.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_8_For_Loop.cpp)\n\nFor loop is like `while` loop but with some extra features\n\n```cpp\nfor (size_t i = 0; i \u003c count; i++) {\n  /* code */\n}\n```\n\n### 2.9 Range base `For` loop\n\nSee [2_9_Range_For.cpp](https://github.com/akshaybabloo/CPP-Notes/blob/master/2_Basics/2_9_Range_For.cpp)\n\nStarting from C++11, we can use range based `For` loop\n\n```cpp\nfor (type var1 : var2) {\n  /* code */\n}\n```\n\n### 2.10 Using stdout\n\nC++ also has a way to used object oriented way of printing out contents to the terminal/command prompt. So far we have used `printf` and `puts`.\n\n```cpp\nstd::cout \u003c\u003c \"Hello World!\" \u003c\u003c std::endl;\n```\n\nThe above code shows a bitwise stream of string to `cout`. The `\u003c\u003c` shows left shift of the content.\n\nCreating a compiled version of `cout` uses a lot of resources when compared to `puts` or `printf`, this is because to compile `cout` the whole standard library - `STL` - is copied.\n\n## 3 Functions\n\nA function can be defined as a block of code that is separate from the existing code; that is all the variables used in a function would only belong to that particular function. For example (pseudo code):\n\n```cpp\nint a = 10;\nint b = 20;\n\nc = sum(a, b);\n\nint sum (int a, int b){\n  return a + b;\n}\n\nprintf(\"%d\\n\", c);\n```\n\nFrom the above the variables `a` and `b` in function `sum()` are different from the initialized variable `a` and `b`.\n\nThis particular type of function is call `call by value` function. Another type of function is called as the `call by reference` or sometimes called as the `call by address`. For example (pseudo code):\n\n```cpp\nint a = 10;\nint b = 20;\n\nc = sum(\u0026a, \u0026b);\n\nint sum (int *a, int *b){\n  return *a + *b;\n}\n\nprintf(\"%d\\n\", c);\n```\n\n### 3.1 Defining a function\n\nSee [3_1_1_Define_Function](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_1_Define_Function.cpp)\n\nIn C++, a function should be declared first before calling it. That is:\n\n```cpp\nvoid name(/* arguments */) {\n  /* code */\n}\n\nint main(int argc, char const *argv[]) {\n  name()\n  return 0;\n}\n```\n\nC++ will not compile if the function being called is written after the main function.\n\nSee [3_1_2_Forward_Declaration](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_2_Forward_Declaration.cpp)\n\nTo overcome this problem, we have something called `Forward Declaration`. For example:\n\n```cpp\nvoid name(/* arguments */);\n\nint main(int argc, char const *argv[]) {\n  name()\n  return 0;\n}\n\nvoid name(/* arguments */) {\n  /* code */\n}\n```\n\n`void name(/* arguments */);` is know as `Forward Declaration` or prototype of `name()`\n\nSee [3_1_3_Function_Header.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_3_Function_Header.cpp) and [3_1_3_Function_Header.h](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_1_Define_Function/3_1_3_Function_Header.cpp)\n\nThe common way to do `Forward Declaration` is to put the prototype in a header file. For example:\n\n`3_1_3_Function_Header.cpp`\n\n```cpp\n#include \"3_1_3_Function_Header.h\"\n\nint main(int argc, char const *argv[]) {\n  name()\n  return 0;\n}\n\nvoid name(/* arguments */) {\n  /* code */\n}\n```\n\n`3_1_3_Function_Header.h`\n\n```cpp\n#ifndef 3_1_3_Function_Header_h\n#define 3_1_3_Function_Header_h\n\nvoid name(/* arguments */);\n\n#endif\n```\n\n### 3.2 Passing values to a function\n\nThere are two two ways to pass values to a function\n\n1. Pass by Value\n2. Pass by Reference\n\n**Pass by value:**\n\nSee [3_2_1_Pass_by_Value.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_2_Passing_Values/3_2_1_Pass_by_Value.cpp)\n\nWhen you pass a value to a function, a copy of that value is stored in the argument.\n\n```cpp\nvoid sum(int c, int d) {\n    printf(\"%d\\n\", c + d);\n}\n\nint main(int argc, char const *argv[]) {\n    int a = 10;\n    int b = 20;\n\n    sum(a,b);\n    return 0;\n}\n```\n\n**Pass by Reference:**\n\nSee [3_2_2_Pass_by_Reference.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_2_Passing_Values/3_2_2_Pass_by_Reference.cpp)\n\nWe will talk more about pointers in the coming chapters. In C/C++ (but not limited to theses languages), when you create a variable a memory is allocated to that variable, this memory space has an address (location of it), so the reference here means we are sending the address of the variable rather than the variable itself.\n\nFor example, let us consider `int a = 10;`, which means an integer variable `a` has a value of `10` if you convert this in a diagrammatically you will get the following:\n\n```\nint a = 10;\n----------\n| a | 10 |  --\u003e 123456\n----------\n```\n\nThe number `123456` is the address/location of integer `a` in the memory. When passing the value by reference you send this address, that means you do not create extra space for data; you just use what you have.\n\n```cpp\nvoid sum(int *a, int *b){\n    printf(\"%d\\n\", *a+*b); // *a and *b pointing to the address given to them.\n}\n\nint main(int argc, char ** argv) {\n    int a = 10;\n    int b = 20;\n    sum(\u0026a,\u0026b); // address of a and b\n    return 0;\n}\n```\n\nSee [3_2_3_Pass_by_Reference_Const.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_2_Passing_Values/3_2_3_Pass_by_Reference_Const.cpp)\n\nThere is one problem with pointers in C/C++, that is if you change the contents of the address in `sum()` function you will change the value of the variable. For example If we add a new integer `a=30` or `*a=30` variable to `sum()`\n\n```cpp\nvoid sum(int \u0026a, int \u0026b){\n    a = 30;\n    printf(\"%d\\n\", a+b);\n}\n\n// or\n\nvoid sum(int *a, int *b){\n    *a = 30;\n    printf(\"%d\\n\", *a+*b);\n}\n\n```\n\nThe value of `a` is completely changed, for this not to happen we will have to use a keyword called `const`.\n\n```cpp\nvoid sum(const int \u0026a, const int \u0026b){\n    a = 30;\n    printf(\"%d\\n\", a+b);\n}\n\n// or\n\nvoid sum(const int *a, const int *b){\n    *a = 30;\n    printf(\"%d\\n\", *a+*b);\n}\n\n```\n\n### 3.3 Automatic variables vs. Static variables\n\n**Automatic variable**\n\nSee [3_3_1_Auto_Variable.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_3_Auto_Static_Variables/3_3_1_Auto_Variable.cpp)\n\nBy default, C++ uses automatic variables in every function. Whenever the function is called the variable local to it is initialized on a stack. For example\n\n```cpp\nvoid name() {\n    int a = 10;\n    printf(\"%d\\n\", a);\n    a = 30;\n}\n\nint main(int argc, char const *argv[]) {\n    name();\n    name();// this will always print the same thing\n    return 0;\n}\n```\n\n**Static variable**\n\nSee [3_3_2_Static_Variable.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_3_Auto_Static_Variables/3_3_2_Static_Variable.cpp)\n\nUnlike automatic variables Static variables do not get created on every function call, they just use whatever was previously defined. Don't forget to use `const` if you don't want to change the value.\n\n### 3.4 Return a function call\n\nSee [3_4_Return_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_4_Return_Function.cpp)\n\nTo return a function, we would have to type in the return type and use the keyword `return` at the end of the function. For example:\n\n```cpp\nint number(){\n    return 10;\n}\n\nint main(int argc, char const *argv[]) {\n    printf(\"%d\\n\", number());\n    return 0;\n}\n```\n\n### 3.5 Function pointer\n\nSee [3_5_Function_Pointer.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_5_Function_Pointer.cpp)\n\nYou can call a function by pointing to it, the same way you point to a variable. The only difference is that the data type of the function should match with the data type of the function pointer. For example\n\n```cpp\n\tvoid name( {\n\t\tputs(\"hello\");\n\t}\n\nint main(int argc, char const *argv[]) {\n\tvoid (*function_pointer)() = name;\n\tfunction_pointer();\n\treturn 0;\n}\n```\n\n### 3.6 Overloading function names\n\nSee [3_6_Overloading_Fucntion_Names.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_6_Overloading_Fucntion_Names.cpp)\n\nIn C++ you can have multiple functions with the same name, but the signature (data type) should be same all over the function.\n\n### 3.7 Overloading operators with function\n\nSee [3_7_Oveloading_Operators.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_7_Oveloading_Operators.cpp)\n\nIn C++ you can change the definition of the following 38 operators:\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e+\u003c/td\u003e\n    \u003ctd\u003e-\u003c/td\u003e\n    \u003ctd\u003e*\u003c/td\u003e\n    \u003ctd\u003e/\u003c/td\u003e\n    \u003ctd\u003e%\u003c/td\u003e\n    \u003ctd\u003e^\u003c/td\u003e\n    \u003ctd\u003e\u0026amp;\u003c/td\u003e\n    \u003ctd\u003e|\u003c/td\u003e\n    \u003ctd\u003e~\u003c/td\u003e\n    \u003ctd\u003e!\u003c/td\u003e\n    \u003ctd\u003e=\u003c/td\u003e\n    \u003ctd\u003e\u0026lt;\u003c/td\u003e\n    \u003ctd\u003e\u0026gt;\u003c/td\u003e\n    \u003ctd\u003e+=\u003c/td\u003e\n    \u003ctd\u003e-=\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e*=\u003c/td\u003e\n    \u003ctd\u003e/=\u003c/td\u003e\n    \u003ctd\u003e%=\u003c/td\u003e\n    \u003ctd\u003e^=\u003c/td\u003e\n    \u003ctd\u003e\u0026amp;=\u003c/td\u003e\n    \u003ctd\u003e|=\u003c/td\u003e\n    \u003ctd\u003e\u0026lt;\u0026lt;\u003c/td\u003e\n    \u003ctd\u003e\u0026gt;\u0026gt;\u003c/td\u003e\n    \u003ctd\u003e\u0026gt;\u0026gt;=\u003c/td\u003e\n    \u003ctd\u003e\u0026lt;\u0026lt;=\u003c/td\u003e\n    \u003ctd\u003e==\u003c/td\u003e\n    \u003ctd\u003e!=\u003c/td\u003e\n    \u003ctd\u003e\u0026lt;=\u003c/td\u003e\n    \u003ctd\u003e\u0026gt;=\u003c/td\u003e\n    \u003ctd\u003e\u0026amp;\u0026amp;\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e||\u003c/td\u003e\n    \u003ctd\u003e++\u003c/td\u003e\n    \u003ctd\u003e--\u003c/td\u003e\n    \u003ctd\u003e,\u003c/td\u003e\n    \u003ctd\u003e-\u0026gt;*\u003c/td\u003e\n    \u003ctd\u003e-\u0026gt;\u003c/td\u003e\n    \u003ctd\u003e( )\u003c/td\u003e\n    \u003ctd\u003e[ ]\u003c/td\u003e\n    \u003ctd\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003c/td\u003e\n    \u003ctd\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nThat means an addition operator can be turned into multiplication operator.\n\n### 3.8 Variable number of arguments\n\nSee [3_8_Variable_Arguments.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_8_Variable_Arguments.cpp)\n\nIn C++ you can have multiple arguments given to a function, this can be achieved by adding `...` in the function arguments space.\n\nThere are four macros that needs to be called when using a variable arguments:\n\n* *va_list*: `va_list fa` is used as a parameter.\n* *va_start*: `va_start(ap, parameter)` initialize a variable argument list.\n* *va_arg*: `va_arg(ap, type)` gets the next available argument of a data type.\n* *va_end*: `va_end(ap)` Ends using variable argument list\n\n### 3.9 Recursive function\n\nSee [3_9_Recursive_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/3_Functions/3_9_Recursive_Function.cpp)\n\nIn C++ you can call a function itself. For example:\n\n```cpp\nint main(int argc, char const *argv[]) {\n\tmain();\n\treturn 0;\n}\n```\n\nThese types of functions are called recursive functions. These functions as an alternate to For loops.\n\n## 4 Preprocessors\n\nThe preprocessors are used to process the code before sending it to the compiler. The most common way is the file inclusion using `#include \u003c\u003e`. You can also use macro preprocessors by using `#define NUMBER 1`, these acts like a string substitution.\n\nWhen you open a `.h` the contents of the file you often see looks something like this:\n\n```cpp\n#ifndef main_h\n#define main_h\n\nvoid function_name();\n\n#endif /* main_h */\n```\n\nThey are called as an \"include guard\" which checks for inclusion.\n\nAnother type of preprocessor is used by using `#pragma` that are used (or targeted) for specific compilers and architectures.\n\n### 4.1 Macro constants\n\nSee [4_1_Macro_Constants.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_1_Macro_Constants.cpp)\n\nYou can define a macro constant by using `#define macro`. For example:\n\n```cpp\n#define NUMBER 1\n\nint main(int argc, char const *argv[]) {\n\tprintf(\"%d\\n\", NUMBER);\n\treturn 0;\n}\n```\n\nWhen the above code is compiled the NUMBER is replaced by a literal value before the code reaches to the compiler. At this point you cannot get its address or use pointers.\n\n### 4.2 Including a file\n\nSee [4_2_Include_File](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_2_Include_File/)\n\nTo include a file in a C++ file you would have to use `#include \"file_name.h\"`. This will place all the contents in the `cpp` before the code is sent to the compiler.\n\n### 4.3 Conditions in preprocessor\n\nSee [4_3_Preprocessor_Conditions](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_3_Preprocessor_Conditions)\n\nPreprocessor consists of different types of conditional compilation\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e#if\u003c/td\u003e\n    \u003ctd\u003eOpening `if` condition\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e#else\u003c/td\u003e\n    \u003ctd\u003e`else` condition\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e#elif\u003c/td\u003e\n    \u003ctd\u003e`else if` condition\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e#ifdef\u003c/td\u003e\n    \u003ctd\u003e`if defined` condition\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e#ifndef\u003c/td\u003e\n    \u003ctd\u003e`if not defined` condition\u003c/td\u003e\n  \u003c/tr\u003e\n  \u003ctr\u003e\n    \u003ctd\u003e#endif\u003c/td\u003e\n    \u003ctd\u003e`end if` condition\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\nAlso, there are two alternatives for `#ifdef` and `#ifndef`, they are:\n\n```cpp\n#if defined(macro)\n#if !defined(macro)\n```\n\n### 4.4 Macro expansion\n\nSee [4_4_Macro_Expansion.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_4_Macro_Expansion.cpp)\n\n\nMacro's can also take parameters and replace them when called. For example:\n\n```cpp\n#define ADD(a,b) (a+b)\n\nint main(int argc, char const *argv[]) {\n\tprintf(\"%d\\n\", ADD(10,20));\n\treturn 0;\n}\n```\n\n### 4.5 Problems with Macro's\n\nSee [4_5_Macro_Problems.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_5_Macro_Problems.cpp)\n\nYou should always be careful when using parameterised macros. See [4_5_Macro_Problems.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_5_Macro_Problems.cpp) for more details.\n\n### 4.6 Line continuation\n\nSee [4_6_Line_Continuation.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_6_Line_Continuation.cpp)\n\nIf you want to use complex macros you can use `line continuation` by add `\\` at the end of the each line. For example:\n\n```cpp\n#define LOOPER(i) do \\\n                { \\\n                    printf(\"%d\\n\",i++); \\\n                } while (i \u003c 3);\n```\n\n### 4.6 Include guard\n\nSee [4_6_Include_Guard](https://github.com/akshaybabloo/CPP-Notes/tree/master/4_Preprocessors/4_6_Include_Guard)\n\nThere might be a situation where you might have to define a header file in another header file and when called in a C++ file you might include both header files. When you compile this you will have a `Build fail`, to over come this we have to include something called as `Include guard`. It looks something like this\n\n```cpp\n#ifndef _HEADERNAME_H\n#define _HEADERNAME_H\n...\n#endif\n```\n\n## 5 Classes and Objects in C++\n\nC++ is a an Object Oriented Program, that's what makes it different from C programming language. A class is define by using `class` keyword followed by class name. For example:\n\n```cpp\nclass name_t {\n\tint i; // Data members\npublic: // Function members\n\tname_t (arguments); // Constructor\n\t~name_t (); // Destructor\n\n};\n```\n\nFew points to remember:\n\n* A class can have multiple constructor and only one destructor.\n* A class when called and naming it is called an instance of that class. Example `name_t name;`, `name` is an instance of class `name_t`.\n* Using classes you can allocate memory properly.\n\nMore information can be found [here](http://www.cplusplus.com/doc/tutorial/classes/).\n\n### 5.1 Defining Classes and Objects\n\nThere are different ways to define a class. For example\n\nSee [5_1_1_Define_Classes.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_1_Define_Classes/5_1_1_Define_Classes.cpp)\n\n```cpp\nclass name_t {\n\tint i;\npublic:\n\tvoid some_name (arguments){ /* do something */};\n};\n\nint main(int argc, char const *argv[]) {\n\tname_t obj1;\n\treturn 0;\n}\n```\n\nAnother way is to use `private` keyword, you can then use this to define `private` variables and function after `public`. For example:\n\n```cpp\nclass name_t {\npublic:\n\tvoid some_name (arguments){/* do something */};\nprivate:\n\tint i;\n};\n\nint main(int argc, char const *argv[]) {\n\tname_t obj1;\n\treturn 0;\n}\n```\n\nThe public functions can be used outside, just declare it in the class file and define it outside the `class`. For example:\n\nSee [5_1_2_Define_Classes.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_1_Define_Classes/5_1_2_Define_Classes.cpp)\n\n```cpp\nclass name_t {\npublic:\n\tvoid some_name (arguments);\nprivate:\n\tint i;\n};\n\nvoid name_t::some_name (arguments){/* do something */};\n\nint main(int argc, char const *argv[]) {\n\tname_t obj1;\n\treturn 0;\n}\n```\n\nThe code can be divided into 3 stages:\n\n* *Interface*: Usually kept in the header file.\n\n\t```cpp\n\tclass name_t {\n\tprivate:\n\t\t/* data */\n\tpublic:\n\t\tvoid some_name (arguments);\n\t};\n\t```\n* *Implementation*: Usually kept in an implementation file\n\n\t```cpp\n\tvoid name_t::some_name (arguments){/* do something */};\n\t```\n* *Usage*: Usually kept in `cpp` file\n\n\t```cpp\n\tint main(int argc, char const *argv[]) {\n\t\tname_t obj1;\n\t\treturn 0;\n\t}\n\t```\n\n### 5.2 Data members\n\nSee [5_2_Data_Members.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_2_Data_Members.cpp)\n\nIn C and C++ we can find keyword called `struct`, when used in C++ it is an object. The different between a `struct` and `class` is that, `struct` by default has `public` data members, where as `class` has `private` data members, everything else is the same. For example:\n\n```cpp\nstruct name_t {\n\t/* data */\n};\n\n```\n\nis same as\n\n```cpp\nstruct name_t {\npublic:\n\t/* data */\n};\n```\n\n### 5.3 Function members\n\nSee [5_3_Fucntion_Members.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_3_Fucntion_Members.cpp)\n\nYou can define a same function with different signatures in C++.\n\n### 5.4 Constructors and Destructors\n\nSee [5_4_Constructors_Dstructors.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_4_Constructors_Dstructors.cpp)\n\nA constructor can be used to send in arguments while initializing a class. Destructors are used to clear the memory after the program ends, in C++ destructor are always called at the end of the program by default.\n\n### 5.5 Implicit and Explicit conversion\n\nSee [5_5_Implicit_Explicit.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_5_Implicit_Explicit.cpp)\n\nBy Default C++ does implicit conversion. To make an explicit conversion we need to use `explicit` keyword for a constructor.\n\nFor example:\n\n```cpp\nclass name_t {\n\npublic:\n\texplicit name_t (arguments);\n\tvirtual ~name_t ();\n\n};\n```\n\n### 5.6 Namespaces\n\nSee [5_6_Namespaces.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_6_Namespaces.cpp)\n\nNamespace in C++ acts like a scope to a group of classes, functions etc... A Namespace can be created by using `namespace` keyword. For example:\n\n```cpp\nnamespace name {\n\n};\n```\n\n### 5.7 Using `this`\n\nSee [5_7_Using_this.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_7_Using_this.cpp)\n\nAn object in C++ can access its own pointer, to do so, `this` keyword is used. You can print out the address of a pointer by using\n\n```cpp\nprintf(\"%p\\n\", this);\n```\n\n### 5.8 Operator overload: Member function\n\nSee [5_8_Operator_Overload_Member_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_8_Operator_Overload_Member_Function.cpp)\n\nAny function that belongs to a class is called a member function. Operator overload can be a part of member function.\n\n### 5.9 Operator overload: Non-member function\n\nSee [5_9_Operator_Overload_Non_Member_Function.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_9_Operator_Overload_Non_Member_Function.cpp)\n\nAny function that does not belong to a class is called a  non-member function.\n\n### 5.10 Conversion operator\n\nSee [5_10_Conversion_Operator.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_10_Conversion_Operator.cpp)\n\nYou can use `+=` to concatenate a string with a rational number that belongs to a member function.\n\n### 5.11 Using new and delete\n\nSee [5_11_New_and_Delete.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/5_Classes_Objects/5_11_New_and_Delete.cpp)\n\nC++ allows you to allocate and delete memory for different data types using two keywords, `new` - To allocate memory and `delete` - To deallocate memory. For example:\n\n```cpp\nclass name_t {\nprivate:\n\t/* data */\npublic:\n\tname_t (arguments);\n\tvirtual ~name_t ();\n\n};\n\nint main(int argc, char const *argv[]) {\n\tname_t *a = new name_t(); // to allocate memory\n\tdelete a; // to deallocate memory\n\treturn 0;\n}\n```\n\n## 5.6 File IO\n\nIn this section we will look at how to read and write files using `fstream`.;\n\n### 5.6.1 Reading Files\n\n[6_1_Reading_File.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/6_File_IO/6_1_Reading_File.cpp)\n\nReading a file in C++ can be done using `ifstream`, this data type has many functions associated to it but we want `open`, `good` and `close`. `open` opens a file from the memory, `good` checks if the state of stream is good or not and `close` closes the file after reading from it.\n\n### 5.6.2 Writing File\n\n[6_2_Writing_File.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/6_File_IO/6_2_Writing_File.cpp)\n\nWriting file can be done using `ofstring`, like `ifstring`, this data type provides the same functions - `open` and `close`. If a file already exists with that name, its over written, this can be changed using `ios::app` option that appends any string given to it.\n\n\n## 6. Data Structures\n\n_A data structure is a group of data elements grouped together under one name._ A structure can have multiple data types grouped together to form a way of representation.\n\n### 6.1 Structs\n\n[7_1_Structs.cpp](https://github.com/akshaybabloo/CPP-Notes/tree/master/7_Data_Structures/7_1_Structs.cpp)\n\nIt has a group of data types that can be called by name. It can be represented as:\n\n```cpp\nstruct STRUCT_NAME {\n    DATA_TYPE NAME;\n};\n```\n\nYou can access them as:\n\n```cpp\nSTRUCT_NAME some_name;\nsome_name.NAME\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakshaybabloo%2Fcpp-notes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fakshaybabloo%2Fcpp-notes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fakshaybabloo%2Fcpp-notes/lists"}