Skip to main content

Posts

Showing posts from September, 2016

Java Literals

Literals are the most obvious kind of constants. They are used to express particular values within the source code of a program. We have already used some in previous chapters to give specific values to variables or to express messages we wanted our programs to print out, for example, when we wrote:   a = 5; The 5 in this piece of code was a literal constant . Literal constants can be classified into: integer, floating-point, characters, strings, Boolean, pointers, and user-defined literals. Integer Numerals 1 2 3 1776 707 -273 These are numerical constants that identify integer values. Notice that they are not enclosed in quotes or any other special character; they are a simple succession of digits representing a whole number in decimal base; for example, 1776 always represents the value one thousand seven hundred seventy-six . In addition to decimal numbers (those that most of us use every day), C++ allows the use of octal numbers (base 8)

Type deduction: auto and decltype

When a new variable is initialized, the compiler can figure out what the type of the variable is automatically by the initializer. For this, it suffices to use auto as the type specifier for the variable: 1 2 int foo = 0; auto bar = foo; // the same as: int bar = foo; Here, bar is declared as having an auto type; therefore, the type of bar is the type of the value used to initialize it: in this case it uses the type of foo , which is int . Variables that are not initialized can also make use of type deduction with the decltype specifier: 1 2 int foo = 0; decltype (foo) bar; // the same as: int bar; Here, bar is declared as having the same type as foo . auto and decltype are powerful features recently added to the language. But the type deduction features they introduce are meant to be used either when the type cannot be obtained by other means or when using it improves code readability. The two examples above were likely neither of these use

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: 1 2 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 exact

Fundamental data types

The values of variables are stored somewhere in an unspecified location in the computer memory as zeros and ones. Our program does not need to know the exact location where a variable is stored; it can simply refer to it by its name. What the program needs to be aware of is the kind of data stored in the variable. It's not the same to store a simple integer as it is to store a letter or a large floating-point number; even though they are all represented using zeros and ones, they are not interpreted in the same way, and in many cases, they don't occupy the same amount of memory. 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 charac

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_

Comments

As noted above, 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: 1 2 // line comment /* block comment */ The first of them, known as line comment , discards everything from where the pair of slash signs ( // ) are found up to the end of that same line. The second one, known as block comment , discards everything between the /* characters and the first appearance of the */ characters, with the possibility of including multiple lines. Let's add comments to our second program: /* my second program in C++ with more comments */ #include int main () { std::cout << "Hello World! "; // prints Hello World! std::cout << "I'm a C++ program"; // prints I'm a C++ program } Output Hello World! I'm a C++ program If comments are incl