C++ Primer Plus (6th Edition) Study Notes Chapter 2

Preprocessor compilation directives:
#include <iostream>

The main function header should use:
intmain()
If no return statement is encountered at the end of the main function, it is added by default:
return 0;
as an end.

If using iostream as a header file, the namespace pragma should be included to make the definition available to the program:
using namespace std;

If string is a string, the following code will display the string:
cout << string

Comprehensive use of cout and cin:
int carrots;
cout << "How many carrots do you have?" << endl; //endl is the line change control character
cin >> carrots;
cout << "Here are two more.\n";
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots." << endl;
cin.get(); //read input when entering a number and pressing enter
cin.get(); //pause the program until the enter key is pressed

operation result:
How many carrots do you have?
12
Here are two more.
Now you have 14 carrots.

User-defined function:
1. First declare the function to be defined in the front end of the program:
int size (int, int) ;
return value type function name function input 1 type function input 2 type statement end
2. When called in the program:
x = size (6, 8) ;
函数返回的值被赋给x 函数名 传递给函数的参数信息,注意逗号后的空格         语句结束
3.在main函数后给出所声明的函数的定义:
int size (int n, int m)
{
return n * m;
}
4.额外注意事项:
函数头为void时不需要return返回语句;
如果在定义中出现了cout等语句时,定义的开头应加上:
using namespace std;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324763562&siteId=291194637