Tutorial How to start programming in C++ (Part 2 of 5)

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

JM Security

Level 29
Verified
Apr 12, 2015
1,831
14,290
#1
Hi everyone!

Previous article: [Tutorial] How to start programming in C++ (Part 1)

Today I'm going to explain the main parts of a software written in C++.

So, let's start with part 2!


The output stream

When it comes to input or output without clarify, reference is made to the default devices: in this case the keyboard and video. If you will need to use different devices you will have to specify it explicitly.

The operating system provides a high level interface to the hardware: the devices are mapped into memory, that is, in practice, a part of the main memory (buffer) as temporary storage of data to and from devices. In this way, for example, output operations may be performed in the same way regardless of the device: will the system that will manage the specific nature of the hardware. The I/O system provides the abstract concept of a stream or channel (the stream). This term refers to a logical device independent of the physical device: who writes the program you will have to take care of data passing through the channel regardless of physical device specifications that are using (the video, magnetic disc player, a printer). Output stream, in the case of the video, it is used in a sequential manner: it can append all the outputs in the channel and the system will print them one after the other.

Program to show different ways of print to video

#include <iostream>

using namespace std;



int main ()

{

"first row" cout < <; /*1*/

"below the line" cout < < < < endl; /*2*/

"new line very long" cout < <

"the line continues on video"

"\n This is printed on a new line"

< < endl; /*3*/



return 0;

}



If you are building, and launching the program to run, you get:

First row after row

New very long line the line continues on video

This is printed on a new line



In 1 you channels (through the insertion operator < <) in the stream cout the string first line. Everything is enclosed in "" (double quote character), as can be seen in the execution, will appear on video as is. IL; closes the statement.

In 2, although it is a new statement, the string, as can be seen in the execution, is seen here at first because the stream is used sequentially. To go to the new row is appended (operator < <) the modifier endl (end-line), which terminates the line does move to the next line in the video.

The statement 3 is divided into multiple lines of listing. Statement terminates, as usual, with ";" It allows you to distribute the text to appear on multiple physical lines. In each he writes a string enclosed by the usual characters: this is ... next row appears that displays new line ... To switch to a new line on video, it was used in this case the control character \n in string to display. The space next to the control character is not required but is introduced only to highlight it. Ultimately to be able to go to the next line you can append to the output stream, endl or put in the string to be printed the control character \n.

Within the string to be printed can be inserted as well as other control codes, all preceded by the escape character (\), here provides a list of those that can be most useful:

\n moves the cursor to the beginning of the next line

move the cursor to the next tab stop \t (tab stop is set for each 8 characters)

' print a tittle

\ "print quotes

The execution of this output statement in the sample program:

...

cout \nBase ":" basic < < < "height:" < < < < < height < < endl;

cout area "Area:" < < < < < < endl;



would, if they had been introduced the values 3 and 7 respectively for the base and height:

Base: 3 height: 7

Area: 21



the first output, as required by the control code will print a line under the last preceding output.


The stream of input

To make the program of calculating the area of a rectangle more general, it allows those who is using it to enter the values of the base and the height; in this way, the algorithm will calculate the area of a any rectangle.

CIN >> base >> height;



Executing this statement causes the system to wait for the user to enter two numbers separated by a space, and that will go to preserve in the specified variables.

In this statement uses the input channel cin and the extraction operator >>. One could interpret the statement as: extract from two input channel data and store them in variables specified. As noted previously, the definition of the input channel is, like that of the output channel, in the iostream library.

Because there is a string of invitation prior to the input statement, what the user will see displayed at run time of the program will be

Base and height value separated by a space: _



Right now expects the input channel has a value to be extracted. If the user types the values 10 and 13 separated by a space and followed by enter

Base and height value separated by a space: 10 13



the data will be assigned, respectively, to the base and height variables.

Program execution, assuming that the user enters the values 10 and 13, will be.

RECTANGLE AREA Calculation



Base and height value separated by a space: 10 13



Base: 10 height: 13

Area: 130



Example inputs are grouped to make evident to the reading that it is the base and the height of the same rectangle: the two input data relate to the dimensions of a rectangle though, physically, they are two values.

You could acquire two values independently:

...

cout "base Value" < <;

