Theory - C P P

Theory


  • Variables => A container (storage area) used to hold data. Each variable should be given a unique name (identifier)
    • Can only have alphabets, numbers and underscore
    • Cannot begin with a number
    • Cannot begin with an uppercase character
    • Cannot be a keyword defined in C++ language
  • Naming Conventions
    • camelCase
    • PascalCase
    • snake_case
  • Global Variable
    • Bad Practice, Any function can change it which will affect everything else
  • Declaration => Telling the compiler about the variable
  • Definition => Declaration + Space Reservation
  • Memory is stored as Hexadecimal
  • Algorithms that are both deterministic (meaning they produce the same result for the same input every time) and effective (meaning they efficiently solve the problem)
  • Operator Precedence Table => If you don't want to remember it then just use Brackets
  • Reference Variable
    • Reference variable is an alternate name of already existing variable
    • It cannot be changed to refer another variable and should be initialized at the time of declaration and cannot be NULL
    • dataType &ref = variable;
  • Download Compiler
    • https://www.msys2.org/ => Install
    • Next > Finish > Terminal Opens
    • pacman -Syu => Enter y when required => Terminal Closes
    • Open MSYS2 MSYS Terminal
    • pacman -Su => Close terminal
    • Open MSYS2 MinGW 64-bit
    • pacman -Ss gcc
    • pacman -S mingw-w64-x86_64-gcc
    • pacman -Ss gdb => Install Debugger
    • pacman -S mingw-w64-x86_64-gdb
    • gcc --version
    • g++ --version
    • gdb --version
    • C:\msys64\mingw64\bin => Add bin path to Environmental variables
  • Steps
    • Understand > Given Values > Approach > Program
    • Flowchart > Pseudocode > Algorithm > Code
    • Source Code > Compiler > Binary Language
  • Flowchart
    • Components => Terminator, I/O, Process, Condition, Arrows, Connector
  • Process
    • .txt > .cpp > compile > executable (.exe)
  • Terminal
    • To Build File
      • g++ fileName.cpp => To Compile
      • g++ fileName.cpp -o newfileName => To build with user defined newFileName.exe
    • Run the code
      • .\fileName => To Execute
      • .\a.exe => By default this name is created
      • .\fileName > output.txt => Creates a output text file and stores the output in it
    • ctrl + C => To terminate/ stop the running program
    • size .\fileName.exe => Shows size of each memory allocation

Basic


  • sizeof(var) => Returns Size of Variable
  • exit(0) => Program Terminates, Performs some cleaning before termination of the program
  • INT_MIN => Minimum integer value possible
  • INT_MAX => Maximum integer value possible
  • max(val1, val2) => Returns maximum of both values
  • min(val1, val2) => Returns minimum of both values
  • free(var) => Removes the variable from the memory completely
  • abs(value1 - value2) => Returns modulo of the result
  • memset(var, value, size) => Initialize the given Variable with given Value up to Size given
  • typedef oldVarName newVarName => Change Names to simpler forms for easier use
  • Preprocessor Directive => Operates on your source code before the actual compilation process begins
    • #include <iostream> => Includes cin, cout Functions
    • #include <cmath> => Includes sqrt, round Functions
    • #include <math.h> => Includes pow Functions
    • #include <climits> => Includes INT_MIN, INT_MAX
    • #include <algorithm> => Sort Functions
    • #include <ext/pb_ds/tree_policy.hpp> => For policy based data structure
      • #include <ext/pb_ds/assoc_container.hpp>
    • #include <iomanip> => Used to manipulate the output
    • #include <bits/stdc++.h> => Includes all Header files
    • Test Substitution
    • Macro Expansion
      • Token pasting => Allows you to concatenate tokens to form new tokens
        • #define CONCATENATE(x, y) x ## y
  • Namespace
    • using namespace std
    • using std::cout
    • using std::endl
    • using namespace _gnu_pbds => For policy based data structure
    • define var value => Declare a global constant variable
  • Output
    • cout << var => Print Variable as output
    • cout << fixed => Give an output in the fixed point notation
    • cout << scientific => Give an output in the scientific point notation
    • cout << endl;
    • cout << "\n"; => Next Line, Act as Flush
    • cout << flush => Flush the input
    • cout << setw(n); => Set the width of the output
    • cout << left;
    • cout << setfill('value') => Fills the remaining space with the given character
    • cout << setiosflags(ios::uppercase)
    • cout << setiosflags(ios::showbase)
    • cout << setiosflags(ios::showbase | ios::uppercase)
    • cout << resetiosflags(ios::showbase)
    • cout << setbase(n)
    • cout << hex; => Gives output in hexadecimal
    • cerr << var => Acts as cout but message is printed on error screen rather than output screen, Flush by default
    • cout << setpercision(n) => Set the maximum number of decimal places after the point
  • Input
    • cin >> var => Take a Variable input
    • cin.peek()
    • cin.getline(varA, n) => To take input with spaces
    • getline(cin, varS) => Take input value of string with spaces
    • cin.ignore() => Clear the buffer like space and enter to not include it
    • cin does not read Space, Enter, Tab => cin.get() can be used for those
  • Macros
    • #define => This keyword is used
      • Piece of code in a program that is replaced by value of Macro before compilation (Doesn't take memory), Can't be changed later
    • Types
      • Object-like Macros
        • #define DATE 31
      • Chain Macros
        • Macros inside macros are termed as chain macros
      • Multi-line Macros
        • create a multi-line macro use backslash-newline
      • Function-like Macro
        • If we put a space between the macro name and the parentheses in the macro definition then the macro will not work
        • #define min(a, b) (((a) < (b)) ? (a) : (b))
Share: