Sie sind auf Seite 1von 72

Introducción a C++

Dr. Javier Murillo

Universidad de Sonora
Hermosillo, Sonora, México.
January 15, 2019

jamkoons@gmail.com Fı́sica de Partı́culas 1 / 69


Overview

1 Introduction to C++

2 Outlook

jamkoons@gmail.com Fı́sica de Partı́culas 2 / 69


Introduction to C++

Introduction to C++

jamkoons@gmail.com Fı́sica de Partı́culas / 69


Introduction to C++ Books

C++ Bookshelf

jamkoons@gmail.com Fı́sica de Partı́culas 3 / 69


Introduction to C++ Books

I The C++ programing language → Fourth Edition, Bjarne Stroustrup


I Practical C++ programing → Second Edition, Steve Oualline

jamkoons@gmail.com Fı́sica de Partı́culas 4 / 69


Introduction to C++ Books

C++ resources

jamkoons@gmail.com Fı́sica de Partı́culas 5 / 69


Introduction to C++ Books

Software development stream

jamkoons@gmail.com Fı́sica de Partı́culas 6 / 69


Introduction to C++ Applications

I Some of todays most visible and widely used systems have their critical parts
written in C++; examples are:

- Amadeus (airline ticketing)


- Amazon (web commerce)
- Bloomberg (finantial information)
- Google (web search)
- Facebook (social media)

I Other programing languages and technologies depend critically on C++’s


performance and reliability in their implementation; examples include:

- Java Virtual Machines (e.g., Oracle’s HotSpot)


- JavaScript interpreters (e.g., Google’s V8)
- Browsers (e.g., Microsoft’s Internet Explorer, Mozilla’s Firefox, Apple’s Safari
and Google’s Chrome)
- Application frameworks (e.g., Microsoft‘s .NET Web services framework)

jamkoons@gmail.com Fı́sica de Partı́culas 7 / 69


Introduction to C++ What is C++?

What is C++?

I C++ is a general purpose object oriented programming language widely used in


software industry and beyond. It was developed by Bjarne Stroustrup in 1979
as an enhacement to the C language (‘C with classes’)

→ Though comprised of fairly basic syntax and conventions, it is incredibly


powerful, especially with the addition of the ‘standard libraries’. Anything you
can think of to do on a computer can be (but not necessarily should be) done
in 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

jamkoons@gmail.com Fı́sica de Partı́culas 8 / 69


Introduction to C++ What is C++?

What is C++?

I “C++ is a general-purpose programing language emphasizing the design and use


of type and rich lightweight abstractions” - Stroustrup

I Direct map to hardware of instructions and fundamental data types


- initially from C

I Classes with contructors and destructors, inheritance and generic programming


techniques - initially from Simula

I Derived from C, Simula, Fortran, and Cobol and allows the creation of Java and
C# languages

jamkoons@gmail.com Fı́sica de Partı́culas 9 / 69


Introduction to C++ What is C++?

Writing and Compiling Code

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

jamkoons@gmail.com Fı́sica de Partı́culas 10 / 69


Introduction to C++ What is C++?

Writing and Compiling Code

I Create a directory to put your code in


I Copy the code here into a text editor
I Save the file as ’ex1.cpp’
I Run the g++ compiler
I Execute the program

jamkoons@gmail.com Fı́sica de Partı́culas 11 / 69


Introduction to C++ What is C++?

Writing and Compiling Code

I The Hello World Program

jamkoons@gmail.com Fı́sica de Partı́culas 12 / 69


Introduction to C++ What is C++?

Basic Syntax of a C++ Program

I # → Preprocessor directive. In this case, including other code

I int main() → A function definiton (see later)

I {} → The braces indicate blocks of code in this case a function

I // or /* */ → It is a good practice to add comments to your code - these


are ignored by the compiler but help you explain what you’re trying to do, both
to other people and yourself (a few months on)

I ; → Every statement in C/C++ must be ended with a semi-colon. This is a


frequent cause of compile errors

jamkoons@gmail.com Fı́sica de Partı́culas 13 / 69


Introduction to C++ C++ Data Types

Introducing Variables

I Variables can be considered as labels to objects or areas in memory

→ They are declared by specifying the object type which can be one of the
built-in basic types, e.g.:

- A boolean (true/false) - ‘bool’


- Integer number - ‘int’
- Floating point number - ‘float’
- Double precision number - ‘double’
- Single character/0-255 number - ‘char’

I Or alternatively, a user-defined type (like classes)

jamkoons@gmail.com Fı́sica de Partı́culas 14 / 69


Introduction to C++ C++ Data Types

Variables and Memory locations

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

jamkoons@gmail.com Fı́sica de Partı́culas 15 / 69


Introduction to C++ C++ Data Types

Variables and Memory locations

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 First, the three variables are declared

→ All this does is assign memory locations, not actual values


→ Other allocations of memory will not overwrite these as long as they are
allocated

jamkoons@gmail.com Fı́sica de Partı́culas 16 / 69


Introduction to C++ C++ Data Types

Variables and Memory locations

I We then assign values to the 3 variables so they are initialised

→ 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

Variables and Memory locations

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

jamkoons@gmail.com Fı́sica de Partı́culas 18 / 69


Introduction to C++ C++ Data Types

Variables and Memory locations

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

→ In C++/C, before using any variable or method it must be declared so the


compiler knows how to deal with it
→ In the basic cases previously, if you removed the ‘int x’ lines, you would get
a compiler error as it wouldn’t know what type the variables were when it
found them later
→ The declaration of a variable follows the syntax:
<object type> <variable name> (<initialization parameters>)
→ This will also create an object of the requested type (i.e. assign the
appropriate memory)

jamkoons@gmail.com Fı́sica de Partı́culas 19 / 69


Introduction to C++ C++ Data Types

Variable types

I Variables → Are nothing but reserved memory locations to store values

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)

jamkoons@gmail.com Fı́sica de Partı́culas 20 / 69


Introduction to C++ C++ Data Types

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

unsigned long int my int


I One can simply use the word unsigned, short, or long without the variable
type word for integers

jamkoons@gmail.com Fı́sica de Partı́culas 21 / 69


Introduction to C++ C++ Data Types

Modifiers

→ long and short modify the maximum and minimum values that a
data type will hold

size hierarchy: short int < int < long int


size hierarchy: float < double < long double

→ There are no long float or short floating point numbers

→ signed types includes both positive and negative numbers


and is the default type

→ unsigned types are always without any sign, that is always


positive

jamkoons@gmail.com Fı́sica de Partı́culas 22 / 69


Introduction to C++ C++ Data Types

Typedef Declarations and Eumerated Types

I Typedef Declaration
→ typedef type newname

Example
typedef int feet;
feet distance;

I Enumerated Type Declaration


→ enum enum-name {list of names} var list;

Example
enum color{ red, green, blue } c;
c = blue; → by default blue has the value 2

I Names of variables must begin either with a letter or an underscore

jamkoons@gmail.com Fı́sica de Partı́culas 23 / 69


Introduction to C++ C++ Data Types

Typedef Declarations and Eumerated Types

jamkoons@gmail.com Fı́sica de Partı́culas 24 / 69


Introduction to C++ C++ Data Types

Variable definition and initialization in C++

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 Initializing → type variable name = value;

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

jamkoons@gmail.com Fı́sica de Partı́culas 25 / 69


Introduction to C++ C++ Data Types

Variable Scope in C++

I Local variables → Inside a function or a block

I Formal parameters → In the definition of function parameters

I Global variables → Outside all functions

I A program can have the same name for local and global variables but
value of local variables inside a function will take preference

I It is a good programing practice to initialize variables properly, otherwise


sometimes program would produce unexpected results

jamkoons@gmail.com Fı́sica de Partı́culas 26 / 69


Introduction to C++ C++ Data Types

C++ Qualifiers

I Qualifiers add an extra quality such as specifying the volatility or constness


of a variable (usually found in code that manipulates hardware directly)

- const → Objects cannot be changed by your program during execution


(it produces a compiler error if the value is attempted to be changed)

- volatile → Tells the compiler that a variable’s value may be changed in


ways not explicitly specified by the program. Indicates that an object may be
changed by something external to the program at any time and so must be
re-read from memory every time it is accessed

jamkoons@gmail.com Fı́sica de Partı́culas 27 / 69


Introduction to C++ C++ Data Types

C++ constants / literals

I Constants/literals → Fixed values that the program may not alter


(literals)

Defining contants:
# define
or use:
const

jamkoons@gmail.com Fı́sica de Partı́culas 28 / 69


Introduction to C++ Operators

Operators

I In addition to variable declaration, you have also now met the idea of operators

→ C++ is based heavily on the idea of operators acting on objects. A few


examples are here:
- Multiplication: a*b
- Addition: a + b
- Increment: a++
- Bitwise shift/stream: a << b
- Modulus: %
- Array: []

→ The syntax for this depends on the operator, but a few examples are:
<object1><operator><object2> (e.g. *, +, -)
<object1><operator> (e.g. [], ++)

jamkoons@gmail.com Fı́sica de Partı́culas 29 / 69


Introduction to C++ Operators

I Operator → A symbol that tells the compiler to perform specific


mathematical or logical manipulations

Types of operators

+, −, ∗, /, % (modulus operator),
Arithmetic Operators
++ (increment operator), −− (decrement operator)

Relational Operators ==, ! =, >, <, >=, <=

Logical Operators &&, ||, !

Bitwise Operators &, |, ˆ, %, ˜, <<, >>

=, + =, −+, ∗ =, / =, % =,
Assignment Operators
<<=, >>=, & =, ˆ=, | =
sizeof(), condition ? x:y,
Misc Operators
. / →, cast, &, *

jamkoons@gmail.com Fı́sica de Partı́culas 30 / 69


Introduction to C++ Tab and ofstream

A More Interactive Program - Tab Space

jamkoons@gmail.com Fı́sica de Partı́culas 31 / 69


Introduction to C++ Tab and ofstream

A More Interactive Program - ofstream

jamkoons@gmail.com Fı́sica de Partı́culas 32 / 69


Introduction to C++ Variables

A More Interactive Program

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

jamkoons@gmail.com Fı́sica de Partı́culas 33 / 69


Introduction to C++ Variables

A More Interactive Program - use of cin

I Basic use of cin ...

jamkoons@gmail.com Fı́sica de Partı́culas 34 / 69


Introduction to C++ Variables

A More Interactive Program - use of cin

I Checking for the user input ...

jamkoons@gmail.com Fı́sica de Partı́culas 35 / 69


Introduction to C++ Variables

A More Interactive Program - use of cin

I Checking for the user input ...

jamkoons@gmail.com Fı́sica de Partı́culas 36 / 69


Introduction to C++ Standardisation and Libraries

Libraries

I ISO standard → Interational Organization for Standarization

I Standard library containers (built-in types)


→ Current ISO standard is C++11
→ Rich set of functions manipulating files, strings, etc

I The Standard Template Library (STL)


→ Rich set of methods, manipulating data structures, etc.

jamkoons@gmail.com Fı́sica de Partı́culas 37 / 69


Introduction to C++ More Features

More Features

I C++ is
- Statically typed (type checking is performed during compile time
as opposed to run time)
- Compiled
- General purpose
- Case sensitive

I C++ supports procedural, object oriented and generic programing

I C++ is a middle-level language


→ Combination of both high level and low level language features

I Most frecuently used and free available compiler is GNU C/C++


compiler

jamkoons@gmail.com Fı́sica de Partı́culas 38 / 69


Introduction to C++ C++ program structure

Program Flow

I #include headers → contain information that is either necessary or useful to the


program

I Makefile → File containing the compilation settings

I Semicolons → Statement terminator

I Blocks → Set of logically connected statements that are surrounded by opening and
closing braces

I C++ identifiers → Name used to identify a variable, function, class, module, or


any other user-defined items
→ some charaters are nor allowed such as @, $, %

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

jamkoons@gmail.com Fı́sica de Partı́culas 39 / 69


Introduction to C++ C++ program structure

Program Flow

I Trigraphs → Is a three character sequence that represents a single character and the
sequence always starts with a two question marks

→ Anywhere they appear: string literals, character literals, comments, processor


directives, etc

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

jamkoons@gmail.com Fı́sica de Partı́culas 40 / 69


Introduction to C++ C++ program structure

Program Flow - Trigraphs


I Trigraphs → Is a three character sequence that represents a single character and the
sequence always starts with a two question marks

jamkoons@gmail.com Fı́sica de Partı́culas 41 / 69


Introduction to C++ C++ program structure

Program Flow - Scope

I It would be difficult to do much with the language if a program was just


executed from top to bottom and you couldn’t control what parts of the code
were executed

→ 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

jamkoons@gmail.com Fı́sica de Partı́culas 42 / 69


Introduction to C++ C++ program structure

Program Flow - Scope

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’

jamkoons@gmail.com Fı́sica de Partı́culas 43 / 69


Introduction to C++ C++ program structure

Program Flow - Conditionals

I Conditionals allow different code blocks to be executed based on the outcome


of a simple test

I It uses a number of additional operators, including:


- Comparison: ==
- Greater/Less than: ><
- Greater/Less than or equal to: <=
- Not equal to: !=

→ The general syntax for this is shown here:


if (a == b)
{
Do something ...
}
else{
Do something else instead ...
}
jamkoons@gmail.com Fı́sica de Partı́culas 44 / 69
Introduction to C++ C++ program structure

Adding More Functionality

I We can now control the flow of the program depending on a comparison

→ Problem 1 / list 2 / 19

jamkoons@gmail.com Fı́sica de Partı́culas 45 / 69


Introduction to C++ C++ program structure

Program Flow - Loops

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:

for(int i = 0; i < 10; ++i){


//Do something 10 times
}

jamkoons@gmail.com Fı́sica de Partı́culas 46 / 69


Introduction to C++ C++ program structure

Program Flow - Loops

I A typical example:

for(int i = 0; i < 10; ++i){


//Do something 10 times
}

→ The initialisation step is performed at the start of the loop


→ The loop continues until the condition evaluates ‘false’
→ After every loop cycle, the process code is run
→ Be careful about the position of the semi-colons
→ You can use ‘break’ to terminate a loop and ‘continue’ to skip to the next
iteration

jamkoons@gmail.com Fı́sica de Partı́culas 47 / 69


Introduction to C++ C++ program structure

Program Flow - Loops

I The other type of loop we will look at is the ‘while’ loop

→ 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;
}

jamkoons@gmail.com Fı́sica de Partı́culas 48 / 69


Introduction to C++ C++ program structure

Program Flow - Loops

jamkoons@gmail.com Fı́sica de Partı́culas 49 / 69


Introduction to C++ C++ program structure

Program Flow - Loops

I About while loops ...

- The evaluation of the condition is done at the beginning of each loop


- The loop will continue until the condition is false, so be careful of infinite
loops
- As before, use ‘break’ to get out of the loop and ‘continue’ to skip the
iteration

I About std::cin ...

- std::cin.clear(); //clear the fail flag


- std::cin.ignore(INT MAX, "\n"); //clear the cin buffer

I For loops are introduced later when some functions are discussed

jamkoons@gmail.com Fı́sica de Partı́culas 50 / 69


Introduction to C++ C++ program structure

Program Flow - break

jamkoons@gmail.com Fı́sica de Partı́culas 51 / 69


Introduction to C++ C++ program structure

Program Flow - Functions

I It’s good practice to re-use as much code as possible

→ 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:

<return type> <function name> (<arguments>) { <code block> }

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

→ An important point to remember is that variables are passed ‘by value’


i.e. the object value is cipied to the new variable, the object itself is not sent
to the function

jamkoons@gmail.com Fı́sica de Partı́culas 52 / 69


Introduction to C++ C++ program structure

Program Flow - Functions

I Implementing functions within the program

jamkoons@gmail.com Fı́sica de Partı́culas 53 / 69


Introduction to C++ C++ program structure

Program Flow - Functions

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

jamkoons@gmail.com Fı́sica de Partı́culas 54 / 69


Introduction to C++ Coding Basics

Operators Precedence in C++

I The multiplication operator has higher precendence than the addition operator

jamkoons@gmail.com Fı́sica de Partı́culas 55 / 69


Introduction to C++ Coding Basics

Interacting with the terminal

I << → Pass multiple values out to the screen

I endl → Inserts a new line character after every line

I sizeof() → Returns the size of various data types

jamkoons@gmail.com Fı́sica de Partı́culas 56 / 69


Introduction to C++ Coding Basics

C++ Loop Types

I A loop statement allows us to execute a statement or group of statements


multiple times and following in the general form of a loop statement in most
of the programing languages

jamkoons@gmail.com Fı́sica de Partı́culas 57 / 69


Introduction to C++ Coding Basics

C++ Loop Types

I while loop → Repeat a statement or group of statements while a given


condition is true

I for loop → Execute a sequence of statements multiple times & abbreviates


the code that manages the loop variable

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

jamkoons@gmail.com Fı́sica de Partı́culas 58 / 69


Introduction to C++ Coding Basics

Loop Control Statements

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 goto statement → Transfers control to the labeled statement. Though it is


not advised to use goto statement in a program

jamkoons@gmail.com Fı́sica de Partı́culas 59 / 69


Introduction to C++ Coding Basics

More About Loops

I Infinite Loop → The loop condition never becomes false, or the conditional
expression is left empty → for( ; ; )

C++ decision making statements

I if statement → Consists of a boolean expression followed by one or more


statements

I if else statement → Allowing boolean to be false

I switch statement →

jamkoons@gmail.com Fı́sica de Partı́culas 60 / 69


Introduction to C++ C++ Functions

What is a function in C++?

I Function → Group of statements that together perform a task. A program


has at least a main() function

I A Function Declaration → Tells the compiler about the function‘s name,


return type and parameters

I A Function Definiton → provides the actual body of the function

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

jamkoons@gmail.com Fı́sica de Partı́culas 61 / 69


Introduction to C++ C++ Functions

Defining a Function

return type function name(parameter list){

body of the function ...

jamkoons@gmail.com Fı́sica de Partı́culas 62 / 69


Introduction to C++ Calling a Function

Calling a function in C++

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

jamkoons@gmail.com Fı́sica de Partı́culas 63 / 69


Introduction to C++ Calling a Function

Calling a function in C++

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

I By default C++ uses call by value to pass arguments

jamkoons@gmail.com Fı́sica de Partı́culas 64 / 69


Introduction to C++ Numbers, Operations and Arrays

Numbers in C++

I Numbers in C++ → short / int / long / float / double

jamkoons@gmail.com Fı́sica de Partı́culas 65 / 69


Introduction to C++ Operations

Math Operations in C++

I Available functions in standard C and C++ libraries → Built-in functions,


these are functions that can be included in your program to be used
- cos(double)
- sin(double)
- tan(double)
- log(double)
- pow(double, double)
- hypot(double, double)
- sqrt(double)
- abs(int)
- fabs(double)
- floor(double) → rounding down

jamkoons@gmail.com Fı́sica de Partı́culas 66 / 69


Introduction to C++ Operations

Random Numbers in C++

I rand() function (alone generates a pseudo-random number)


→ fixed by adding the srand() function

I time()
→ To get the number in seconds on your system time

jamkoons@gmail.com Fı́sica de Partı́culas 67 / 69


Introduction to C++ Arrays

Arrays in C++

I Array → Stores a fixed-size sequential collection of elements of the same type,


store a collection of data

I All arrays consist of continuous memory locations → the lowest address


corresponds to the first element and the highest address to the last element

I Declaring Arrays → type array name[array size] → Single-dimension


array

I Multidimensional Arrays → Supported by C++. A pointer to the first


element of an array can be generated by simply specifying the array name
without any index

I Passing Arrays to Functions → one can pass to a function a pointer to an


array by specifying the array‘s name without any index

I Return array from functions → C++ allows a function to return an array

jamkoons@gmail.com Fı́sica de Partı́culas 68 / 69


Outlook

Outlook

See additional slides with more plots & complementary information

jamkoons@gmail.com Fı́sica de Partı́culas 69 / 69


Backup

Backup

jamkoons@gmail.com Fı́sica de Partı́culas / 69


Backup

C++ constants / literals

I Storage class → Defines the scope (visibility) and lifetime of variables and/or
functions within a C++ program

auto Default storage class


register The variable has a maximum size
Instructs the compiler to keep a local variable in existence during the lifetime of the program
static instead of creating and destroying it each time it comes into and goes out of scope

extern g++ main.cpp support.cpp -o write


mutable A mutable member can be modified by a const memeber function

jamkoons@gmail.com Fı́sica de Partı́culas 69 / 69

Das könnte Ihnen auch gefallen