C++ Tutorial/Language Basics/Variables

Материал из C\C++ эксперт
Версия от 13:30, 25 мая 2010; Admin (обсуждение | вклад) (1 версия: Импорт контента...)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A scoping example

<source lang="cpp">#include <iostream> using std::cout; using std::endl; void useLocal( void ); void useStaticLocal( void ); void useGlobal( void ); int x = 1; int main() {

  int x = 5;
  cout << x << endl;
  {
     int x = 7;
     cout << "local x in main"s inner scope is " << x << endl;
  }
  cout << x << endl;
  useLocal(); 
  useStaticLocal(); 
  useGlobal(); 
  useLocal(); 
  useStaticLocal(); 
  useGlobal(); 
  cout << x << endl;
  return 0;

} void useLocal( void ) {

  int x = 25;
  cout << "local x is " << x << " on entering useLocal" << endl;
  x = x + 20;
  cout << "local x is " << x << " on exiting useLocal" << endl;

} void useStaticLocal( void ) {

  static int x = 50;
  cout << "local static x is " << x << " on entering useStaticLocal" 
     << endl;
  x = x + 20;
  cout << "local static x is " << x << " on exiting useStaticLocal" 
     << endl;

} void useGlobal( void ) {

  cout << "global x is " << x << " on entering useGlobal" << endl;
  x = x + 20;
  cout << "global x is " << x << " on exiting useGlobal" << endl;

}</source>

5
local x in main"s inner scope is 7
5
local x is 25 on entering useLocal
local x is 45 on exiting useLocal
local static x is 50 on entering useStaticLocal
local static x is 70 on exiting useStaticLocal
global x is 1 on entering useGlobal
global x is 21 on exiting useGlobal
local x is 25 on entering useLocal
local x is 45 on exiting useLocal
local static x is 70 on entering useStaticLocal
local static x is 90 on exiting useStaticLocal
global x is 21 on entering useGlobal
global x is 41 on exiting useGlobal
5

Assign value to a variable

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 int length; // this declares a variable 

 length = 7; // this assigns 7 to length 

 cout << "The length is "; 
 cout << length; // This displays 7 

 return 0; 

}</source>

The length is 7"

Comparing data values

<source lang="cpp">#include <iostream> using std::cin; using std::cout; using std::endl; int main() {

 char first = 10;
 char second = 20;
 cout << "The value of the expression first < second is: "
      << (first < second)
      << endl
      << "The value of the expression first == second is: "
      << (first == second)
      << endl;
 return 0;

}</source>

The value of the expression first < second is: 1
The value of the expression first == second is: 0

Dynamic initialization.

<source lang="cpp">#include <iostream> using namespace std;

int main() {

 double radius = 4.0, height = 5.0; 

 // dynamically initializ volume 
 double volume = 3.1416 * radius * radius * height; 

 cout << "Volume is " << volume; 

 return 0; 

}</source>

Volume is 251.328"

Finding maximum and minimum values for data types

<source lang="cpp">#include <limits>

  1. include <iostream>

using std::cout; using std::endl; using std::numeric_limits; int main() {

 cout << "The range for type short is from "
      << numeric_limits<short>::min()
      << " to "
      << numeric_limits<short>::max();
 cout << "The range for type int is from "
      << numeric_limits<int>::min()
      << " to "
      << numeric_limits<int>::max();
 cout << "The range for type long is from "
      << numeric_limits<long>::min()
      << " to "
      << numeric_limits<long>::max();
 cout << "The range for type float is from "
      << numeric_limits<float>::min()
      << " to "
      << numeric_limits<float>::max();
 cout << "The range for type double is from "
      << numeric_limits<double>::min()
      << " to "
      << numeric_limits<double>::max();
 cout << "The range for type long double is from "
      << numeric_limits<long double>::min()
      << " to "
      << numeric_limits<long double>::max();
 cout << endl;
 return 0;

}</source>

The range for type short is from -32768 to 32767The range for type int is from -
2147483648 to 2147483647The range for type long is from -2147483648 to 214748364
7The range for type float is from 1.17549e-038 to 3.40282e+038The range for type
 double is from 2.22507e-308 to 1.79769e+308The range for type long double is fr
om 0 to 1.#INF

Using the unary scope resolution operator

<source lang="cpp">#include <iostream> using std::cout; using std::endl; int n = 7; int main() {

  double n = 10.5;
  cout << "Local double value of n = " << n
     << "\nGlobal int value of n = " << ::n << endl;
  return 0;

}</source>

Local double value of n = 10.5
Global int value of n = 7