BEFORE: C++ Declaration of variables

In C++ every name and every expression has a type that determines the operations that may be performed on it. For example, the declaration:

…..

{

intinch;…..

…..

}

…..

…..

specifies that** inch** is of type **int**; that is, inch is an integer variable.

C++ offers a variety of fundamental types, which correspond directly to hardware facilities.

For example:

**bool** // Boolean, possible values are true and false

**char** // character, for example, ’a’, ’z’, and ’9’

**int **// integer, for example, 1, 42, and 1066

**double** // double-precision floating-point number, for example, 3.14 and 299793.0

Each of the fundamental types has a fixed size that determines the range of values that can be stored in them (for integers) or the precision and range of those values (for floating point numbers). A char variable is of the natural size to hold a character on a given machine (typically an 8-bit byte), and the sizes of other types are quoted in multiples of the size of a char. The size of a type is implementation defined (i.e., it can vary among different machines) and can be obtained by the **sizeof** operator; for example **sizeof(char)** equals 1 and** sizeof(int) **is often 4. We can represent sizes graphically:

The arithmetic operators can be used for appropriate combinations of these types:

x+y // plus

+x // unar y plus

x−y // minus

−x // unar y minus

x∗y // multiply

x/y // divide

x%y // remainder (modulus) for integers

So can the comparison operators:

x==y // equal

x!=y // not equal

x<y // less than

x>y // greater than

x<=y // less than or equal

x>=y // greater than or equal

In assignments and in arithmetic operations, C++ performs all meaningful conversions between the basic types so that they can be mixed freely, as follows:

….

….

voidsome_function() // function that doesn’t return a value{

doubled = 2.2; // initialize floating-point number

inti = 7; // initialize integerd = d+i; // assign sum to d

i = d∗i; // assign product to i (truncating the double to an int)

}

…..

…..

Note that **=** is the assignment operator and **==** tests equality.

C++ offers a variety of notations for expressing initialization, such as the **= **used above, and a universal form based on curly brace delimited initializer lists, as follows:

**double** d1 = 2.3;

**double** d2 {2.3};

**complex** z = 1; // a complex number with double-precision floating-point scalars

**complex** z2 {d1,d2};

**complex** z3 = {1,2}; // the = is optional with { … }

**vector** v {1,2,3,4,5,6}; // a vector of ints

A constant cannot be left uninitialized and a variable should only be left uninitialized in extremely rare circumstances. Don’t introduce a name until you have a suitable value for it.

When defining a variable, you don’t actually need to state its type explicitly when it can be deduced from the initializer:

**auto **b = true; // a bool

**auto** ch = ’x’; // a char

**auto **i = 123; // an int

**auto** d = 1.2; // a double

**auto** z = sqrt(y); // z has the type of whatever sqrt(y) returns

With auto, we use the** =** syntax because there is no type conversion involved that might cause problems.

In addition to the conventional arithmetic and logical operators, C++ offers more specific operations for modifying a variable:

x+=y // x = x+y

++x // increment: x = x+1

x−=y // x = x-y

−−x // decrement: x = x-1

x∗=y // scaling: x = x*y

x/=y // scaling: x = x/y

x%=y // x = x%y

These operators are concise, convenient, and very frequently used.

C++ supports two notions of immutability:

• **const:** meaning roughly ‘‘I promise not to change this value’’. This is used primarily to specify interfaces, so that data can be passed to functions without fear of it being modified. The compiler enforces the promise made by **const**.

• **constexpr**: meaning roughly ‘‘to be evaluated at compile time’’. This is used primarily to specify constants, to allow placement of data in memory where it is unlikely to be corrupted, and for performance.

For example:

doub le sum(const vector&); // sum will not modify its argument

const int dmv = 17; // dmv is a named constant

conste xpr double max1 = 1.4∗square(dmv); // OK if square(17) is a constant expression

const double max2 = 1.4∗square(dmv); // OK, may be evaluated at run time

v ector v { 1.2, 3.4, 4.5 }; // v is not a constant

const double s1 = sum(v); // OK: evaluated at run time

conste xpr double s2 = sum(v); // error : sum(v) not constant expression

Next example will illustrate what we have discussed so far:

double square(double x) //eleva al cuadrado un número tipo double-precision // //floating point{

return x * x;

}

voidprint_square(double x){

std::cout << “the square of” << x << “is” << square(x) << ‘\n’;

}

intmain(){

print_square(1.234); //imprime el cuadrado de 1.234

print_square(5.555); // imprime el cuadrado de 5.555

}

Output:

The **std::** specifies that the name **cout** is to be found in the standard-library namespace. Without **std:: **operator, the statement ” cout << “the square of” << x << “is” << square(x) << ‘\n’;” has no sense.

A ‘‘return type’’ **void** indicates that a function does not return a value.

Numeric Data.

C++ contains intrinsic data types to store **numeric values** in your application code. It’s important to remember that these values are binary-based and not as flexible as their base 10 counterparts. For example, in mathematical terms of a base 10 integer, the definition is a value that is negative infinity to positive infinity whole numbers. Modern computers still cannot represent numbers these large. Take as an example the int type in the Numeric Data Types table. The range does not exceed 3 billion in either direction.

**Note: **I recommend Visual Studio IDE (Interface Development Environment) , and after Introducción a C++ en Visual Studio.

BEFORE: C++ Declaration of variables

Source:

Review by: Larry Francis Obando – Technical Specialist – **Educational Content Writer.**

Escuela de Ingeniería Eléctrica de la Universidad Central de Venezuela, Caracas.

Escuela de Ingeniería Electrónica de la Universidad Simón Bolívar, Valle de Sartenejas.

Escuela de Turismo de la Universidad Simón Bolívar, Núcleo Litoral.

Contact: Caracas, Quito, Guayaquil, Cuenca – Telf. 00593998524011

WhatsApp: +593998524011

email: dademuchconnection@gmail.com