Taste in the Classics: Chapter II C + + basic object, type and value (top)

Source: Internet
Author: User

Abstract: Original creation Place: http://www.cnblogs.com/Alandre/sediment brick slurry Carpenter hope reprint, keep abstract, thank you!

self-discipline, is to take a positive and active attitude, to solve the pain of life ~

In the previous chapter, we talked about Hello,world's life. Below we savor some of the basics, starting with a simple case, understanding the concepts of objects, types, and so on. I like to explain the example, let everyone listen to the taste, not boring ~

2.1 Example of a simple greeting

The most common is Keyborad input (keyboard input). From the output of HELLOWORLDD, we also want to get the user's input. Typically, a program that is in effect produces output to its input. For example, enter your name, output

A word of greeting. Sometimes it's around, like my favorite Apple phone. Human-Computer Interaction is so simple ~

Figure 2.1.1 Human-computer interaction (. NET)

Back to the technical point, in order to read the user input data, we need to place in the computer memory somewhere to read the required content. This is the object (Class). The object is what, when an object is a certain type of memory area. Where the type determines the information for the object. And a named object, called an instance of the object (also called a variable). To make a metaphor, one sees the object everyone thinks of is the person, but here the human is equal to the object. If you think of a specific object, it's a variable.

Let's look at an example of a simple greeting:

#include <iostream> #include <string>using namespace Std;int main () {string your_name;cin >> your_name ; cout << "Hello ~" << your_name << Endl;}

From the code we can see that this program is very simple. When you enter your name, like my "Jeff_lee". The output is as follows:

Figure 2.1.2 Display Greeting Program Interface

It is worth noting that my fisrt name and last name cannot be separated from the space. The reason is simple:

cin>>The operator reads the data according to the type of the subsequent variable. Enter end condition: Encounter Enter, Space, TAB key.

Analysis of just that small program (believe: Small program also has learning place):

Cin >> Your_Name;

This will save the memory area of a string and name the area your_name. The point is that the computer allocates memory space for this variable. :

Figure 2.1.3 Memory Analysis

cout << "Hello ~" << your_name << Endl;

The following sentence also says that it will put that memory into the value of the object, depending on the type of the variable. There is data in the empty memory area above.

Figure 2.1.4 Memory Analysis

2.2 Variables and types

As we said above, the location where the computer's memory stores data is called an object. We need to use a name to access it, so that this object becomes a variable. The variable has her specific type. The basic types are listed below:

    • bool Boolean type
    • Char character 8-bit
    • wchar_t Wide-character 16-bit
    • Short 16-bit
    • int shaping 16 bits
    • Long length 32 bit
    • Float single-precision floating-point 6 is a valid number
    • Double dual-precision floating-point 10-bit valid digits
    • Long double

Let's look at the next interesting example:

#include <iostream> #include <string>using namespace Std;int main () {string Your_name;int your_age;cin > > your_name >> your_age;cout << ' hi!~ ' << your_name << ' is ' << your_age << ' year s old ";}

If you try to enter "Jeff Lee 21", press ENTER. You'll be amazed why not the "Jeff Lee is years-old." But

Figure 2.2 Display of interesting examples

Here we focus on two points, again emphasizing cin>> the operator is reading data based on the type of the subsequent variable. Enter end condition: Encounter Enter, Space, TAB key. Second, string-type "Lee" is not read by the int type age. It's a tiger. It's going to be random numbers. Why? Because age is not initialized, when you do, you get garbage values for a portion of memory. For example, the above -858993460. You can think, if you run the above program again, enter the same, age will change ~

Operators >> Related Materials: http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/

2.3 Arithmetic

Operation, what is an operation? The essence of an operation is actually a set of mappings. The initial procedure is basically for the operation of the birth. This small piece seems to come to our favorite field of mathematics.

Figure 2.3 Earlier computers

However, many want to keep in mind the details of mathematics. Like what:

String str = "Jefflee"-"Lee";

The compiler rejects the subtraction of the string. But the compiler was stupid, she didn't know some for example, age is greater than 0;

int age =-99;

It's ridiculous in your opinion, it's really right there.

So, the operation wants us to conform to the compiler rules and to define our own rules in conjunction with the problem.

Summary

(This is the second chapter for the next article to come together to summarize.) )

If the above articles or links are helpful to you, don't forget to click the "Like a" button on the article button or in the bottom right corner of the page. You can also click on the right side of the page "share" hover button oh, let more people read this article

Here is the PDF, to be downloaded please open: (http://files.cnblogs.com/Alandre/cplusplusV1.pdf)

Taste in the Classics: Chapter II C + + basic object, type and value (top)

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.