lec1 Introduction
1 Compiled Language and C++
- Concisencess
- Maintianability
- Portability
- gcc
g++ filename -o objectname
2 Hello World
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
return 0;
}
- keywords
Word with special meaning to the complier
int, double, for, auto
- Identifiers
Names of things that are not built into the language
cout, std, x, myFunction
- Literals
Basic constant values whose value is specified directly in the source code
"Hello, world!", 24.3, 0, 'c'
- Operators
Mathematical or logical operations
+, -, &&, %, <<
- Punctuation/Separators
Punctuation defining the structure of a program
{} () , ;
- Whitespace
Spaces of various sorts; ignored by the complier
Spaces, tables, newlines, comments
- Comments exist to explain non-obvious things going on in the code. /* multilines */
// singleline
- Preprocessor to dump in the contents of another file
include
- Outputting some piece of text to the screen.
cout <<
- Namespace
identifiers can be defined within a context-sort of a directory of names
using namespace std;
std::
- Escape sequences
\a, \b, \f, \n, \r, \t, \, ', ", \some interger x
- return 0
indicates that the program should tell the oprating system it has completed successfully. This syntax will be explained in the context of functions
3 Basic Language Features
- values and statements
- operators
- data types
char-1 byte
int-4 bytes
bool-1 byte
double-8 bytes
4 Variables
#include <iostream>
using namesapce std;
int main() {
int x;
x = 4 + 2;
cout << x / 3 << ' ' << x * 2
return 0;
}
- The name of a variable is an identifier token. Identifiers may contain numbers, letters, and underscores( _ ), and may not start with a number
- a single statement that does both declaration and initializaiton
int x = 4 + 2;
5 Input
#include <iostream>
using namespace std;
int main() {
int x;
cin >> x;
cout << x / 3 << ' ' << x * 2;
return 0;
}
-
<< is the syntax for outputting values, cin >> is the syntax for inputting values
- if you have trouble remembering which way the angle brackets go for cout and cin, think of them as arrows pointing in the direction of data flow. cin prepresents the terminal, with data flowing from it to your variables; cout like wise represents the terminal, and your data flows to it.
6 Debugging
- Two kinds of errors: compilation errors and runtime errors Compilation errors are problem raised by the compiler, generally resulting from violations of the syntax rules or misuse of types. These are often caused by typos and the like. Runtime errors are problems that you only spot when you run the program: you did specify a legal program, but it doesn't do what you wanted it to. These are usually more tricky to catch, since the compiler won't tell you about them
网友评论