Let's take a look at C + + two. Definition, initialization, and assignment of objects

Source: Internet
Author: User

---restore content starts---

The distance after writing the first article has been for several months, has been trying to write, do not know where to pen.

The first article by a lot of web sites crawled, in fact, I do not exclude being crawled, because this is not a profitable article, but I hope you can last a bleak wind or xstsow name.

Back to the point, I mentioned in the first chapter that main is a main function, int is the data type of the main function, it is necessary to introduce the data type again or in more detail before starting the following content.

The data type includes a collection and a series of operations, and different data types correspond to the same set, for example, int corresponds to an integer between -2^ (32-1) ~ 2^ (32-1)-1, and the corresponding operation is subtraction and so on we are not common operations.

Different data types correspond to different data structures, because data storage needs to occupy a certain amount of space. Computer storage is stored in binary, and the computer is just a follow-up with the user, how much space you give him, how much space he uses. In this way, some people define a variety of data types, some data types correspond to integers, some correspond to real numbers (floating-point numbers), there are corresponding characters (char), some correspond to True and False (Boolean), and so on, and each data is stored in the same way, are stored in a fixed-size binary space, if the binary space size is only 1 bits, then he can only represent 0, 1, that is, can represent two different data, if there are 2 bits, can represent 1, 2, 3, 4, 4 different data. In the actual storage, but also to involve some more advanced things, in this chapter, I will not repeat.

I put an appendix in the previous chapter, with a series of data types in the appendix, and I summon it again.

The Great summon operation!!!!!!

Hello,!!!!!!.

C + + basic data types

Type + meaning + minimum storage (by 2 binary) is as follows:

BOOL Boolean type

Char character 8-bit

wchar_t Wide-character 16-bit

Short 16-bit

unsigned short unsigned 16-bit (unsigned type cannot represent a negative number, but a positive number can represent a greater range)

Long length 32 bit

unsigned long unsigned 32-bit

Long long double-length 64-bit

Unsigned long long unsigned double-long 64 is

Float single-precision floating-point 6 is a valid number (floating-point numbers can be understood as small numbers)

Double dual-precision floating-point 10-bit valid digits

Long double extended precision floating-point 10-bit valid digits

Notice that I'm in blue and bold place, the minimum storage space means that the following types of storage space for the smallest size is so small, but some data types can be larger. In the C + + standard, some data ranges are explicitly limited, while others have only a broad scope requirement. The int we used earlier does not appear in the table, its storage space is usually considered to be 32 bits, but in some cases it becomes 16 bits.

Suppose we need to get a person to enter two numbers less than maxint, and then reverse the sequential output, then we need two variables: int a, int b.

They can be defined in the program in the form of int A; int b; (branch can also) or int a, b;

The difference between the first and the second is that the first is used; The two statements are separated, so the B definition must be re-declared to be of type int. The second is used to split two variables, which is still a statement, so no additional declarations are required. In other words, if we want to define multiple variables of the same type, we can define them separately and use them in the same statement, which is useful for the most basic data types, you can define a double A, B can also define char a, B.

  

The definition of a variable can be defined both outside the function as

int A, B; int Main () {    return0;}

can also be defined in the function (not necessarily the main function) inside

int Main () {    int  A, b;     return 0 ;}

Defined outside the function, which we call global variables, he can be used in any function, defined inside the function, we call local variables, he can only be used within the function. A global variable is usually automatically initialized to 0, and local variables are initialized by ourselves.

So, here's the question, how to initialize it.

Still provides two methods, the first one is to define a variable, a single statement to initialize the int A; A = 0;

The second way is to define one side of the initialization, int a = 0;

Both are correct and apply to all basic types, but the second one has a problem to be aware of.

int A, b = 0; Only B is initialized to 0, if you want to initialize two, so write int a = 0, b = 0;

Initializing the data is a good habit, but not all data needs to be initialized, such as a data that needs to be read, which is obviously not required to be initialized.

There is another way of initializing, which we call "constructor syntax (constructor syntax)"

int a (0);

Then you find that the programmer is sick of the place, initialize the initialization, but also have to do two ways, and the second kind of a high-end name, so really good?

In fact, using = to initialize is the lineage of C's language habit, if a type is not a built-in basic type, or the variable actually contains more than one data, then this method is not practical.

string s = "World"

That's fine, of course, but if we're going to assign a value to the complex number in the standard library, it's not that simple, complex. The plural needs two parts, the real part and the imaginary part, and he can certainly be expressed as ni+m, but in the procedure we prefer to record his real and imaginary parts separately.

  

#include <complex>complex<double> A (07);

That angle bracket looks like an eyesore, but he's important, and he shows that complex is a template class, which can be used for many data types, but we have to specify his type when it's formally used,<double> Indicates that the complex is a double class.

The complex number must be represented by a floating-point number, but it does not limit which floating-point number, so we take a double.

At the end of the day, when we started to talk about STL, we were exposed to many styles of template classes, and then we could understand his usage.

Another talk about char (character) and bool (Boolean) data, char represents the character, in the program we usually enclose the character in parentheses, such as ' R ', why do that, for example.

    Char ' R ' ;     char a = R;

There are two kinds of r one is a single quote, one is not, there is no single quotation mark representation of the variable, there is a single quotation mark representation of the character, if we do not make such a distinction, then in the execution of the following char a = R, a should be equal to r this variable or r this character? To solve this problem, we usually enclose characters in single quotation marks.

There is also a data type called string (String), as the name implies, he just put a bunch of strings up, and his assignment and characters are different, that is, the string is usually enclosed in double quotation marks, rather than single quotation marks.

There are some wonderful things in the characters that Char represents, they are escape characters (Eacape sequence), they are used to denote something that cannot be directly expressed, such as

' \ n ' line break

' \ t ' tab

' \ ' single quote

' \ s ' null character

' \ ' double quotation marks

' \ \ ' backslash.

And then if you want to output a newline, you're going to cout << ' \ n ';

Then the bool type, the boolean corresponding to the value of two, true and False,true is true, false means false, so far, he is not useful, but in the fourth chapter, I will introduce him in detail.

Finally, a thing called a constant (const), a constant relative to a variable, his value is set at the beginning, and will never change. The way he defines it is

const int a = 100;

After you have defined solid A, you can no longer modify the value of a, which means you can no longer execute an assignment statement for a.

  

Let's take a look at C + + two. Object definition, initialization, and assignment

Related Article

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.