CPP
This is the notes for C++ learning.
QT Starter Recipe
In C++ we have two ways to create a new class object:
1 |
|
In QT, we have a class called Qwideget
, it also has a function called show
, use this function can show a window.
1 |
|
We can add a picture in this window:
1 |
|
Now, add a button:
1 |
|
Now add a picture into the button:
1 |
|
Now we want to generate multiple buttons in the window:
1 |
|
Hash Map
Ordered Map (Map)
The elements in the ordered map or we say the map, are sorted. In many cases we use the tree structure for this particular issue, such as binary trees.
1 |
|
Here we defined a hash map, the key is the audio_ID, and the value is the trans:
1 |
|
each time we need to add more one element, we use:
Here, the “audio_file_name” here are the keys.
1 |
|
Retreive the data for audio_file_name_3:
1 |
|
So we can easily grap the value results of the “audio_file_name_3” sr.
Unordered Map
An unordered map is a hash table, it used the hash function to hash the key, and we can easily retreive the bucket we want. This is usually unordered and faster than the map.
1 |
|
1 |
|
Read a wav file and render it
Environmental Install
Intall SDL
https://gist.github.com/aaangeletakis/3187339a99f7786c25075d4d9c80fad5
Git clone the Tutorial Code
https://github.com/BennyQBD/AudioTutorial
Understand audio
https://www.ee.iitb.ac.in/course/~daplab/resources/
Introduction
Environment of C++
In windows there is the Visual C++
Linux: GCC : (the gcc
in most cases are compiled for c, and g++ is for C++)
1 |
|
/ Clang (Clang++)…
The IDE includes: Visual Studio / CodeLite / Code::blocks / Eclipse…
The usual tools included:
1 |
|
The logic of the C++ is compling:
We can follow this simple solutionsource code
>> processing
>> excecutable programs
, the problem is that it can takes a long time. And if there is little editing of the source code, and we have to re-compile it again.
The solution is to divide the source codes into multiple source codes like:
1 |
|
Now we can compile the C++ program more quickly, and easy to maintain. So the core steps can be compile
and link
. But here also comes with a question!!!
What if we used the same variable but in different C++ files, which may cause the collision, so we need to declare the definition of the variable. So the header files is to define the variables
, it defines all the variables, if another c++ files need it, just borrow and declare it, which would be much more convinient. The initial way of using the header files can be declare the variables, but laterly it also includes some definitions.
But the golden rule still be if you declare one variable, you only can declare that variable once. If different c++ files want to refer to that variable, we just include it from the header files.
Here is a “Hello world” example:
1st. C Preprocessor
1 |
|
Now we can do that preprocessing:
1 |
|
The main.i
file will include all the header files into our program and preprocess it.
2st. C Compile
The next step could be the C compiling. It will generate the compiled code.
1 |
|
Now we can get the compiled code: main.s
The main
in here represents the main
function we declared from the previous hello world
program.
The call
in here is to print the “hello world” message.
3st. Assembler & Linker/link editor
The use of the assembler is to use the compiled code into the machine code, that machine can read.
We will refer to the system link or other objects and put it together to get the runable program.
Direct Way
We can direct compile those steps in C++ just by one line of code:
1 |
|
Header Files
Preprocessing
Here is the step to convert the original file into the translated component.
ifdef
Ifdef is to solve the reference too deep problem, we can declare these codes in the header file:
1 |
|
Or we can try pragma_once which can replace the same way like #ifndef:
1 |
|
We suggested to use the #pragma to avoid the collision between different header files.
Header Include
We need to include the header file:
#include “ .h”
If we are using “ “, the system will search the header file from the current directory. In most cases, we will use this way since its we wrote those header files.
#include <>
If we use <>, the system will search from the environment variable, or installed C++ packages, there is no “.h” ending.
System I/O lib
The input stream
can be: cin
, the output stream
can be cout
, cerr
for error printing, clog
for printing the log…
Compile
We will convert the interpreted units into the compiled languages:
Here is a good example:
1 |
|
Now here is the gcc compiling procedures: Here is the -O0
is to compile the codes without any other optimizations.
But if we set like -O 3
, which we will get the third level optimization:
Hello World
Return 0
Every function it will define the return
value except the return value like void
or in main
function.
So in here we will define a main function:
1 |
|
So there maybe a warning like no return value
, we need to add the return value:
1 |
|
Also there is also no need to really declare a formal parameter totally if we do not want use that variable but still want to keep it:
1 |
|
1 |
|
Namespace
If we need to avoid there are multiple functions with the same name, we can use the namespace
to avoid the comflictions.
1 |
|
Or we can do using namespace
:
1 |
|
printf
In C++, we can use the std::cout
or printf
:
1 |
|
It can equals to :
1 |
|
if
The essence of if
is division like trees.
Here is the if
in C++:
1 |
|
while
The essence of the while
is recursion or loop.
Here is an example is guessing number:
1 |
|
The logic here is that, if the user input is not equal to the x
there, so the program will request you to input a number, else it will stop.
Struct
We can define the struct of multiple variables we want to call, especially for those variables who have inner connections.
1 |
|
Type and variables
C++ is a very strong language focused on the type of variables typically.
There are two steps to set a variable in C++:
Initialize
The first step to define a value can be:
1 |
|
Now the variable x
can be created into a storage address
, and pass the value into it.
Pass the value
1 |
|
The computer now will firstly find the address of the x
, and change the value from that address [rbp-4] (-4 = 0xFFFFFFFFFFFFFFFC) into 10 (10 = 0xA). And the same idea is that y
is correspond to [rbp-8 ] (-8 = 0xFFFFFFFFFFFFFFF8) whose the value is 5 = 0x5.
Now the idea can be let y
equals to x
. The logic here is we firstly get the address of y which is [rbp-8] and pass the value under that address into eax
, and pass the eax
value into the address which is [rbp-4].
There is one notice is that, the C++ will sometimes convert the value for you if you misuse it, like in here the 10.5 is a double float number, but the C++ will let you to store that value only include the integer part which is the automatic setting of the C++.
Pointer
Here we can firstly define a pointer, and the pointer address will point to a value.
1 |
|
& is for getting the address. Pointer will also create a space in your computer, it will also can become an object. Pointer it is a procedure to point! Pointer can save the address.
We can also set the default pointer into null
, like:
1 |
|
We can use pointer to save a lot of computing space, especially when we want to pass a relatively “big” value into the function.
1 |
|
Useful Linux Cmds
nm
We can use:
1 |
|
It will print all the referred values or params when the program is compiled.