Intro4A11
This project has been on my to-do list for a long while..And I guess i would never begin to finish it before my gradu....however, i managed to get it started,so here are the notes,hope u like it:)
1.How C++ Works
Intro
this episode is mainly talk about how the compiler compile a source file into a executable binary/program and the basic structure of a source file.
Let's get started with the HelloWorld program we just wrote.
#include<iostream>
int main()
{
std::cout << "Haven't seen you for so long,cpp" << std::endl;
std::cin.get();
}
Preprocessor Statements
Codes look like this,starts with "#"->
#include<iostream>
are called Preprocess Statement in Cpp. And just as their name,they will be preprocessed before the program is compiled.
For example, in the above statement, iostream is introduced into the program as a preloaded statement to provide definitions & declarations for functions std::cout and std::cin.get.
Compile in Visual Studio
The explain of compile-link process is in article<8086>,we won't talk about it here.
In VS, you can simply compile the current,single C++ file by pressIng the key Ctrl+F7
To Build(compile&link)the whole project, including your Main.cpp and whatever C++ file you got,you can right-click the name of your project in the solution window.
How to add another func..
tbc
2.How the C++ Compiler Works
Intro
We program,to be more precise,we write text and transform it to an actual application so that our computer can run. Actually, this transformation is a 2-step process: Compiling & Linking, in this section we'll talk about Compiling.
Preprocessing
The compiler will pre-execute all Preprocessing statements(the statement with #,including #include,#define,#ifdef),let's take #include as a example,when compiler pre-execute this statement,it basically copy and pastes the code inside the included file:
Endbrace.h:
}
Main.cpp
#include<iostream>
void Bkk(const char* message)
{
std::cout << message << std::endl;
#include"EndBrace.h"
To figure out what
How the Linker works in C++
Variables in C++
To use the data in our program,so we have Variables. Variables are basically a piece of named data storaged in the memory.
Primitive data type
There are multiple primitive data types which almost covers the all type u can imagine in C++.However, since C++ is a powerful language,the only difference you need to be aware of is the size of these variables.
Variables | Size(Bytes) | what for |
---|---|---|
char | 1 | character |
bool | 1 | boolean number |
short | 2 | integer |
int | 4 | integer |
long | 4 | integer |
float | 4 | decimal numbers(single) |
double | 8 | decimal numbers(double) |
long long | 8 | integer |
You can check the size of the variable through func "sizeof()"in C++.
and also,you can define reference and pointer thtough
int* a;
int& a;
Functions in C++
Ah,function,here we go again
What for?
Imagine a situation, you need to print a 九九乘法表 on your console with out using any loop, the easiest choice now is to copy and paste such code for 45 times.....?
int a,b;
a= ;
b= ;
cout<< a*b << endl;
Quite an annoying job, isn't it ? You have to "cv" and redefine the variables over and over again,and sometimes it might cuz some unpredictable errors. And it's really difficult to understand these shit code you just wrote.
"Wouldn't it be nice if there was something to integrate these repetitive code?"you thought.
And yes, we do have such "something":
Basic definition
You can define a function with statements in the format below:
type name(parameters)
{
The body of the funtion;
return value;
}
And that magic something you want can be defined like:
void function(int a,int b)
{
cout<< a*b << endl;
}
Return value
The word type here refers to the type of the return value of a function.
In C++,all functions(except void function) MUST have a return value.
Another exception is the main function,its return value defaults to zero so you don't have to declare it manually:)
Fun fact: the return value is only necessary in debug mode💡