On this page
misc TODO:
static_casts/ reinterpret_cast TODO:
https://www.learncpp.com/cpp-tutorial/introduction-to-type-conversion-and-static_cast/
Literals
#include <iostream>
int main(){
std::cout << 5 << '\n'; // 5 (no suffix) is type int (by default)
std::cout << 5L << '\n'; // 5L is type long
std::cout << 5u << '\n'; // 5u is type unsigned int
// basically the same as
int a = 5; // ok: types match
unsigned int b = 6; // ok: compiler will convert int value 6 to unsigned int value 6
long c = 7; // ok: compiler will convert int value 7 to long value 7
}
Data type | Suffix | Meaning |
---|---|---|
integral | u or U | unsigned int |
integral | l or L | long |
integral | ul, uL, Ul, UL, lu, lU, Lu, LU | unsigned long |
integral | ll or LL | long long |
integral | ull, uLL, Ull, ULL, llu, llU, LLu, LLU | unsigned long long |
integral | z or Z | The signed version of std::size_t (C++23) |
integral | uz, uZ, Uz, UZ, zu, zU, Zu, ZU | std::size_t (C++23) |
floating point | f or F | float |
floating point | l or L | long double |
string | s | std::string |
string | sv | std::string_view |
Const
const
constexpr
#define
Enums
enum season {
spring,
summer,
autumn,
winter
};
compiler flags (#ifdef
)
before compiling we can have some options for what code we want
For example, we can have code only for tests, simulations, or hardware
this is done through #ifdef
In Taproot the options are listed here
Name | variables |
---|---|
Test | ENV_UNIT_TESTS, PLATFORM_HOSTED, RUN_WITH_PROFILING |
Sim | PLATFORM_HOSTED, RUN_WITH_PROFILING |
Hardware | N/A |
We in Hardware mode so in the #ifdef
block line 30 will not be included when compiling
Here you see we are in Test mode so ENV_UNIT_TESTS is turned on. So line 30 will be included
c++ practice
Using everything you learned try to do these:
- simple ArrayList class (try adding these features one by one)
- class field should have: size, capacity
- should use a template and namespace
- Default size
#define size 4
- Constructor should either take an list with values, or nothing and just create an empty array of default size.
- methods:
- constructor/deconstructor
get(int index)
edit(int index, T val)
double()
doubles the arrayappend(T val)
print()
- If you want more you can write sample classes for stacks, queues, trees, etc.