Saturday, 22 June 2013

Standard Input (cin)

The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the
overloaded operator of extraction (>>) on the cin stream. The operator must be followed by the variable that will
store the data that is going to be extracted from the stream. For example:

 

The first statement declares a variable of type int called age, and the second one waits for an input from cin(the
keyboard) in order to store it in this integer variable.
cin can only process the input from the keyboard once the RETURN key has been pressed. Therefore, even if you
request a single character, the extraction from cin will not process the input until the user presses RETURN after the character has been introduced.
You must always consider the type of the variable that you are using as a container with cin extractions. If you
request an integer you will get an integer, if you request a character you will get a character and if you request a
string of characters you will get a string of characters.


The user of a program may be one of the factors that generate errors even in the simplest programs that use cin
(like the one we have just seen). Since if you request an integer value and the user introduces a name(which
generally is a string of characters), the result may cause your program to mis operate since it is not what we were
expecting from the user. So when you use the data input provided by cin extractions you will have to trust that
the user of your program will be cooperative and that he/she will not introduce his/her name or something similar
when an integer value is requested. A little ahead,when we see the string stream class we will see a possible
solution for the errors that can be caused by this type of user input.
You can also use cin to request more than one datum input from the user:


is equivalent to:


In both cases the user must give two data, one for variable a and another one for variable b that may be separated
by any valid blank separator: a space, a tab character or a newline.








Saturday, 19 January 2013

Standard Output (cout)

By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout.
cout is used in conjunction with the insertion operator, which is written as <<(two "less than" signs).



The <<operator inserts the data that follows it into the stream preceding it. In the examples above it inserted the

constant string Output sentence, the numerical constant 120and variable x into the standard output stream cout.
Notice that the sentence in the first instruction is enclosed between double quotes (") because it is a constant
string of characters. Whenever we want to use constant strings of characters we must enclose them between
double quotes (") so that they can be clearly distinguished from variable names. For example, these two sentences
have very different results:



The insertion operator (<<) may be used more than once in a single statement:




This last statement would print the message Hello, I am a C++ statement on the screen. The utility of repeating

the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or
more than one variable:



If we assume the age variable to contain the value 24and the zip code variable to contain 90064the output of the

previous statement would be:



It is important to notice that cout does not add a line break after its output unless we explicitly indicate it,

therefore, the following statements:



will be shown on the screen one following the other without any line break between them:


This is a sentence.This is another sentence.


even though we had written them in two different insertions into cout. In order to perform a line break on the

output we must explicitly insert a new-line character into cout. In C++ a new-line character can be specified as \n
(backslash, n):



This produces the following output:

First sentence.
Second sentence.
Third sentence.
Additionally, to add a new-line, you may also use the endl manipulator. For example:



would print out:

First sentence.
Second sentence.
The end l manipulator produces a newline character, exactly as the insertion of '\n'does, but it also has an
additional behavior when it is used with buffered streams: the buffer is flushed. Anyway, cout will be an
unbuffered stream in most cases, so you can generally use both the \nescape character and the end l manipulator
in order to specify a new line without any difference in its behavior.

Friday, 18 January 2013

Basic Input/Output

Until now, the example programs of previous sections provided very little interaction with the user, if any at all.
Using the standard input and output library, we will be able to interact with the user by printing messages on the
screen and getting the user's input from the keyboard.
C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such
as the screen or the keyboard. A stream is an object where a program can either insert or extract characters
to/from it. We do not really need to care about many specifications about the physical media associated with the
stream - we only need to know it will accept or provide characters sequentially.
The standard C++ library includes the header file iostream , where the standard input and output stream objects
are declared.

Tuesday, 8 January 2013

Precedence of operators

When writing complex expressions with several operands, we may have some doubts about which operand is
evaluated first and which later. For example, in this expression:



we may doubt if it really means:



The correct answer is the first of the two expressions, with a result of 6. There is an established order with the
priority of each operator, and not only the arithmetic ones (those whose preference come from mathematics) but
for all the operators which can appear in C++. From greatest to lowest priority, the priority order isas follows:




Grouping defines the precedence order in which operators are evaluated in the case that there are several
operators of the same level in an expression.
All these precedence levels for operators can be manipulated or become more legible by removing possible
ambiguities using parentheses signs (and ), as in this example:




might be written either as:


or

depending on the operation that we want to perform.
So if you want to write complicated expressions and you are not completely sure of the precedence levels, always
include parentheses. It will also become a code easier to read.
















Bitwise Operators ( &, |, ^, ~, <<, >> )

Bitwise operators modify variables considering the bit patterns that represent the values they store.


Explicit type casting operator

Type casting operators allow you to convert a datum of a given type to another. There are several ways to do this
in C++. The simplest one, which has been inherited from the C language, is to precede the expression to be
converted by the new type enclosed between parentheses (()):



The previous code converts the float number 3.14to an integer value (3), the remainder is lost. Here, the
typecasting operator was (int). Another way to do the same thing in C++ is using the functional notation:
preceding the expression to be converted by the type and enclosing the expression between parentheses:



Both ways of type casting are valid in C++.

size of ()
This operator accepts one parameter, which can be either a type or a variable itself and returns the size in bytes of
that type or object:


This will assign the value 1to a because char is a one-byte long type.
The value returned by size of is a constant, so it is always determined before program execution.


Other operators
Later in these tutorials, we will see a few more operators, like the ones referring to pointers or the specifics for
object-oriented programming. Each one is treated in its respective section.

Conditional operator ( ? )

The conditional operator evaluates an expression returning a value if that expression is true and a different one if
the expression is evaluated as false. Its format is: 

condition ? result1 : result2

If condition is true the expression will return result1, if it is not it will return result2.

 
In this example a was 2 and b was 7, so the expression being evaluated (a>b) was not true, thus the first value
specified after the question mark was discarded in favor of the second value (the one after the colon)which was b,
with a value of 7.


Comma operator ( , )

The comma operator (,) is used to separate two or more expressions that are included where only one expression
is expected. When the set of expressions has to be evaluated for a value, only the rightmost expression is
considered.
For example, the following code: 


Would first assign the value 3to b, and then assign b+2to variable a. So, at the end, variable a would contain the
value 5while variable b would contain value 3.

Saturday, 5 January 2013

Logical operators ( !, &&, || )

The Operator !is the C++ operator to perform the Boolean operation NOT, it has only one operand, located at its
right, and the only thing that it does is to inverse the value of it, producing false if its operand is true and true if its
operand is false. Basically, it returns the opposite Boolean value of evaluating its operand. For example: 




The logical operators &&and ||are used when evaluating two expressions to obtain a single relational result. The
operator &&corresponds with Boolean logical operation AND. This operation results true if both its two operands
are true, and false otherwise. The following panel shows the result of operator &&evaluating the expression a &&
b:
&& OPERATOR




The operator ||corresponds with Boolean logical operation OR. This operation results true if either one of its two
operands is true, thus being false only when both operands are false themselves. Here are the possible results of a
|| b: 



|| OPERATOR



For example: