Beruflich Dokumente
Kultur Dokumente
Universidad de Sonora
Hermosillo, Sonora, México.
January 15, 2019
1 Introduction to C++
2 Outlook
Introduction to C++
C++ Bookshelf
C++ resources
I Some of todays most visible and widely used systems have their critical parts
written in C++; examples are:
What is C++?
→ C++ has been adopted as the standard for most coding tasks in Modern
Particle Physics and so it’s well worth getting to know
What is C++?
I Derived from C, Simula, Fortran, and Cobol and allows the creation of Java and
C# languages
I C++ code can be written using any text editor, but to create the actual
programs requires a compiler that creates the machine-readable code
Introducing Variables
→ They are declared by specifying the object type which can be one of the
built-in basic types, e.g.:
I To show how variables work we’ll now go over a basic program that simply
multiplies two numbers together
→ It may seem a little basic at the present, but will help a lot when we analyze
other more advanced topics
I To show how variables work we’ll now go over a basic program that simply
multiplies two numbers together
→ It may seem a little basic at the present, but will help a lot when we analyze
other more advanced topics
→ The value of ‘c’ is then printed to the screen using the Standard Library
‘std::cout’
jamkoons@gmail.com Fı́sica de Partı́culas 17 / 69
Introduction to C++ C++ Data Types
I Zero is returned from the function and the variables are removed
→ Note that, though the memory is now available for re-allocation, the values
are not reset
I The example you’ve just seen using the basic numeric type ‘int’ is an example
of ‘variable declaration’ and the associated creation of an integer object
Variable types
Example
int this integer;
bool Boolean
char Character
int Integer
float Floating point
double Double floating point
void Valueless
wchar t Wide character (handle a larger character set)
Modifiers
I Modifiers → Used to alter the meaning of the base type so that it more precisely fits
the needs of various situations
→ Mostly for char, int and double data types
signed
unsigned
short
long
Example
Modifiers
→ long and short modify the maximum and minimum values that a
data type will hold
I Typedef Declaration
→ typedef type newname
Example
typedef int feet;
feet distance;
Example
enum color{ red, green, blue } c;
c = blue; → by default blue has the value 2
I Definition
→ Where and how much to create the storage for the variable
→ type variable list (one or more identifier names separated by
comas)
I Though you can declare a variable multiple times in your C++ program,
but it can be defined only once in a file, a file or a block of code
I A program can have the same name for local and global variables but
value of local variables inside a function will take preference
C++ Qualifiers
Defining contants:
# define
or use:
const
Operators
I In addition to variable declaration, you have also now met the idea of operators
→ The syntax for this depends on the operator, but a few examples are:
<object1><operator><object2> (e.g. *, +, -)
<object1><operator> (e.g. [], ++)
Types of operators
+, −, ∗, /, % (modulus operator),
Arithmetic Operators
++ (increment operator), −− (decrement operator)
=, + =, −+, ∗ =, / =, % =,
Assignment Operators
<<=, >>=, & =, ˆ=, | =
sizeof(), condition ? x:y,
Misc Operators
. / →, cast, &, *
I To give a taste of variables in action, you’ll now create a program that asks for
two numbers and outputs the product of them
→ To do this you will need to use the ‘std::cin’ variable as well as the
‘std::cout’
→ ‘std::cout’ you have already met in the Hello World program. ‘std::cin’
works in a similar way but for input
double b;
std::cin >> b; // Fill the variable ‘b’ with the user input
I Don’t worry about either of these at the present, just know that:
- std::cin takes input from the user and parses in to variables
- std::cout takes variables and outputs them to the screen
Libraries
More Features
I C++ is
- Statically typed (type checking is performed during compile time
as opposed to run time)
- Compiled
- General purpose
- Case sensitive
Program Flow
I Blocks → Set of logically connected statements that are surrounded by opening and
closing braces
I C++ keywords → There reseved words may not be used as constant, variable or any
other identifier names
→ asm, else, new, this, throw, bool, explicit, etc
Program Flow
I Trigraphs → Is a three character sequence that represents a single character and the
sequence always starts with a two question marks
I White space in C++ → Separates one part of a statement from another and enables
compiler to identify elements within a statement
- Blank line → A line containing only whitespace, possibly with a comment
→ C++ compiler totally ignores it
I Comments → Explanatory statements that help anyone reading its source code. C++
supports single-line and multi-line comments
→ There are a number of ways provided to gain this control over the program:
- Conditional
- Loops
- Functions
I Before we go into the main ways of controlling program flow, it’s important to
understand the idea of scope
→ This refers to ‘blocks of code’ that are separated from each other by braces
→ you have already encountered one such code block in the ‘main’ function
I Variables declared in one code block will not be visible in an ‘outer’ block
but will be present in an ‘inner’ block
I When the end of a code block is reached, any local variables declared and
objects created in that block are destroyed - this is termed going ‘out of
scope’
→ Problem 1 / list 2 / 19
I Loops are very useful for re-using code - a very important practice in all code
development, not just C++. Loops allow you to repeat a code block a set
number of times or until a condition is met
I The first type of loop we will look at is the ‘for’ loop which has the syntax:
for(<initialisation>; <condition>; <loop process>){
<code block>
}
→ A typical example:
I A typical example:
→ This is somewhat simpler than the for loop as it just loops until a
condition evaluates to ‘false’. The syntax is:
while ( <condition>){
<code block>
}
→ An example that does the same thing as the for loop is shown below:
int i = 0;
while (i < 10){
//Do something 10 times
++i;
}
I For loops are introduced later when some functions are discussed
→ The main way of doing this in C++ is through the use of functions that
can then be called in other code blocks
→ Just like a variable, a function must be declared before it can be used:
I After declaration, the function is called by just giving the function name and
the required parameters in brackets
→ At this point, the program flow jumps to this function until it hits a
‘return’ statement or the end of it’s scope
I Declare and define a function multiply that multiplies two numbers together
and returns the result
I Declare and define a function print that prints the given number with an
additonal message
I The function main is a special function that is where the program starts, but
it behaves in the same way
I The multiplication operator has higher precendence than the addition operator
I do ... while loop → Like a while statement, exccept that it tests the condition
at the end of the loop body
I nested loops → Use one or more loops inside any other while, for, or do ...
while loop
I break statement → terminates the loop or switch statement and transfers the
execution to the statement immediately following the loop or switch
I continue statement → Causes the loop to skip the remainder of its body and
immediately re-test its condition prior to re-iterating
I Infinite Loop → The loop condition never becomes false, or the conditional
expression is left empty → for( ; ; )
I switch statement →
I C++ Standard library provides numerous built-in functions that your program
can call
strcat() → concatenate two strings
memcpy() → copy one memory location to another location and many more
functions
Defining a Function
I Call a function → Pass required parameters along with function name, and if
function returns a value then the returned value can be stored
→ Call or inkove the function
→ When its function ending closing brace is reached, it returns program
control back to the main program
Function arguments
I Call by value → Copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the parameter inside
the function have no effect on the argument
I Call by pointer → This method copies the address of the argument into the
formal parameter. Inside the function, the address is used to access the actual
argument used in the call. This means that changes made to the parameter
affect the argument
I Call by reference → This method copies the reference of an argument into the
formal parameter. Inside the function, the reference is used to access the
actual argument used in the call. This means that changes made to the
parameter affect the argument
Numbers in C++
I time()
→ To get the number in seconds on your system time
Arrays in C++
Outlook
Backup
I Storage class → Defines the scope (visibility) and lifetime of variables and/or
functions within a C++ program