[C++ basic study notes] C++ input and output streams and default parameters

I want to be a curious baby, read with questions, hum~

How does C++ perform input and output? How is it different from the C language?
What are the default parameters for C++? How to understand and master?

insert image description here



C++ input & output

When babies are born into this world, they greet this new world in their own unique way.
Similar to a newborn baby, after the C++ language just came out, it can be regarded as a new thing. As a new programming language, it will also have its own unique way of greeting this beautiful world!

#include<iostream>//input/output stream 输入输出流
using namespace std; //C++库中所有东西都是放到std这个命名空间中的
int main()
{
    
    
	cout << "hello world\n";//可以用\n换行
	//cout--输出流 流的理解:内容顺着 << 流入到cout(控制台显示黑框)中
	cout << "hello world" << endl;//也可以用<<endl换行 C++更推荐这个
	return 0;
}

insert image description here

Note:
1. When using cout standard output (console) and cin standard input (keyboard), you must include the header file and std standard namespace.
Note: In the early standard library, all functions were implemented in the global domain and declared in the header file with the .h suffix. When using it, only the corresponding header file was required. Later, it was implemented in the std namespace, in order to distinguish it from the C header file. , and in order to use the naming space correctly, it is stipulated that C++ does not have .h;
some old compilers (such as vc 6.0) also support the <iostream.h> format, and subsequent compilers do not support it, so it is recommended to use <iostream. >+std way.
2. It is more convenient to use C++ for input and output, without adding data format control, such as: integer %d, character %c (C++ cout can automatically detect and identify the type of data when outputting)

insert image description here
This automatic type recognition is achieved through function overloading, which will be explained in detail later.


Use of input stream cin

#include<iostream>//input/output stream 输入输出流
using namespace std; //C++库中所有东西都是放到std这个命名空间中的
int main()
{
    
    
	int i = 1;
	double d = 2.22;
	cin >> i >> d;//表示从控制台流向i和d的空间去
	cout << i << "  " << d << endl;
	return 0;
}

insert image description here


Default parameter

The concept of default parameters

What are the default parameters?

A default parameter is when a function is declared or defined to specify a default value for a function's parameters. When calling the function, the default value is used if no arguments are specified, otherwise the specified arguments are used.

Example:

#include<iostream>
using namespace std;
// int a = 0 在定义形参的时候给其初始值 此时a就是缺省参数
void Func(int a = 0)
{
    
    
	cout << a << endl;
}
int main()
{
    
    
	Func();//实参为空的时候,就会使用缺省参数的值
	Func(10);//实参不为空的时候,使用实参的值
	return 0;
}

insert image description here

Understanding: The default parameters are actually equivalent to the spare tire in our life. When the actual parameters are not empty, the default parameters are not used, and the default parameters are used when the actual parameters are empty. In a similar way, the spare tire of the car has been placed in the trunk or at the rear of the car when the main tire of the car is working normally, and is not used. , you need to change the spare tire on the road!
Taking the emotional spare tire in life as an example, if a girl has a good relationship with her boyfriend, then the girl's spare tire boyfriend No. 1, spare tire boyfriend No. 2...3... are useless. When the girl and her boyfriend had a bad relationship and broke up, the spare tire No. 1 appeared and quickly became the girl's new boyfriend, and the spare tire No. 2 became the spare tire No. 1... In the end, the

insert image description here

Some fancy ideas: why not call the spare tire parameter? Or what about dog licking parameters?

My thoughts: Maybe it has something to do with the limitations of the times. Maybe the C++ programmer who proposed the default parameters didn't ride a car very much, and then there was no spare tire and the word licking dog in that era. The word licking dog is a new phenomenon in the Internet. word~

//在定义形参的时候可以给其赋值
void Func(int a = 0)
{
    
    
	cout << a << endl;
}
/*当传递实参时,该缺省参数不使用,当不传递实参时,就使用缺省参数。
缺省参数类似于汽车的备胎
*/

Classification of default parameters

There are two types of default parameters

1. Full default parameters

All parameters are defaulted, or all parameters are default parameters.

#include<iostream>
using namespace std;
//全缺省参数
void Func1(int a = 10, int b = 20, int c = 30)
{
    
    
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}
int main()
{
    
    
	Func1();
	cout << "-------------" << endl;
	Func1(1);
	cout << "-------------" << endl;
	Func1(1, 2);
	cout << "-------------" << endl;
	Func1(1, 2, 3);
	cout << "-------------" << endl;
	return 0;
}

Four calling methods
insert image description here

The comma, is to separate the passed parameters, and Func1(, , 3) ​​cannot be used directly, which is a syntax error.
When calling, if you want to pass parameters, you must pass the parameters from left to right, and cannot be left blank.


2. Semi-default parameters:

The default part of the parameters, that is, some parameters are default parameters, and the other part are not default parameters

Understand: This 'half' does not mean half, half, think about if there are 3 parameters, is the half default parameter 1.5? Obviously this understanding is wrong, the correct understanding of this 'half' is relative to 'full', 'full' refers to the whole, and 'half' refers to part!

#include<iostream>
using namespace std;
//半缺省参数 --- 缺省部分参数
void Func2(int a, int b = 20, int c = 30)
{
    
    
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}
int main()
{
    
    
	//Func1();所有参数不传 程序错误
	//cout << "-------------" << endl;
	Func2(1);
	cout << "-------------" << endl;
	Func2(1, 2);
	cout << "-------------" << endl;
	Func2(1, 2, 3);
	cout << "-------------" << endl;
	return 0;
}

insert image description here

At the same time, it is also stipulated that the default must be continuous from right to left, that is to say, the default cannot be separated in the middle.

For example:
insert image description here
another example : another example
insert image description here
:
insert image description here
a semi-default correct example:
insert image description here
another example:
insert image description here

The default must be from right to left. Default
regulation: The actual parameters must be passed from the right to the right.

For example, in the above example, if we pass the parameters
insert image description here
like this: or this:
insert image description here
the above two cases are wrong!
The parameter part of Func2(, , 3) ​​is equivalent to a comma expression, which is meaningless.
Contrast: The default parameters can only be defaulted continuously from right to left, and can only be passed continuously from left to right when calling (syntax regulations)

Grammar learning requires half-memorization and half-comprehension. Some things are prescriptive and incomprehensible. We need to memorize them directly. Some things can be understood. Understanding them will help strengthen our memory. (Memory and understanding are not contradictory, but complementary)

Guess you like

Origin blog.csdn.net/QIYICat/article/details/119767637