[Road from C to C++] Take you to understand input and output (preliminary), default parameters and function overloading

foreword

This article is the second part of the column [The Road from C to C++], talking about input and output (preliminary), default parameters and function overloading in C++.

C++ input and output (preliminary)

Preliminary Understanding

​ I believe that most people have tried to say hello to the world when they first learned programming languages: hello world, so how do you say hello to the world in C++?

#include<iostream>//输入输出需要包含这个头文件
using namespace std;// std是C++标准库的命名空间名,C++将标准库的定义实现都放到这个命名空间中

int main()
{
    
    
	cout<<"Hello world!!!"<<endl;
	return 0;
}

​ Does it feel very different from C? Is this cout an output function? <<Isn't it a bitwise operator? What is endl? I believe you must have a lot of confusion and curiosity at the moment, but at present, it is impossible to fully answer your questions. We will answer some questions bit by bit in the later study. We only need to understand these things first:

  1. When using cout (console out) standard output object (console) and cin (console in) standard input object (keyboard), you must include the < iostream > header file and specify the namespace std.

  2. cout and cin are global stream objects, and endl is a special C++ symbol, which means newline output (equivalent to '\n'), and they are all included in the <iostream> header file.

  3. << is the stream insertion operator and >> is the stream extraction operator.

  4. It is more convenient to use C++ for input and output. It does not need to manually control the format like printf/scanf input and output. C++ input and output can automatically identify variable types.

  5. In fact, cout and cin are objects of type ostream and istream respectively. >> and << also involve knowledge such as operator overloading. We will learn this knowledge later, so we simply learn how to use them here. Later, we will learn more about the usage and principle of IO flow.

Notice:

​ In the early standard library, all functions were implemented in the global domain, declared in the header file with the . Also in order to use the namespace correctly, it is stipulated that the C++ header file does not contain .h. The <iostream.h> format is also supported in the old compiler (vc 6.0), which is not supported by subsequent compilers, so it is recommended to use the method of <iostream>+std.

​ There are many more complex usages of cout and cin, such as controlling the output precision of floating-point numbers, controlling the output format of integers, and so on. It's just that C++ is compatible with the usage of C language (you can use the formatted input and output functions scanf and printf), and these are not used a lot, so we won't start learning here.

Examples

​ Let's first understand cout and cin visually. The original name of cout is console out, which means console output, and the original name of cin, console in, means console input. cin corresponds to the stream extraction operator >>, which can be understood as the content input to the console "flows" into the specified variable space like water flow; cout corresponds to the stream insertion operator <<, which can be understood as the content of the variable or constant space like Like a stream of water, it "flows" out to the console and then outputs it.

image-20220916102132764

​ A << or >> corresponds to an element, and multiple data can be input or output at one time. endl has the same effect as '\n'.

​ If you want to output strings and values ​​alternately, it is not very convenient to use cout, and it does not look intuitive.

int main()
{
    
    
    int a = 5;
    int b = 10;
    int c;
    int d;

    cin >> c >> d;
    cout << "The value of a is " << a << '\n' << "The value of b is " << b << endl;

    return 0;
}

Since C++ is compatible with C, printf can be used in this case. So which function to use is completely determined by personal needs, you can use cin, cout and scanf. Printf can also be used, just choose the most convenient one in different scenarios.

default parameters

Default parameter concept

​ The default parameter is to specify a default value (also called default value) for the parameter of the function when declaring or defining the function. When calling the function, if no actual parameter is specified, the default value of the formal parameter is adopted, otherwise the specified actual parameter is used.

void Func1(int a)//若不传参会报错,必须传参
{
    
    
    cout<<a<<endl;
}

void Func2(int a = 0)//可传参也可不传参
{
    
    
	cout<<a<<endl;
}

int main()
{
    
    
    Func(); // 没有传参时,使用行参的默认值
	Func(10); // 传参时,使用指定的实参
	return 0;
}

Classification of default parameters

All default parameters

​ Function parameters are all set to default parameters.

void Func(int a = 10, int b = 20, int c = 30)
{
    
    
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;
	cout<<"c = "<<c<<endl;
}

int main()
{
    
    
    Func(1, 2);
    return 0;
}

It should be noted that the order of passing parameters is continuous from left to right. For example, in the above code, only 1 and 2 are passed to a and b respectively, and no jumps can be passed to c.

semi-default parameter

​ Some parameters are default parameters.

void Func(int a, int b = 10, int c = 20)
{
    
    
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;
	cout<<"c = "<<c<<endl;
}

​ Semi-default parameters must be defaulted continuously from right to left, and cannot be given at intervals, for example, it is not possible void Func(int a, int b = 10, int c).

Default parameter characteristics

  1. The default parameter cannot appear in the function declaration and definition at the same time (only in the declaration of the header file when there are multiple files)

    Reason: Let the declaration and definition appear at the same time. If the values ​​provided by the two locations happen to be different, the compiler cannot determine which default value should be used.

  2. The default value must be a constant or a global variable

  3. C language does not support (compiler does not support)

function overloading

​ In natural language, a word can have multiple meanings, and people can judge the true meaning of the word through the context, that is, the word is overloaded.
​For example: There was a joke before that you don’t need to watch or worry about two state-owned sports events. One is table tennis and the other is men's soccer. The former is "no one can win!", and the latter is "no one can win!"

Function overloading concept

Function overloading: It is a special case of functions. C++ allows several functions with the same name to declare similar functions in the same scope. These functions with the same name have different formal parameter lists (parameter number or type or type order), which are often used to deal with To achieve similar functions but different data types.

different parameter types

​ Add function, one is the version whose parameter type is int, and the other is the version whose parameter type is double, which will automatically recognize the parameter type.

#include<iostream>
using namespace std;

int Add(int left, int right)
{
    
    
	return left + right;
}

double Add(double left, double right)
{
    
    
	return left + right;
}

int main()
{
    
    
	Add(10, 20);
	Add(10.1, 20.2);
    return 0;
}

The number of parameters is different

#include<iostream>
using namespace std;

void f()
{
    
    
	cout << "f()" << endl;
}
void f(int a)
{
    
    
	cout << "The value of a is " << a << endl;
}

int main()
{
    
    
    f();
	f(10);
    return 0;
}

Parameter type order is different

#include<iostream>
using namespace std;

void f(int a, char b)
{
    
    
	printf("The value of a is %d\n", a);
    printf("The value of b is %d\n", b);
}

void f(char b, int a)
{
    
    
	printf("The value of a is %d\n", a);
    printf("The value of b is %d\n", b);
}

int main()
{
    
    
	f(10, 'a');
	f('a', 10);
	return 0;
}

image-20220916112254572

Function overloading with default parameters

​ Functions with default parameters can also constitute function overloading. After all, default parameters are also parameters, but an additional default value is specified.

​ Look at the following piece of code, because there is no problem with default parameters without passing parameters, so there will be ambiguityf() about which function to call , and an error will be reported when calling.

#include<iostream>
using namespace std;

void f()
{
    
    
	cout << "f()" << endl;
}
void f(int a = 10, int b = 20)
{
    
    
	cout << "The value of a is " << a << endl;
    cout << "The value of b is " << b << endl;
}

int main()
{
    
    
    f();
	f(10, 20);
    return 0;
}

C++ supports the principle of function overloading (preliminary)

​ The principle is name mangling, and it is not suitable to explain it in depth here. We only need a superficial understanding, and we will talk about the in-depth content later.

​ Why does C++ support function overloading, but C language does not support function overloading?

​ Remember, I said before that in C/C++, to run a program, it needs to go through the following stages: preprocessing, compiling, assembling, and linking.

image-20220916113954501

Specific process

image-20220916114147232

​ When forming a symbol table, C directly puts the function name in it, while C++ modifies the function name according to the parameter type.

​ Since the modification rules of VS under Windows are too complicated, and the modification rules of g++ under Linux are simple and easy to understand, we use g++ to demonstrate the modified name below. ​The result after compiling
with

image-20220916114423121

Conclusion: Under linux, after compiling with gcc, the modification of the function name has not changed.

The result after compiling with a C++ compiler

image-20220916114444551

Conclusion: Under Linux, after compiling with g++, the modification of the function name changes, and the compiler adds the function parameter type information to the modified name, and the function modification becomes [_Z+function length+function name+type initials 】.

​ Through this, I understand that the C language cannot support overloading, because there is no way to distinguish functions with the same name. C++ is distinguished by function modification rules. As long as the parameters are different, the modified names are different, and overloading is supported.
​ If the function names and parameters of two functions are the same, only the return value is different, which does not constitute overloading, because the compiler cannot distinguish only by the return value when calling.

​ Is it because the function name modification rule does not bring a return value so that it does not constitute overloading? Since you can modify the function name according to the parameter type to distinguish different overloaded functions, can you also modify the function name according to the return value type to distinguish it? At first glance, it seems to be possible, so functions with different return values ​​can also constitute overloading, so why doesn't the syntax support it?

​ The real reason why it does not constitute overloading is that there will be ambiguity when calling, because the return value type cannot be specified when calling, and the two functions look the same, so it is impossible to distinguish which one is used.

image-20220916124339664


Thank you for watching, your support is my greatest encouragement~
insert image description here

Guess you like

Origin blog.csdn.net/weixin_61561736/article/details/126897904