2010
10-17

C++ lesson 2 – variables

In that lesson I will describe what are variables.

Numeric variable int

At the begin let speak about variable type int. That variable is a number. And that number can be changed during working of program. For example if we are creating computer game we can have variable called number_of_lifes. Let say that at begin that variable will be equal to 3, and during game it can be changed (Player can lost or can get more lifes thanks to geting high score). program:
`int number_of_lifes= 3;cout<<"number of your lifes is "<<number_of_lifes<<endl; `
out:
`umber of your lifes is 3`
Now lets try to change that variable program:
`int number_of_lifes= 3;cout<<"number of your lifes is "<<number_of_lifes<<endl;number_of_lifes= 5;cout<<"number of your lifes is "<<number_of_lifes<<endl; `
out:
`umber of your lifes is 3number of your lifes is 5`
OK, now lets try program with few variables, and try some numeric operations: program:
`int a=1;int b=2;int c=3;cout<<"c is "<<c<<endl;c= a+b;cout<<"c is "<<c<<endl;c= 2*b;cout<<"c is "<<c<<endl; `
out:
`c is 3c is 3c is 4 `
You can even use variable to callculate it new value. In mathematica it have no sense, but in programing it have program:
`int a=2;cout<<"a is "<<a<<endl;a= a+1;cout<<"a is "<<a<<endl; `
out:
`a is 2a is 3`

Other types of variables

here is list of standard C++ variables:
 size min max description bool 1B 0 1 true or false char 1B -128 127 character, but can be also interpretated as number int 4B -2147483648 2147483647 numeric variable float 4B - - Floating point number. double 8B - - Floating point number.
char is one sinlge character, and if you want to set it value you need to set it's value between ' ' (not " ").
`char c= 'c';`
Working example:
`#include <iostream>#include <cstdlib>using namespace std; int main(){    bool b= 0;    int i= 123;    float f=3.14;    double d= 3.14159265358979323846;    cout<<"b have logic value "<<b<<endl;    cout<<"i is "<<i<<endl;    cout<<"f approximating value is "<<f<<endl;    cout<<"d approximating value is "<<d<<endl;    return 0;} `
output:
`b have logic value 0i is 123f approximating value is 3.14d approximating value is 3.14159 `

3 Coments.

1. Boldy says:

Hola, No estб seguro de que esto es verdad:), pero gracias a un cargo.

Boldy

• Emma says:

ANSI ISO/IEC 14882:2003(E)12.3.1 Conversion by conturtcsorA conturtcsor declared without the function-specifier explicit that can be called with a single parameter specifies a conversion from the type of its first parameter to the type of its class. Such a conturtcsor is called a converting conturtcsor. class X {// …public:X(int);X(const char*, int =0);};An explicit conturtcsor constructs objects just like non-explicit conturtcsors, but does so only where the direct-initialization syntax (8.5) or where casts (5.2.9, 5.4) are explicitly used. A default conturtcsor may be an explicit conturtcsor; such a conturtcsor will be used to perform default-initialization or value initialization(8.5).и далее по тексту. Это ответ на вопрос “А по какому праву?”. По тому какой синтаксис был использован в выражении (sss xxx = 7;) компилятор определяет какой конструктор должен быть вызван. Про неявное преобразование типов рассказывать думаю будет лишним.

• Gavin says:

costenxpr が type specifier であるとは言っているわけではありません むしろ逆のことを言っているつもりです costenxpr は型に影響しないことにより 関数に対する costenxpr 修飾が不可能となる関数ポインタ経由の関数呼び出しは costenxpr function に含めることはできない という指摘をしています costenxpr int (*table[])(void) における costenxpr は table の宣言に影響するのであって それが自動的にポインタの指す先に波及したりはしません static int (*table[])(void) としても table が static になるだけで ポインタの指す関数に static が波及するわけではないのと同様です 関数が costenxpr function となる costenxpr function から呼び出せる関数となる 条件は 7.1.5 p2 にしか見当たりません