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:
  1. int number_of_lifes= 3;
  2. cout<<"number of your lifes is "<<number_of_lifes<<endl;
  3.  
out:
  1. umber of your lifes is 3
Now lets try to change that variable program:
  1. int number_of_lifes= 3;
  2. cout<<"number of your lifes is "<<number_of_lifes<<endl;
  3. number_of_lifes= 5;
  4. cout<<"number of your lifes is "<<number_of_lifes<<endl;
  5.  
out:
  1. umber of your lifes is 3
  2. number of your lifes is 5
OK, now lets try program with few variables, and try some numeric operations: program:
  1. int a=1;
  2. int b=2;
  3. int c=3;
  4. cout<<"c is "<<c<<endl;
  5. c= a+b;
  6. cout<<"c is "<<c<<endl;
  7. c= 2*b;
  8. cout<<"c is "<<c<<endl;
  9.  
out:
  1. c is 3
  2. c is 3
  3. c is 4
  4.  
You can even use variable to callculate it new value. In mathematica it have no sense, but in programing it have program:
  1. int a=2;
  2. cout<<"a is "<<a<<endl;
  3. a= a+1;
  4. cout<<"a is "<<a<<endl;
  5.  
out:
  1. a is 2
  2. a 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 " ").
  1. char c= 'c';
Working example:
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. bool b= 0;
  8. int i= 123;
  9. float f=3.14;
  10. double d= 3.14159265358979323846;
  11. cout<<"b have logic value "<<b<<endl;
  12. cout<<"i is "<<i<<endl;
  13. cout<<"f approximating value is "<<f<<endl;
  14. cout<<"d approximating value is "<<d<<endl;
  15. return 0;
  16. }
  17.  
output:
  1. b have logic value 0
  2. i is 123
  3. f approximating value is 3.14
  4. d approximating value is 3.14159
  5.  

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 にしか見当たりません

Leave a Reply

  ( Ctrl+Enter )