Variable Declaration always follows the pattern of: [ = ];
- Every Variable has a type
- Static Type
- Better to initialize variables when they’re declared
- name must start with letter
- don’t include type in name
- Google-Style uses underscores with all lowercase, in snake_case
- names (C++ is…) are case sensitive
Examples:
int uninitialized_var;
int initialized_var = 0;
Out of the Box Types in C++ advanced
- bool this_is_fun = false; // Boolean: True or False
- char carret_return = ‘\n' // Single Character
- int meaning_of_life = 42; // Integer number
- short smaller_int = 42; // Short Number
- long bigger_int = 42; // Long Number
- float fraction = 0.01f; // Single Precision Float
- double precise_num = 0.01 //Double Precision float
- auto some_int = 13; //Automatic Type [int]
Operations on arithmetic types
all character, integer and floating point types are arithmetic
operators: +, -, *, /
comparisons return bool
+=, -=, *=, /=
Avoid == for floats bc they are imprecise
Logical operations ||, &&, ! % modulo increment and decrement a++ === ++a === a += 1
Strings #include to use std::string concatenate with + check if empty str.empty()
#include <iostream>
#include <string>
int main() {
std::string hello="Hello";
std::cout << "Type your name:" << std::endl;
std::string name = "";
std::cin >> name;
std::cout << hello + ", " + name + "!" << std::endl;
return 0;
- #include to use std::array
- store collection of items of same type
- Create from Data:
array<float, 3> arr = {1.0f, 2.0f, 3.0f};
- access item with
arr[i]
- number of stored items
arr.size()
- remove all elements
arr.clear()
- useful access alias:
- first item:
arr.front() == arr[0]
- last item:
arr.back() == arr[arr.size() - 1];
- first item: