0% found this document useful (0 votes)
17 views

Week 2

Nice

Uploaded by

joshualumbo1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Week 2

Nice

Uploaded by

joshualumbo1
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Week 2

Variables and Data Types

Let's imagine that I ask you to remember the number 5, and then I ask you to also memorize the number 2 at the same time. You have
just stored two different values in your memory (5 and 2). Now, if I ask you to add 1 to the first number I said, you should be retaining
the numbers 6 (that is 5+1) and 2 in your memory. Then we could, for example, subtract these values and obtain 4 as result.

The whole process described above is a simile of what a computer can do with two variables. The same process can be expressed in
C++ with the following set of statements:

a = 5;
b = 2;
a = a + 1;
result = a - b;

Obviously, this is a very simple example, since we have only used two small integer values, but consider that your computer can store
millions of numbers like these at the same time and conduct sophisticated mathematical operations with them.

We can now define variable as a portion of memory to store a value.

Each variable needs a name that identifies it and distinguishes it from the others. For example, in the previous code the variable names
were a, b, and result, but we could have called the variables any names we could have come up with, as long as they were valid C++
identifiers.

Identifiers
A valid identifier is a sequence of one or more letters, digits, or underscore characters (_). Spaces, punctuation marks, and symbols
cannot be part of an identifier. In addition, identifiers shall always begin with a letter. They can also begin with an underline character
(_), but such identifiers are -on most cases- considered reserved for compiler-specific keywords or external identifiers, as well as
identifiers containing two successive underscore characters anywhere. In no case can they begin with a digit.

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

Specific compilers may also have additional specific reserved keywords.

Very important: 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 identifiying three different variables.

Fundamental data types

Fundamental data types are basic types implemented directly by the language that represent the basic storage units supported natively
by most systems. They can mainly be classified into:
 Character types: They can represent a single character, such as 'A' or '$'. The most basic type is char, which is a one-byte
character. Other types are also provided for wider characters.
 Numerical integer types: They can store a whole number value, such as 7 or 1024. They exist in a variety of sizes, and can
either be signed or unsigned, depending on whether they support negative values or not.
 Floating-point types: They can represent real values, such as 3.14 or 0.01, with different levels of precision, depending on
which of the three floating-point types is used.
 Boolean type: The boolean type, known in C++ as bool, can only represent one of two states, true or false.

Here is the complete list of fundamental types in C++:


Group Type names* Notes on size / precision
char Exactly one byte in size. At least 8 bits.
char16_t Not smaller than char. At least 16 bits.
Character types
char32_t Not smaller than char16_t. At least 32 bits.
wchar_t Can represent the largest supported character set.
signed char Same size as char. At least 8 bits.
signed short int Not smaller than char. At least 16 bits.
Integer types (signed)
signed int Not smaller than short. At least 16 bits.
signed long int Not smaller than int. At least 32 bits.
signed long long int Not smaller than long. At least 64 bits.
unsigned char
unsigned short int
unsigned int
Integer types (unsigned) unsigned long int
(same size as their signed counterparts)
unsigned long
long int
float
Floating-point types double Precision not less than float
long double Precision not less than double
Boolean type bool
Void type void no storage
Null pointer decltype(nullptr)

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.

If declaring more than one variable of the same type, they can all be declared in a single statement by separating their identifiers with
commas. For example:

int a, b, c;

This declares three variables (a, b and c), all of them of type int, and has exactly the same meaning as:

int a;
int b;
int c;

To see what variable declarations look like in action within a program, let's have a look at the entire C++ code of the example about
your mental memory proposed at the beginning of this chapter:

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
// declaring variables:
int a, b;
int result;

// process:
a = 5;
b = 2;
a = a + 1;
result = a - b;

// print out the result:


cout << result;

// terminate the program:


return 0;
}
Output:
4

Initialization of variables
When the variables in the example above are declared, they have an undetermined value until they are assigned a value for the first
time. But 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;

For example, to declare a variable of type int called x and initialize it to a value of zero from the same moment it is declared, we can
write:

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);


For example:

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


For example:
int x {0};

All three ways of initializing variables are valid and equivalent in C++.

// initialization of variables

#include <iostream>
using namespace std;

int main ()
{
int a=5; // initial value: 5
int b(3); // initial value: 3
int c{2}; // initial value: 2
int result; // initial value undetermined

a = a + b;
result = a - c;
cout << result;

return 0;
}

Output:
6

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

// my first string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string mystring;
mystring = "This is a string";
cout << mystring;
return 0;
}
This is a string
Edit & run on cpp.sh

As you can see in the previous example, strings can be initialized with any valid string literal, just like numerical type variables can be
initialized to any valid numerical literal. As with fundamental types, all initialization formats are valid with strings:

string mystring = "This is a string";


string mystring ("This is a string");
string mystring {"This is a 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:

// my first string
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string mystring;
mystring = "This is the initial string content";
cout << mystring << endl;
mystring = "This is a different string content";
cout << mystring << endl;
return 0;
}
This is the initial string content
This is a different string content

Note: inserting the endl manipulator ends the line (printing a newline character and flushing the stream).

The string class is a compound type. As you can see in the example above, compound types are used in the same way as fundamental
types: the same syntax is used to declare variables and to initialize them.

You might also like