To compile the code you have to do g++ exemple.cpp -o nameExemple
after that it will produce an executable in the same dir which we can run by doing
./nameExemple
Programs written in assembly languages tend to be very fast, and assembly is still used today when speed is critical.
A Compiler is a program that reads source code and produces a stand-alone executable program that can then be run.
A programming language gets compiled (You get a program that is not understandable to humans (unless they learned how to read it)). A scripting language gets evaluated at run-time.
There are however interpreters for programming languages and compilers for scripting languages. This is just a rule of thumb that gets it right most of the time.
C++ excels in situations where high performance and precise control over memory and other resources is needed. Here are a few common types of applications that most likely would be written in C++:
- Video games
- Real-time systems (e.g. for transportation, manufacturing, etc…)
- High-performance financial applications (e.g. high frequency trading)
- Graphical applications and simulations
- Productivity / office applications
- Embedded software
- Audio and video processing
Typically, good solutions have the following characteristics:
- They are straightforward (not overly complicated or confusing).
- They are well documented (especially around any assumptions being made or limitations).
- They are built modularly, so parts can be reused or changed later without impacting other parts of the program.
- They are robust, and can recover or give useful error messages when something unexpected happens.
Declaration statements
Jump statements
Expression statements
Compound statements
Selection statements (conditionals)
Iteration statements (loops)
Try blocks
Every C++ program must have a special function named main (all lower case letters). When the program is run, execution starts with the first statement inside of function main and then continues sequentially.
std::cin and std::cout always go on the left-hand side of the statement.
std::cout is used to output a value (cout = character output)
std::cin is used to get an input value (cin = character input)
<< is used with std::cout, and shows the direction that data is moving (if std::cout represents the console, the output data is moving from the variable to the console). std::cout << 4 moves the value of 4 to the console
>> is used with std::cin, and shows the direction that data is moving (if std::cin represents the keyboard, the input data is moving from the keyboard to the variable). std::cin >> x moves the value the user entered from the keyboard into x
Your program produces different results every time it is run.
Your program consistently produces the same incorrect result.
Your program behaves inconsistently (sometimes produces the correct result, sometimes not).
Your program seems like its working but produces incorrect results later in the program.
Your program crashes, either immediately or later.
Your program works on some compilers but not others.
Your program works until you change some other seemingly unrelated code.
A statement is a type of instruction that causes the program to perform some action. Statements are often terminated by a semicolon.
A function is a collection of statements that execute sequentially. Every C++ program must include a special function named main. When you run your program, execution starts at the top of the main function.
Reusability once a function is written, it can be written, it can be called a multiple times from within the probability of copy/pase errors.
When a function becomes too long, too complicated, or hard to understand, it can be split into multiple sub-functions. This is called refactoring.
Keep your progrms simple to start Add features over time. Focus on one area at a time Test each piece of code as you go
QuizTime
A statement is an instruction in a computer program that tells the computer to perform an action.
### What is a function?
A function is a collection of statements that executes sequentially.
main
Execution starts with the first statement inside the main function.
The semicolon (;)
A syntax error is a compiler error that occurs at compile-time when your program violates the grammar rules of the C++ language.
A library file is a collection of precompiled code that has been “packaged up” for reuse in other programs. The C++ Standard Library is a library that ships with C++ that contains additional functionality to use in your programs.
Data is any sequence of symbols (numbers, letters, etc…) that can be interpreted to mean something.
A value is a single piece of data stored in memory.
A variable is a named region of memory.
An identifier is the name that a variable is accessed by.
A type tells the program how to interpret a value in memory.
An integer is a number that can be written without a fractional component.
Initialization gives a variable a value at the point when it is created. Assignment gives a variable a value at some point after it is created.
An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.
What is undefined behavior, and what can happen if you do something that exhibits undefined behavior?
Undefined behavior is the result of executing code whose behavior is not well defined by the language. The result can be almost anything, including something that behaves correctly.