C ++ Primer Chapter 1 Quick Start notes

Source: Internet
Author: User

 

C ++ Primer Chapter 1 Quick Start

 

Many people will take notes when learning this book! I think this is a good practice. I can also consolidate my learning knowledge, find problems in later learning, and then I can come back to modify and supplement it.

I read the first chapter in the library and asked me if I felt bored. I said I felt great! I don't plan to finish it quickly. I want to master every knowledge point, so I will see it very slowly at the beginning, and I am not in a hurry, just as little money said. It is not difficult to read this book because of the C ++ Foundation. After reading this book, I find that it is not suitable for beginners to read it. It is best to have a basic foundation, I started to write the notes for this chapter.

This chapter introduces most of the basic elements of C ++: built-in types, library types, class types, variables, expressions, statements, and functions. This chapter mainly serves as a buffer, and the subsequent chapters will slowly clarify the topic of this chapter.

Section 1.1-write a simple C ++ Program

A c ++ program contains one or more functions, one of which must be named main.

A function is composed of a sequence of statements that execute the function.

The Return Value of the main function must be int type, which indicates an integer. The int type is a built-in type, that is, it is defined by the C ++ language.

In C ++, most statements use semicolons as the end mark. (Easy to ignore)

When a return statement carries a value, this value is the return value of the function.

The return value type must be the same as the return type of the function.

-- Compile and execute programs

The program in this book seems to call the GUN compiler. I have been using Visual C ++ 6.0, which is an old compiler. Recently I started to use the Visual Studio series compiler, not very easy to use, a little sad!

-- Program source file naming rules

The source file name consists of the file name and the file suffix.

The extension form of the visual Studio compiler is (. cpp)

 

Section 1.2 -- Initial shard input/output

This function is provided by the standard library (iostream library)

Istream and ostream, indicating the input stream and output stream respectively

Standard Input and Output objects are cin and cout respectively. The standard library also defines the other two ostream objects named cerr and clog respectively;

The textbook code is as follows:

# Include <iostream>

Int main ()

{

Std: cout <"Enter two number:" <std: endl;

Int v1, v2;

Std: cin> v1> v2;

Std: cout <"The sum of" <v1 <"and" <v2 <"is" <v1 + v2 <std: endl;

Return 0;

}

The first line of the program is a preprocessing indication. the name in the angle brackets is a header file.

1. Write to stream

Format: (std: cout); <is the output operator.

 

An endl is a special value called an operator that has a line break effect and refreshes the buffer associated with the device.

2. Use the name in the standard library

Namespace std: the output form of this book is a little different from that of Tan Berner's C ++ programming. Tan Berner started with the program

(Using namespace std;), and then the output will have cout directly. This book uses the namespace std as the prefix and uses the scope OPERATOR ::.

 

3. Read into Stream

Format: (std: cin);> is the input operator.

 

4. Complete the program

Is the output result;

Note: When writing a C ++ program, most spaces can be replaced by line breaks. But there are two exceptions:

1. spaces in the string nominal value cannot be replaced by line breaks ("").

2. space characters (# include "") are not allowed in the pre-processing indicator "").

 

1.3 -- about comments

A single line comment starts with a double slash (//), and another separator, annotation pair (/**/). This annotation starts, end.

Comments are generally used for multi-line interpretation, while double slash comments are often used for marking half or single rows. (Annotation pairs cannot be nested)

 

1.4 -- Control Structure

While statement

Code:

// Jump out of the loop when val> 10

While (val <= 10 ){

Sum + = val;

++ Val;

}

From the code above, we can see that while is executed repeatedly through the test condition and the related expression Statement (while_body_statement) until the condition is false.

The condition is an evaluable expression that can test the result. If the result is not 0, the condition is true and the value is 0, and the condition is false.

For statement

Code:

For (int val = 1; val <= 10; ++ val)

 

Sum + = val;

 

The preceding Code consists of the for statement header and for statement body.

The for statement header consists of three parts: An initialization statement, a condition, and an expression. For statement

Execution sequence:

1. Initialization statement

2. Cyclic Conditions

3. Statements

4. Value-Added cyclic Variables

If statement

General format:

If (expression) Statement 1

Else Statement 2

 

1.5 class Introduction

Three issues need to be clarified when using classes

1. What is the class name?

2. Where is it defined?

3. What operations does it support?

 

Sales_item class

1. operations on the Sales_item object

Each class defines a type with the same type name as the class name.

Defines variables of the class type. When you write down Sales_item;

It indicates that item is an object of the Sales_itemd type.

 

Sales_item is a custom type. When a custom header file is used, double quotation marks ("") are used to enclose the file name.

Initial functions

What is a member function? It belongs to a member of a class and appears in the class body. This is different from common functions. member functions can access any member of this class and reference valid data in this scope. When calling a member function, you must specify the object to be operated by the function. The syntax is to use the vertex operator (.): item1.same _ isbn;

 

 

1.6 -- C ++ Program

The previously introduced knowledge is only being used for some knowledge reserves. After learning this chapter, we can solve the problem of the original bookstore.

 

<Span style = "font-family: KaiTi_GB2312;"> </span> <pre class = "cpp" name = "code"> // C ++ Program

# Include <iostream>

# Include "Sales_item.h"

 

Int main ()

 

{

 

Sales_item total, trans; // defines two Sale_item class objects

// Read the transaction

If (std: cin> total ){

 

While (std: cin> trans)

// The ISBN is the same

If (total. same_isbn (trans ))

// Update total

Total = total + trans;

// Different ISBN

Else {

 

Std: cout <total <std: endl;

// Reset total with the newly read transaction

Total = trans;

 

}

 

Std: cout <total <std: endl;

 

} Else {

 

Std: cout <"No data ?!" <Std: endl;

 

Return-1;

 

}

 

Return 0;

 

}

 

 

 

 

This chapter has introduced enough knowledge points, and I have clearly listed these knowledge points. Although it took me not too much time to write this note, however, I still think it is a waste of time and the progress is very slow. The basic plan is to take notes immediately after reading a chapter. In the future, I will try to record the key points and difficulties as much as possible, and there are some error-prone knowledge points, there are also some common mistakes. The first note, with a good start, will work harder to write

From wwj's dream path

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.