C++ Tutorial № 2 : Variables. Types. Boolean type.
Submitted by Bright777 on Saturday, February 15, 2014 - 09:04.
Hi everyone. It is Bright77 and today we are going to talk about variables: what is variable, how we should declare it and how to operate with them.
First of all we need to give a definition of term “variable”. You can say that variable it is thing that should change. It is quite right, but… So, in C++ you can imagine variable like a special container to store the information we need in the current program in the current time. Variables can store different information: from integer values to sentences and texts. As a conclusion variables can be different types.
Types of variables
The main types of variables in C++ are :
Boolean type
Character type
Integer types
Floating-point types
Also the user can define such types as “enum” type, which is used to enumerate the specific set of values.
Among them user can use such types as:
Pointer types
Array types
Reference types
Data structures and classes
These types we can separate in some groups of types:
Integral types – in this set we can put boolean type, character type and integer type. Combining integral types and floating-point types we get group called arithmetic types. Enumerations and classes are in the group called user-defined types.
So let`s get started from Boolean type.
Boolean type
This type represents the result of logical operations. It can have only two values true or false. Declaration below:
bool c;//declaring variable of type bool without the value
bool a =true;//boolean variable with value "true"
Also there is a little trick in C++ - if you declare boolean variable with non-zero int value it become "true", with zero value - "false". Code below shows it.
bool x =2;//x become "true"
bool y =0;//y become "false"
So, thats all for today. In the next tutorial we will talk about other types such as character type, integer types, floating-point types. If any questions - post them. See you later :)