Print Notes 1
Print Notes 1
- Programming is not limited only to printing simple texts on the screen. In order to go a little
further on and to become able to write programs that perform useful tasks that really save us
work, we need to introduce the concept of variables.
- Fundamental data types are basic types implemented directly by the language that represent the
basic storage units supported natively by most systems, there are different types of variables
(defined with different keywords), for example:
- Numerical integer types - They can store a whole number value, without decimals such as 7 or
1024. The most basic type is int.
- Character types - They can represent a single character, such as 'A' or 'B'. The most basic type is
char, Char values are surrounded by single quotes.
- Floating-point types - They can represent real values, such as 3.14 or 0.01, here we can use either
float or double depending on level of precision.
- Boolean type - The boolean type, known in C++ as bool, stores values with two states: true or
false
- String Type - stores text, such as "Hello World". String values are surrounded by double quotes.
C++ INDENTIFIERS
- All C++ variables must be identified with unique names. These unique names are called
identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum,
totalVolume).
- C++ uses a number of keywords to identify operations and data descriptions; therefore, identifiers
created by a programmer cannot match these keywords. The standard reserved keywords that
cannot be used for programmer created identifiers are:
- alignas, alignof, and, and_eq, asm, auto, bitand, bitor, bool, break, case, catch, char, char16_t,
char32_t, class, compl, const, constexpr, const_cast, continue, decltype, default, delete, do,
double, dynamic_cast, else, enum, explicit, export, extern, false, float,
for, friend, goto, if, inline, int, long, mutable, namespace, new, noexcept, not, not_eq, nullptr,
operator, or, or_eq, private, protected, public, register, reinterpret_cast, return, short, signed,
sizeof, static, static_assert, static_cast, struct, switch, template, this, thread_local, throw, true, try,
typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while, xor,
xor_eq
- Very important reminder: The C++ language is a "case sensitive" language. That means that an
identifier written in capital letters is not equivalent to another one with the same name but written
in small letters. Thus, for example, the RESULT variable is not the same as the result variable or
the Result variable. These are three different identifiers identifying three different variables.
DECLARATION OF VARIABLES
- C++ is a strongly-typed language, and requires every variable to be declared with its type before
its first use. This informs the compiler the size to reserve in memory for the variable and how to
interpret its value. The syntax to declare a new variable in C++ is straightforward: we simply
write the type followed by the variable name (i.e., its identifier). For example:
int a;
float myNumber;
- These are two valid declarations of variables. The first one declares a variable of type int with the
identifier a. The second one declares a variable of type float with the identifier mynumber. Once
declared, the variables a and mynumber
can be used within the rest of their scope in the program.
- To declare more than one variable of the same type, use a comma-separated list:
int x = 5, y = 6, z = 50;
cout << x + y + z;
- You can also assign the same value to multiple variables in one line: int x, y, z; x = y = z = 50;
cout << x + y + z;
INITIALIZATION OF VARIABLES
- It is possible for a variable to have a specific value from the moment it is declared. This is called
the initialization of the variable.
- In C++, there are three ways to initialize variables. They are all equivalent and are reminiscent of
the evolution of the language over the years:
- The first one, known as c-like initialization (because it is inherited from the C language), consists
of appending an equal sign followed by the value to which the variable is initialized: type
identifier = initial_value; or int x = 0;
- A second method, known as constructor initialization (introduced by the C++ language), encloses
the initial value between parentheses (()): type identifier (initial_value); or int x (0);
- Finally, a third method, known as uniform initialization, similar to the above, but using curly
braces ({}) instead of parentheses (this was introduced by the revision of the C++ standard, in
2011): type identifier {initial_value}; or int x {0};
INTRODUCTION TO STRINGS
- Fundamental types represent the most basic types handled by the machines where the code may
run. But one of the major strengths of the C++ language is its rich set of compound types, of
which the fundamental types are mere building blocks.
- An example of compound type is the string class. Variables of this type are able to store
sequences of characters, such as words or sentences. A very useful feature!
- A first difference with fundamental data types is that in order to declare and use objects
(variables) of this type, the program needs to include the header where the type is defined within
the standard library (header <string>):
- Strings can also perform all the other basic operations that fundamental data types can, like being
declared without an initial value and change its value during execution.
BASICS OF C++
STRUCTURE OF A PROGRAM:
Line 1: // my first program in C++
- Two slash signs indicate that the rest of the line is a comment inserted by the
programmer but which has no effect on the behavior of the program. Programmers use
them to include short explanations or observations concerning the code or program. In
this case, it is a brief introductory description of the program.
- This statement has three parts: First, std::cout, which identifies the standard character
output device (usually, this is the computer screen). Second, the insertion operator (<<),
which indicates that what follows is inserted into std::cout. Finally, a sentence within
quotes ("Hello world!"), is the content inserted into the standard output.
- Notice that the statement ends with a semicolon (;). This character marks the end of the
statement. All C++ statements must end with a semicolon character. One of the most
common syntax errors in C++ is forgetting to end a statement with a semicolon.
COMMENTS
- As we already discussed, comments do not affect the operation of the program;
however, they provide an important tool to document directly within the source code what
the program does and how it operates.
- C++ supports two ways of commenting code:
// line comment
- discards everything from where the pair of slash signs (//) are found up to the end of that
same line
/* block comment */
- discards everything between the /* */ , with the possibility of including multiple lines.
- cout is part of the standard library, and all the elements in the standard C++ library are
declared within what is called a namespace: the namespace std.
- Both ways of accessing the elements of the std namespace (explicit qualification and
using declarations) are valid in C++ and produce the exact same behavior. For simplicity,
and to improve readability, the examples in these tutorials will more often use this latter
approach with using declarations, although note that explicit qualification is the only way
to guarantee that name collisions never happen.