"C + + Primer"----about variables and basic types

Source: Internet
Author: User

type is the basis of all programs;

C + + defines several basic types: character type (char and wchar_t), integer (short int long bool), float (float Doubel) and provides a mechanism for customizing data types Class St ruct;;

The standard library uses these mechanisms to define many more complex data types, such as variable-length string vectors, and so on;

An object is an area in memory that has a type, and in other words: The calculation of an lvalue expression produces an object;

--------about Initialization

Initialization is divided into copy initialization and direct initialization

Copy initialization syntax using equals (=), direct initialization is to put the initialization in parentheses;;

int value (1024); Direct initialization

int value = 1024; Replication initialization

"Initialization is not assignment" in C + + initialization refers to the creation of a variable and assigns it an initial value, while the assignment is to erase the current value of the object and replace it with a new value;//So the copy initialization is actually a two-step action, the direct initialization syntax is more flexible and more efficient;

For built-in types, there is not much difference between assignment initialization and direct initialization.

But for objects of class type, some initialization can only be done with direct initialization, and a member function that defines how to initialize is called a constructor, a class can define multiple constructors, and each constructor must accept different numbers or different types of arguments;

Rules for initializing-------variables:

1. Initialization of built-in type variables

Whether the built-in type variable is automatically initialized depends on the position defined by the variable;

Variables defined outside the body of the function are initialized to 0

Variables defined in the function body are not initialized automatically;

2. Initialization of class-type variables

Use an explicit initialization

Using the default constructor

Regardless of where the variable is defined, the default function is used, and some class types do not have a default constructor, and for these types, each definition must provide an explicit initialization, and no initial value is at all impossible to define a variable of this type;

----------declarations and definitions

The definition of a variable is used to allocate storage space for a variable, and you can also specify an initial value for the variable, but the variable has only one definition; (the definition is typically accompanied by an initialization)

A declaration is used to indicate to a program the type and name of a variable. Definition is also a declaration (using extern but not initialized)

extern int pi = 3.1415; Defined

extern int pi; declared but not defined;

----------Const Qualifier;

The Const qualifier provides a way to convert a variable to a constant, or a const modifier to indicate that it cannot be modified;

Default condition:

When a non-const variable is defined in the global scope, it can be accessed throughout the program;

A const variable declared in the global scope is a local variable that defines the object file, which exists only in this file and cannot be accessed by another file

By default, similar to the C language static scope;

* The non-const variable defaults to extern, and the cosnt constant that you want to access in other files must be explicitly specified as extern;

------------references;

A reference is another name of the object, and the reference is mainly used as the formal parameter of the function before the & symbol is defined;

The reference must be initialized with an object of the same type as the reference; int val = 1025;     int &var = val; Ok

References are only different names, but point to the same address, the same object; Objects of the same data type are associated with each other;

-----------typedef

Used to define a synonym for a type; typedef int QINT32; Use Qint32 to represent the int data type;

-----------enumeration; enum

Define a set of selectable values for some properties;

Default: The first enumeration member is assigned a value of 0, and each subsequent enumeration is 1 greater than the previous one;

Enum STAT {input, output, append}; Default input = 0; output = 1; append = 2;

Enum Stat {input =2, output, append}; Other members will be implicitly initialized;

--------struct and CALSS

Class The first Access ID all previous members are private:

The first member of a struct is public;//This is the only difference between class and struc;

"C + + Primer"----about variables and basic types

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.