2. Outline
History
Understanding the compiler basics
What is an executable?
C++ program structure
“Hello, World!”
Input, Output, Error
Variables and their types
Getting input from user
Performing arithmetic operations
“if” Conditional statement
3. History of programming
1950s: Fortran, IBM corporation
1950s: Cobol, Grace Hopper (FLOW-MATIC)
1960s: Pascal, Niklaus Wirth
1960s: Basic, Dartmouth College
1970s: Ada, DoD
1972: C, Dennis Ritchie
1979: C++, Bjarne Stroustrup
1991: Java, Sun Microsystems
2001: C#, Microsoft
, …
4.
5.
6. Programs
- A sequence of instructions which perform
a specific task
- The CPU is responsible for executing the commands.
- Each instruction is finally executed by the CPU.
- The final program should be understandable for the CPU
- CPU does not understand the ASCII, human readable texts (known
as source files!!)
- We should translate these sources to machine codes.
- No matter what programming language we use, the CPU instruction
set is fixed.
11. Compiling the Source
- Compiling a source file to create an executable, is performed in
some steps as bellow:
Preprocessing
Compiling
Assembling
Linking
Compiler does these in
one or more passes
12. Compiling the Source
- Compiler does some analysis on source file:
Lexical
Syntax
Semantic
Optimizing the Code
Code Generation
Symbol Table
Error Handler
Intermediate Code Generation
13. Linux C++ compiler
g++:
g++ <SOURCE>
g++ -o <EXECUTABLE> <SOURCE>
gcc:
gcc <SOURCE>
gcc -o <EXECUTABLE> <SOURCE>
GCC: GNU Compiler Collection
14. Linux C++ compiler
- The Gnu Compiler Collection (GCC) project started by Richard
Stallman in 1985.
- GCC includes front-ends for lots of languages including:
• C (gcc)
• C++ (g++)
• Java (gcj)
• Ada (GNAT)
• Objective-C (gobjc)
• Objective-C++
• Fortran
15.
16. Linux Executables
ELF
Relocatable File (.o files)
Shared Object File (.so files)
Executable File
There are 3 main ELF object files:
Linux executable files are in ELF format (Executable and Linkable
Format)
There is no need for specific file extension in Linux, only permission
Core Dumps
17. C++ libraries
Each C++ program consists of “classes” and
“functions”
These are pieces of code that perform special tasks
Printing a string in the output, opening a file for
writing, accessing specific parts of the memory and
…
If your program needs to do these tasks, you can
write the code yourself, or you can use the already
provided functions and classes.
These already prepared functions and classes are
packed into “libraries”
There are lots of libraries available and you can
18. C++ libraries
Shorter development time
Probably better performance
Improves program portability
You don’t need to worry about updating and
maintenance of these libs.
The “C++ standard library” is a rich collection of most
needed functions and classes
19. C++ header files
The C++ standard library is automatically attached to
your program.
The functions and classes in the C++ standard library
are categorized into several sections.
To use each section, you should “include” the
appropriate header file in your source code.
To include a header file, you should use “#include”
phrase. #include <iostream>
20. C++ header files
Header file Usage
iostream
Several standard stream objects
(input, output, …)
memory Memory management utilities
cerrno To view the last error number
cmath Common mathematics functions
chrono C++ time utilities
… …
… …
22. Sample C++ program structure
#include
<iostream>
int main()
{
….
/*
These lines
are comment
as well
*/
….
return 0;
}
// This is a comment
Header files that we need
Compiler will not compile this
line
This means
that the main()
function is
going to return
an integer
after it finishes
it’s work
Each program
should have a
“main” function
This return value is used by
other programs and shell to
check for errors.
There could be lots of other
functions and codes in our
program, starting from main
function
23. Hello, World!
You need to know how to create a text file
in Linux CLI
This entity belongs
to “std” namespace
We indicate that this
way
Returning a Zero from main
means that the program
has ended successfully
Lines starting with # are
directives to the “preprocessor”
which is run before the compiler.
This line tells the preprocessor to
include the “iostream” header file
24. Hello, World!
Always use and update comments in your code
C++ statements should end with “;” (almost all of
them)
“syntax error” indicates you have something wrong in
your coding syntax.
Whitespaces and blank lines are OK.
Use “TAB”, blank lines and whitespaces to have a
human readable code!
25. Input, Output, Error
The std namespace contains some “standard”
library routines.
cin:
Used to get data from standard input
cout:
Used to send data to standard output
cerr:
Used to send data to standard error
, …
Usually the STDIN, STDOUT, and STDERR are
connected to the working terminal
27. How can we redirect
program’s output to one
file and errors to
another?
28. Escape sequence
The “” character in output string is an “escape
character”
According to what comes after “”, the output
differs.
The “” and the following character is named
“escape sequence”
29. Variables
We use variables to store data.
Variables are placed in computer’s memory
(RAM).
To use a variable, we should first declare it.
Variable
declaration
Add comments here
to remember why
you have declared
this variable
Variable declaration could be anywhere, just
remember to declare before use
30. Variables
Each variable has its own type, name, size, and
value.
Integer, float, double,…
String, char, …
, ….
The “int” phrase is a
keyword. We can’t use
keywords as our variable
names!
31. C++ variable types
Type Meaning Example
int Integer numbers -3, 0, 5
double Real numbers 3.4, 0.04, -2.5
float Real numbers ???
char Any single character ‘A’, ‘+’, ‘*’
bool Boolean type “true” or “false”
… … …
Type Meaning
Signed Negative or positive
Unsigned Only positive
Long At least 32 bits
Short At least 16 bits
32. Input from user
Using std::cin, you can get input from command
line.
Remember the direction of “>>” and “<<“
Using “std::cout” at each statement is awkward!
33. How can we provide a
program’s required
inputs to avoid
interaction?
39. Conditional statement
“if” statement is used to implement conditional
statements.
if ( CONDITION )
{
.......
.......
.......
}
else
{
.......
.......
.......
}
If “CONDITION” is true,
then this piece of code is
executed
If “CONDITION” is NOT
true, then this piece of code
is executed
41. Conditional statement
What is wrong in this program?
Notice that we used “using”
directive here instead of
using multiple using
declaration.
This enables the program
to use all the names in any
standard C++ header that
a program might include.
If the statements of the
condition are one line
length, you can omit the {
… } block.
42. End of Section 1
In this section, you learned about:
What is an executable
How compilers work
What are libraries and headers
C++ program structure
How to send strings to stdout, stderr
How to get user input from stdin
Variables and their types
Arithmetic operations and their precedence
“if” conditional statement and it’s usage
Notes de l'éditeur
If CPU executes the code, so why can’t we run MS Windows exe on Linux box?
In first program, show that spaces don’t matter, but coding style matters.
http://en.cppreference.com/w/cpp/header
And libraries…
Test the return value…
<< operator overloading
Return non-zero, work with \n, \t, \a , …
D&D, pp42
Man printf
HW: What is the difference between double and float?, what is the maximum size of these types?
Appendix C deitel&deitel
Ex: What will happen if you replace all “int” s to “double” s??
Deitel & Deitel C++ How to program, p51
Parentheses is not an arithmetic operator
Ex: write a program that gets user’s age and prints the year of birth
Ex: write a program to get user’s age, compute the date of birth and check if he is old.
D&D C++ How to Program, p53