C + + Primer Fifth Edition 1th Chapter study Notes * * * *
Experimental code is debugged in red Hat 6.6 or vs 2013 * * * * *
The content of the article is based on the current knowledge writing, the limitations of cognition * * *
1.1 Writing a simple C + + program
function: According to my own understanding, C + + function is a module capable of accomplishing a function.
The composition of the complete function:
① return type: There is not necessarily a return value, so there is not necessarily a return type
② function Name: A module that identifies a specific function according to its name, must exist
③ parameter list: May not have parameters passed in, not necessarily exist
④ function Body: A complete function should have a function body, which contains the function to be implemented.
The following is an example of a complete function:
int main () {return 0;}
1.1.1 Compiling and running the program
As the book says, focus first on the C + + language itself, and then learn about the IDE, which is skipped here.
1.1 Sessions
Exercise 1.2: Rewrite the program and let it return-1. A return value of 1 is usually identified as a program error. Recompile and run your program to see how your system handles the error ID returned by main.
Answer: There is no way to compile successfully under Red Hat, always report such errors:
It is possible to compile successfully in VS 2013, but it is identical to return 0 and does not observe the effect.
1.2 Initial input and output
Input and output are not formal components of the C + + language. Neither C nor C + + itself provides a specialized statement structure for inputs and outputs. The input output is not defined by the C + + itself, but is defined in the I/O library provided by the compilation system.
The C + + standard input and output defines 4 Io, namely CIN (standard input), COUT (standard output), Cerr (standard error), CLOG, and these four are automatically associated with the current program open.
The standard input and output has the concept of buffering and non-buffering, but the impression that the standard error is non-buffered, in the event of an error, immediately output error.
Exercise 1.3: Write a program that prints hello, world on standard output.
Answer:
int main () {std::cout << "Hello,world." << Std::endl;return 0;}
Exercise 1.4: Our program uses the addition operator + to add two numbers. The writer uses the multiplication operator * To print a product of two numbers.
Answer:
int main () {int v1 = 0, v2 = 0;std::cout << "Input-numbers:" << std::endl;std::cin >> v1 >> v2; Std::cout << "The product of" << v1 << "and" << v2 << "is" << v1*v2 << std: : Endl;return 0;}
Exercise 1.5: We put all the output operations in a very long statement. Rewrite the program to place the print operation of each operand in a separate statement.
Answer:
#include <iostream>int main () {int v1 = 0, v2 = 0;std::cout << "Input" numbers: "<< std::endl;std::cin >> v1 >> v2;std::cout << "the product of"; Std::cout << v1;std::cout << "and"; Std::cout &L t;< v2;std::cout << "is"; std::cout << v1*v2;std::cout << std::endl;return 0;}
Exercise 1.6: Explain whether the following program fragment is legal.
Std::cout << "The sum of" << v1;<< "and" << v2;<< "is" << v1 + v2 << std::e Ndl
If the program is legitimate, what does it output? If the program is not legal, why? How should I fix it?
Answer:
Std::cout << "The sum of" << v1; Valid, assuming v1=1, then output the sum of 1
<< "and" << v2;//is not legal, because there is no standard output IO, should be fixed to std::cout << "and" << v2;
<< "is" << v1 + v2 << std::endl; Not valid for the same reason as the second statement, no standard output IO, should be fixed to std::cout << "is" << v1 + v2 << Std::endl;
1.3 Introduction to Annotations
C + + has 2 annotation styles, a single-line double-slash comment, such as//This is a comment
It is also always a multiline descriptor comment, such as/* Comment content */, such comments cannot be nested with each other, such as comments/* First-level comments/* Second-level comments */First layer this is */, in this case from the first delimiter to the third delimiter as a comment, the last */is considered unrecognized code.
1.3 Sessions
Exercise 1.7: Compile a program that contains an incorrect nested comment and observe the error message returned by the compiler.
int main () {std::cout << "test of comments!"; */ * Test */ */return 0;}
VS 2013 Error:1>------Build Started:Project:return, configuration:debug Win32------1> return.cpp1>c:\ Users\pluse\documents\visual Studio 2013\projects\return\return\return.cpp (4): Warning C4138: ' */' found outside of Comment1>c:\users\pluse\documents\visual Studio 2013\projects\return\return\return.cpp (4): Error C2059:syntax Error: '/' ========== build:0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Red hat Error: 1.7.cpp:in function ' int main () ': 1.7.cpp:4: error:expected primary-expression before '/' Token1.7.cpp:5: Error : expected primary-expression before ' return ' 1.7.cpp:5: error:expected '; ' Before ' return '
Exercise 1.8: Indicate which of the following output statements are legal (if any):
Std::cout << "/*"; std::cout << "* *"; std::cout <</* */"*/;std::cout <</* * * *"/"/*" * * *;
Predict what the results of compiling these statements will be, and actually compile the statements to validate your answer (write a small program, each time the above statement as its body), and correct each compilation error.
Answer:
The contents of the double quotation marks are directly output, not annotated, so the first 3 grammars are correct except for the last error. should be modified to:
Std::cout << * * * * * */* * *;
1.4 Control Flow 1.4.1 While statement the format of the while loop is:
while (condition)//NOTE brackets statement
Exercise 1.9: Write a program that adds 50 to 100 integers using a while loop. Answer:
#include <iostream>int main () {int val =, sum = 0;while (val <=) {sum + = val;val++;} Std::cout << "sum of inclusive is" << sum << std::endl;return 0;}
Exercise 1.10: In addition to the + + operator increasing the value of an operand by 1, there is a decrement operator implementation that reduces the value by 1. Write a program that uses the decrement operator to print integers between 10 and 0 in descending order in a loop. Answer:
#include <iostream>int main () {int val = 10;while (val >=0) {std::cout << val<< "; val--;} Std::cout << Std::endl;return 0;}
Exercise 1.11: Write a program that prompts the user to enter two integers to print out all integers in the range specified by the two integers.
Answer:
#include <iostream>//The other loops and judgments have not yet been learned, so use only while to implement int main () {int a,b;std::cout << "input" numbers: "; STD :: Cin >> a >> b;while (a<=b) {std::cout << a << ""; a++;} while (a > B) {std::cout << b << ""; b++;} Std::cout << Std::endl;return 0;}
The form of the 1.4.2 for statement requires attention
for (initialization; Condition;increment)//Note semicolon! Statement
Exercise 1.12: What functionality does the following for loop accomplish? What is the final value of sum?
int sum = 0;for (int i = -100; I <=; ++i) sum + = i;
Answer:
The For loop has completed the sum of 100 to 100, and the end value of summation is 0.
Exercise 1.13: Use the For loop to redo all the exercises in the 1.4.1 section (page 13th). Answer: Slightly
Exercise 1.14: Compare the pros and cons of both for loop and while loop. Answer: In my opinion is 2 different forms of the cycle, there is no big difference, citing the search answers: "In the For loop, the initialization and modification of the loop control variables are placed in the statement header section, the form is more concise, and particularly suitable for the case of known cycle times." In the while loop, the initialization of the loop control variable is generally placed before the while statement, the modification of the cyclic control variable is generally placed in the loop body, the form is not as concise as the for statement, but it is more applicable to the situation of the cycle times difficult to predict (with a certain condition to control the loop). Each of the two forms has advantages, but they are functionally equivalent and can be converted to each other. ”
Exercise 1.15: Write a program that contains common errors discussed in page 16th, "re-compile." Be familiar with compiler-generated error messages. Answer: Slightly 1.4.3 Read the variable number of input data in this section to learn that the while loop can use this format: while (std::cin >> value), this is not seen in C, but there are similar. Exercise 1.16: Write a program that reads a set of numbers from CIN and outputs its and.
#include <iostream>//book program does not work well and does not provide an end method for int main () {int sum = 0, value = 0;std::cout << "Enter Q to Quit:" ; Enter the letter Q to exit, in fact, any letter can exit, this is because value is an int type, the input q belongs to char type, while (std::cin >> value)//So std::cin >> Value returns false thereby ending the loop sum + = Value;std::cout << "sum is:" << sum << std::endl; return 0;}
1.4.4 If statement in the form of the IF statement:
if (expression) </span>//Note the parentheses after the IF!! Statement
Exercise 1.17: If all the values entered are equal, what will the program in this section output? If there is no duplicate value, what will the output be? Answer: If all values entered are equal, the program outputs the number of consecutive occurrences of the input values. If there is no duplicate value, the output will output the previous value 1 consecutive times. Exercise 1.18: Compile and run the program in this section and give it all equal values. Run the program again and enter a value that has no duplicates. Answer: A little exercise 1.19: Modify the program you wrote for 1.4.1, Exercise 1.10 (page 13th), so that it can handle the case that the first number of user input is smaller than the second. Answer: It should be 1.4 to speculate that the topic requires a modified procedure. The answer to the 1-1.11,1.11 exercise is already achievable. 1.5 Class Introduction finally came to "class" Study! See the class in the textbook introduction, I think of the first time is just finished C inside the structure, class and structure very much like, is the type of custom, but the difference is very big. Again look at the introduction of the class, but also think of C language inside the Adt,adt and class very much like, but there are very big differences. First, in non-object-oriented environments, such as C and Fortran, only ADT can be used, and when processing multiple data instances, manual provisioning is required to add some service operations for ADT to create and delete instances, as well as to redesign other service operations of ADT to support multiple instances. Class is not necessary. A preliminary understanding of the class is that the class is an external data type defined for C + +, and when a data type is determined, a property set and an operation set of the type are determined accordingly. For example, a property of type int is that it represents an integer value, so it has an integer property. It allows arithmetic operations to change the sign of an int number, add two int numbers, subtract two int, multiply two int, divide two int, and one int to modulo another. When declaring a variable to be of type int, you mean these operations and only those actions can work on it. "This phrase comes from C Primer Plus" Exercise 1.20: On site http://www.informit.com/title/0321714113, the 1th chapter of the code directory contains the header file sales_item.h. Copy it to your own working directory. Use it to write a program that reads a set of book sales records and prints each record to the standard output. Answer: This section of the experiment needs to use the author's class header file, the book Code directory is: Http://ptgmedia.pearsoncmg.com/images/9780321714114/downloads/GCC_4_7_0.zip, After the download, you can find a "VErsion_test.h "header files and 1 folders under the" sales_item.h "header file, the 2 files are included in the current program source file directory, note that 2 files are required, otherwise compile will be reported: Cannot open include file: ' Version_test.h ': No such file or directory, resulting in unable to complete compilation. This is done using input redirection, as follows: 1. Compile the code to generate the executable. exe file 2. Start-run--cmd: Drag the generated. exe file into the command-line window, drag a prepared set of book sales record text files into the command-line window, and then enter to execute.as shown in the form, the specific path is based on the personal directory environment and is illustrative only. Exercise 1.21: Write a program, read two ISBN the same Sales_item object, output their and. Answer:
#include <iostream> #include "sales_item.h" int main () {Sales_item item1, item2;std::cin >> item1 >> Item2;std::cout << item1 + item2 << Std::endl; return 0;}
Exercise 1.22: Write a program, read multiple sales records with the same ISBN, and output all records. Answer: The same as exercise 1.20
C + + Primer Fifth Edition: Chapter 1th