This is the notes for C++ learning.
In C++ we have two ways to create a new class object:
1 | Student s; |
In QT, we have a class called Qwideget
, it also has a function called show
, use this function can show a window.
1 | Qwidget *w = new Qwidget(); |
We can add a picture in this window:
1 | TDWidget *w = new TDWidget("./image.png") |
Now, add a button:
1 | QPushButton *btn = new QPushButton("Button", w); |
Now add a picture into the button:
1 | TDPushButton *btn = new TDPushButton("./image2.png", w); |
Now we want to generate multiple buttons in the window:
1 | for(int i=0; i<11; i++){ |
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 | # include <map> |
Here we defined a hash map, the key is the audio_ID, and the value is the trans:
1 | std::map<std::string, audioRecord> audioMap; |
each time we need to add more one element, we use:
Here, the “audio_file_name” here are the keys.
1 | audioMap[audio_file_name_1] = audioRecord {"audio_file_name_1", sr, bit_depth, dur}; |
Retreive the data for audio_file_name_3:
1 | audioRecord& audio_file_name_3 = audioMap["audio_file_name_3"] |
So we can easily grap the value results of the “audio_file_name_3” sr.
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 | # include <unordered_map> |
1 | std::unordered_map<std::string, audioRecord> audioMap; |
https://gist.github.com/aaangeletakis/3187339a99f7786c25075d4d9c80fad5
https://github.com/BennyQBD/AudioTutorial
https://www.ee.iitb.ac.in/course/~daplab/resources/
In windows there is the Visual C++
Linux: GCC : (the gcc
in most cases are compiled for c, and g++ is for C++)
1 | g++ |
/ Clang (Clang++)…
The IDE includes: Visual Studio / CodeLite / Code::blocks / Eclipse…
The usual tools included:
1 | – /usr/bin/time |
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 | souce_file_1 (source file + headers) >> compile >> object files >> link >> |
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:
1 | #include <iostream> |
Now we can do that preprocessing:
1 | gcc -E ./main.cpp -o ./main.i |
The main.i
file will include all the header files into our program and preprocess it.
The next step could be the C compiling. It will generate the compiled code.
1 | g++ main.i -S -o main.s |
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.
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.
We can direct compile those steps in C++ just by one line of code:
1 | g++ ./main.cpp -o ./main |
Here is the step to convert the original file into the translated component.
Ifdef is to solve the reference too deep problem, we can declare these codes in the header file:
1 | #ifndef HEADER_NAME |
Or we can try pragma_once which can replace the same way like #ifndef:
1 | #pragma once |
We suggested to use the #pragma to avoid the collision between different header files.
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.
The input stream
can be: cin
, the output stream
can be cout
, cerr
for error printing, clog
for printing the log…
We will convert the interpreted units into the compiled languages:
Here is a good example:
1 | int main() |
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:
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 | #include <iostream> |
So there maybe a warning like no return value
, we need to add the return value:
1 | #include <iostream> |
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 | #include <iostream> |
1 |
|
If we need to avoid there are multiple functions with the same name, we can use the namespace
to avoid the comflictions.
1 | #include <iostream> |
Or we can do using namespace
:
1 | int main(){ |
In C++, we can use the std::cout
or printf
:
1 | std::cout << "Hello World" << std::endl; |
It can equals to :
1 | #include <cstdio> // but it is not that quite standard and strict, so we import <cstdio> lib to refer the printf |
The essence of if
is division like trees.
Here is the if
in C++:
1 | if (x == y){ |
The essence of the while
is recursion or loop.
Here is an example is guessing number:
1 | int main(){ |
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.
We can define the struct of multiple variables we want to call, especially for those variables who have inner connections.
1 | #include <iostream> |
C++ is a very strong language focused on the type of variables typically.
There are two steps to set a variable in C++:
The first step to define a value can be:
1 | int x = 10; |
Now the variable x
can be created into a storage address
, and pass the value into it.
1 | x = y |
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++.
Here we can firstly define a pointer, and the pointer address will point to a value.
1 | int main(){ |
& 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 | int *p = nullptr; // this is just a pre-set value, which means the address of it have not be clearly decided. |
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 | struct Str{ |
We can use:
1 | nm ./main.cpp.o | c++filt -t |
It will print all the referred values or params when the program is compiled.