100% de satisfacción garantizada Inmediatamente disponible después del pago Leer en línea o como PDF No estas atado a nada 4,6 TrustPilot
logo-home
Otro

C++ tutorial notes

Puntuación
-
Vendido
-
Páginas
23
Subido en
16-04-2022
Escrito en
2021/2022

BASIC PROGRAM STRUCTURE A C++ program1 is made up of a mainline procedure called main(), and zero or more additional procedures to perform operations within your program. When a program starts execution, it begins by calling the mainline procedure. Source code for a program can be contained in a single file, or split across multiple files. Additional functionality may also be provided via external libraries. The source code is compiled into object files. The object files are then linked together to produce an executable program.

Mostrar más Leer menos
Institución
Grado

Vista previa del contenido

C++ Tutorial


1 BASIC PROGRAM STRUCTURE

A C++ program1 is made up of a mainline procedure called main(), and zero or more additional procedures to perform
operations within your program. When a program starts execution, it begins by calling the mainline procedure.

Source code for a program can be contained in a single file, or split across multiple files. Additional functionality
may also be provided via external libraries. The source code is compiled into object files. The object files are then
linked together to produce an executable program.


2 OPERATORS

Every computer language provides a set of built-in functions or operators which allow you to perform simple
opera-tions such as addition, subtraction, increment, and decrement. C++ provides a useful set of operators to
perform basic arithmetic, operator grouping, indirection, and binary logic. An expression is a combination of
variables, constants, and operators. Every expression has a result. Operators in C++ have precedence associated
with them; this means ex-pressions using operators are not evaluated strictly left to right. Results from the operators
with the highest precedence are computed first. Consider the following simple C++ expression:

6+3*4/2+2


If this were evaluated left to right, the result would be 20. However, since multiplication and division have a higher
precedence than addition in C++, the result returned is 14, computed as follows.

3*4!12
12/2!6
6+6!12
12+2!14
Of course, we can use parentheses to force a result of 20, if that's what we wanted, with the following expression:

(((6+3)*4)/2)+2


Below is a list of the basic operators in C++, along with an explanation of what they do. The operators are group
according to precedence, from highest to lowest.
1
Throughout these notes, I will use C++ conventions for comments (// rather than /* and */), memory allocation (new and delete rather than malloc()
and free()), printing (cout rather than printf), argument passing, and so on. I will not discuss any of the object-oriented aspects of C++, however. This
material is left to the reader, and can be found in any textbook on C++.




1

, Operator Description
() parentheses define the order in which groups of operators should be evaluated
++, -- pre-increment and pre-decrement
! logical NOT
&, * address of, deference
() casting
->, . member selection
*,/,% multiplication, division, integer division
+, - addition, subtraction
<<, >> bitwise shift
<, <=, >, >= less than, less than or equal, greater than or equal, greater than
==, !=, equality, not equal
&, | bitwise AND, bitwise OR
&&, || logical AND, logical OR
++, -- post-increment and post-decrement
=, *=, /=, etc. assignment operators

Note the difference between the pre-increment and post-increment operators. Pre-increment occurs before any part
of an expression is evaluated. Post-increment occurs after the result of the expression has been returned. For
example, what would the output be from the following lines of code?
a = 1;
b = (a++) - 2 * a++;
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";

n = 1;
m = (++n) - 2 * ++n;
cout << "n = " << n << "\n";
cout << "m = " << m << "\n";


If we compiled and ran this code, we would get the results
a = 3
b = -1
n = 3
m = -4



3 CONDITIONALS

C++ also provides a number of built-in conditional statements. This allows us to execute certain parts of our program
based on some condition being either true or false. Unfortunately, in C++ there is no boolean type. A value of 0 is
considered false, any non-zero value is considered true. A well known example of a conditional statement is the if-then-
else conditional. If some expression evaluates to true, then execute one part of our code, else execute a different part.
Below is a list of the conditional statements available in C++, along with examples of how to use them.


2

, 3.1 if-then-else

if-then-else works exactly as you would expect. If some condition is true, the then portion of the conditional is
executed, otherwise the else part of the conditional is executed. The else part of the conditional is optional. If it
doesn't exist and the expression is false, nothing is executed. Consider the following example, which sets a student's
letter grade based on their overall percentage.

if ( n_grade >= 80 ) {
l_grade = 'A';

} else if ( n_grade >= 65 )
{ l_grade = 'B';

} else if ( n_grade >= 50 )
{ l_grade = 'C';

} else {
l_grade = 'F';
}


The braces are optional here, since the then and else code sections consist of only a single statement. If there were
more than one statement, however, the braces would be required.

3.2 for

for is a looping conditional. It executes a set of statements repeatedly a specific number of times, then exits the loop.
The condition here is a value which determines the number of times the loop will be executed. Often, a variable's
value is used for this purpose, as in the following example.

cout << "Please enter a number:\n";
cin >> loop-count;

for( i = 0; i < loop-count; i++ ) {
cout << "Loop pass " << i << "\n";
}


Notice the for statement is made up of three separate parts: an initialization part, a conditional part, and an
interaction part.

for( init-part; condition-part; iterate-part ) {
...
}


The initialization part is executed before looping begins. It is usually used to initialize variable values. In our
example, we use it to set the value of i to 0. The condition part is checked each time the loop begins executing. If
the condition is true, the loop is executed. If it is false, looping ends. In our example, we continue looping until i is
greater than or equal to the value of loop-count. The interaction part is executed each time the loop finishes. It
is usually used to update the value of a variable being used inside the loop. In our example, we increment i by 1
each time through the loop. Thus, i is used to count the number of times the loop has been executed.



3

Escuela, estudio y materia

Grado

Información del documento

Subido en
16 de abril de 2022
Número de páginas
23
Escrito en
2021/2022
Tipo
OTRO
Personaje
Desconocido

Temas

$10.99
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Leer en línea o como PDF
No estas atado a nada

Conoce al vendedor
Seller avatar
asifali

Conoce al vendedor

Seller avatar
asifali ASIF ALI
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
-
Miembro desde
3 año
Número de seguidores
0
Documentos
70
Última venta
-

0.0

0 reseñas

5
0
4
0
3
0
2
0
1
0

Documentos populares

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes