C++ from the basics
Statically typed language i.e. variables are checked during compile time
Preprocessor
A program that processes the source code before the compiler sees it.
First, replaces all the comments then detects everything that starts with a pound symbol. Secondly, all preprocessor directives get handled and they are replaced by the intended code. OS specific conditional compilation (click here)
namespaces
Organize and group related code into a logical scope to prevent name conflicts. Let, another company develops cout. What to do? That company should use namespace, std::cout is safe already by its namespace. Click here for example
std namespace
- Isn’t defined in one place and the while is built from multiple headers
stdnamespace is like a book & each header file contributes a chapter to it. when you include a header you only get access to the relevant part ofstd
Expression
A sequence of operators and operands that compute a value
Statement
A valid line of code that gets executed and contains one ore many expressions
int age; // statement
age // expression
20 + 11 // expression
age < 20 // expression
age = 65 // expression
age = 65; // statementDebugging
info registers # shows all registers’ contents
print $pc # prints program counter
Variable declaration and definition
int a = 10;
int b (10);
int c {10};Variable scope | Local scope is checked first
#include <iostream>
int age {10};
int main() {
int age {20};
std::cout << age << std::endl; // 20
return 0;
}Type conversions
When operands are of different types, they all must come to the same type as operations only happen with the same type of operands. Coercion will be attempted and if that can’t happen, compilation error will occur.
Higher types are the types with higher capacity and lower are the opposite.
lower types typically get converted to higher ones. short, char gets converted to int automatically
auto a = 22;
double result = static_cast<double> (a) / 7;
cout << result << endl; // 3.14286
auto b = 22;
double output = b / 7;
cout << output << endl; // 3size_t | large enough to hold any object in cpp for a particular platform
bitwise operator
& | ^ << >>
Operator precedence & associativity
First, the operators with higher precedence will get executed
Secondly, operators at the same precedence level, will be executed according to their associativity.
int a = 19, b = 12, c = 78;
cout << a - b + c << endl; // associativity (19-12)+78 = 85auto
Automatically detect types but types are set at runtime
auto a = 10;
a = "Utshaw"; // compilation error: invalid conversion from 'const char*' to 'int' [-fpermissive]constants
Array
const int prices[] {22, 22, 21, 22}; // auto size calculation
const int ages[5] {100};
cout << ages[2] << endl; // 0Vector | #include <vector>
Deep copy with vector:
vector<int> vec{10, 20}; // copying deeply
vector<int> new_vec {vec};
vec[0] = 88;
cout << new_vec[0] << endl; // 10
cout << vec[0] << endl; // 88
// ...
vector<int> vec{10, 20};
vector<int> new_vec = vec; // assignment does deep copy too
vec[0] = 88;
cout << new_vec[0] << endl; // 10
cout << vec[0] << endl; // 88
vector<int> prices (7, 10); // 7 elements are initialized to 10
cout << prices.size() << endl; // 7
cout << prices.at(3) << endl; // 10
vector<int> temperatures {23, 24}; // 2 elements, initialized as mentioned
cout << temperatures.size() << endl; // 2
vector<double> gpas (8, 4.00); // 8 elements, all are 4.00
cout << gpas[7] << endl; // 4
vector<vector<int>> prices {
{10, 22, 8},
{30, 30, 31},
{40, 42, 41},
};
cout << prices.at(2).at(1) << endl; // 42cstring | all functions treat a sequence as a string up to \0 or null
char name[] {"Farhan\0Tanvir"};
cout << strlen(name) << endl; // 6
char full_name[50] {0}; // initialize the whole array with null
strcpy(full_name, "Farhan Tanvir");
cout << strlen(full_name) << endl; // 13Time complexity for strlen(str) is O(n)
string
string name;
getline(cin, name);
cout << name << endl;
cout << name.substr(7,6) << endl; // substr(start_index, length_in_total) Tanvir
cout << name.find("Tan") << endl; // index of "Tan" 7
cout << (name.find("Utshaw") == string::npos) << endl; // 1
name.erase(7, 6); // (start_index, length) erase length amount from start_index
cout << name.length() << endl; // 7 O(1) time complexity
string battery (3, 'A');
cout << battery << endl; //AAA
cout << battery.length() << endl; // 3