2.3 Variables
Exercise2.11
#include <iostream>
using namespace Std;
int main ()
{
cout<< "Enter base and exponent:";
int base,exp;
cin>>base>>exp;
int result=1;
for (int cnc=0;cnc!=exp;++cnc)
Result*=base;
cout<<base<< "raised to the power of" <<exp<< ":" <<result<<endl;
return 0;
}
Variable initialization rules:
Initialization of built-in type variables, variables defined outside the function body are initialized to 0, and built-in type variables defined in the function body are not automatically initialized.
Initializes a class-type variable by defining one or more constructors to control the initialization of the class object. Class can also be initialized with a default constructor, such as a string class that defines a default constructor to initialize a string variable to be an empty strings.
Exercise2.17
Both the variable global_str and the variable local_str initial value are empty strings, and the variable global_int initial value is 0,local_int without an initial value.
Exercise2.18
The first is the declaration, the second is the definition, and the third is the definition, but it can only appear outside the function (that is, name is a global variable).
Exercise2.19
100
Exercise2.20
100 45
Exercise2.21
Illegal, I has a statement scope and can only be used in a for statement.
C++primer4 Chapter2 variables and basic types 2.3