C++primer4 Chapter2 variables and basic types 2.3

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.