CIN >> base;

cout value height: "; < <

CIN >> height;

...



If the encoding was done in this way, the system would have expected, therefore each extraction statement from the stream cin, one followed by enter. The figure would be preserved, as a result, in the specified variable.

If construct and constant declarations

The if construct allows you to encode a selection tree. The syntax of the if statement is:

If (expression)

user's Guide



where the evaluation of expression controls the execution of education: instruction is executed if expression is true.

As an example there is a program that requires the user and, if that number is less than 100, display a message. Due to the simplicity it is proposed directly encoding.

#include <iostream>

using namespace std;



int main ()

{

const int limit = 100; /*1*/

int i;



value to be processed



"introduce an integer" cout < <;

CIN >> i;



check value



If (the < limit)/* 2 */

"number introduced less than" cout < < < < limit < < endl; /*3*/



return 0;

}



The proposed program is the constant 100 that could be used directly in the instructions that involve, but it is more convenient to assign it a symbolic name to use instead of the value. The statement contained in 1 declaring a constant of type int named limit and value 100. In the instructions present in the program, when it involved the constant, uses the name, as shown in the instructions contained in lines 2 and 3.

The difference between the variable declaration and the Declaration of a constant, from the point of view of language syntax expects, in the second case, the const keyword it has been assigned a value. From the point of view of running a variable, declared as a constant, cannot be changed: If the program had some instruction that involves changing the proposed limit, for example, the compiler would generate an error message.

The reason for the use of constant declarations lies in the fact that, in this way, within the program, it appears only the symbolic name. The value appears once at the beginning of the program and, therefore, if there is no need to change this value, for example, by assessing whether the user input does not exceed 150, whatever the length of the program and how many they are constant usages, simply edit the only line of the Declaration and recompile the program in order for this to work with the new settings.

The expression is present in 2 limit < logic that controls the print statement and therefore its nomination can return only one of two Boolean values true or false and C++ correspond to integer values as ones and zeros. It is precisely for this reason that an assignment of the type a = i, is entirely lawful. limit < It evaluates the expression, which evaluates to 1 limit the logic < (true) if i is less than 100 or 0 (false) if i is greater than or equal to 100: the result is an integer that is assigned to the variable a.

A further consequence of Boolean values is that ask whether the value of a is not zero is the same as asking whether the value of a is true, that, in C++, corresponds to the control executed by default (carried out in the absence of different indications), whereby you would also be able to write

...

CIN >> i;

a = i < limit;

If (a)

"number introduced less than" cout < < < < limit < < endl;



The complete syntax of the if statement is as follows:

If (expression)

educational

[else

statement2]

where the evaluation of expression controls the execution of educational and educational statement2 is executed if expression is true if it is false, statement2 is executed.

The following example has previously been omitted the else branch: the fact is perfectly legitimate because it is optional. The square brackets are in complete syntactic form have this meaning.

It is worth noting that, in C++, the comparison for equality between the values of two variables are accessed using the double ==.

Example A:

if (a==b)
cout << "a and b are equals";

Example B:

If (a = b)

cout "value not zero < <;

In the example A we compare the contents of the variable a and variable b: if they are the same prints the target phrase.

In example B you give to the value currently contained in b and occurs if is non-zero, in which case the target phrase is printed.

A further remark should be made about logical operators && (logical AND) and || (Logical OR) that are used to combine multiple conditions. ES.

If (a 10 to 5 && > <)

cout < < "to between 5 and 10";


If (a < 2 || a > 10)

cout < < "a can be < 2 or > 10";

The operator ?

The assignment operator conditioning ? has the following syntax:

Expr1? Expr2: expr3



If Expr1 is true returns Expr2 else expr3 returns.

This operator is used to assign, conditionally, a value to a variable. Doing so can make a program fragment less wasteful and more understandable:

Example A:

if (a>100)
discount=10;
else
discount=5;

Example B:

discount=(a>100 ? 10 : 5);

In both the examples is assigned to the variable discount a value depending on the given condition, except that, in example B, is most clearly visible that this is an assignment. What does not immediately perceptible, until after you read the instructions, in the example A on the construct.

Thanks for reading the part 2 of this tutorial!

Comments are always welcome! :)
 
Last edited: