Learning to Code in C++

Learning to Code in C++

A 10 slide primer to the C++ programming language syntax

37fbf83b7d45711e41e774e29fed710e?s=128

Arnav Gupta

August 30, 2017
Tweet

Transcript

  1. 3.

    Preprocessor Directives #include #ifdef #define • Tells the compiler to

    do certain tasks • Are not part of the program logic • #include = include libraries/header files
  2. 4.

    The main function int main () { return 0; }

    • The function that runs when we execute the program • “0” return means successful execution • Should return “int”, but compilers accept “void” returns too
  3. 5.

    Comments // Single line comment /* Multi line comment */

    • Comments are parts of code that are not compiled or run. • Used to document programs, and make human-readable
  4. 6.

    Variables int a; a = 10; int b = 10;

    int c; c = a; • “int” is data type • “a” is variable name • b = 10, means assignment
  5. 7.

    Data types int a = 10; bool b = true;

    b = false; char c = ‘A’; double d = 10.4; float f = 11.5; Primitive data types are integer, boolean, character, float and double. These can be modified with long, unsigned, and short modifiers.
  6. 8.

    Arrays int a[3] = {1,2,3}; char c[] = {‘a’, ‘b’,

    ‘c’}; a[2] = 5; • An array is a sequence, of finite size, of a particular data size. • int a[3] means “a” is an ordered set of 3 integers. • These can be accessed as a[0], a[1], a[2].
  7. 9.

    Strings char c[4] = “cat”; char d[] = {‘d’, ‘o’,

    ‘g’, ‘\0’}; • In C++ there is no real “string” data type. • A string is an array of char • String needs to be terminated with “\0” char.
  8. 10.

    Printing to console cout << 10; cout << ‘a’; cout

    << “string”; int a = 10; char c = ‘C’; cout << a << c << endl; cout << “new \n line”; • “cout” which means console output, can be used to print to console. • The “<<” operator shifts data into the cout. • We can print data directly, or data variables.
  9. 11.

    Reading from console int a; cin >> a; char c;

    cin >> c; char str[10]; cin >> str; • Like “cout”, “cin” means console input. • We use the “>>” operator to shift data from the cin into our variable. • We can use cin for any primitive data type, as well as string.
  10. 12.

    Pointers • Pointers are location on the memory (RAM) where

    the data is stored. • We use the “ * ” (dereference operator) and “ & ” (address operator) to work with pointers • pointers are also a kind of data type. • address of variable is a pointer • dereference of a pointer gives data stored there