[C++] keywords, namespaces, input and output, default parameters, function overloading

C++ keywords (C++98)

C++ has a total of 63 keywords, and C language has 32 keywords.

Next, let's take a look at how many keywords C++ has. We don't have a specific understanding of the keywords, and we will talk about them in detail later, which is easier.

insert image description here

Namespaces

Background

In C/C++, there are a large number of variables, functions, and classes to be learned later. The names of these variables, functions, and classes will all exist in the global scope, which may cause many conflicts. The purpose of using the namespace is to localize the name of the identifier to avoid naming conflicts or name pollution, and the appearance of the namespace keyword is aimed at this kind of problem.

#include <stdio.h>
#include <stdlib.h> 
//rand本来就是<stdlib.h>里的库函数,下面我们自己又定义一个和它同名的变量,冲突了
int rand = 10; 
// C语言没办法解决类似这样的命名冲突问题,所以C++提出了namespace来解决
int main()
{
    
    
 printf("%d\n", rand);
return 0; }
// 编译后后报错:error C2365: “rand”: 重定义;以前的定义是“函数”

namespace definition

To define a namespace, you need to use the namespace keyword, followed by the name of the namespace, and then a pair of {}, which are members of the namespace.

// 命名空间的名字可以根据需要自己起
// 1. 正常的命名空间定义
namespace lx
{
    
    
 // 命名空间中可以定义变量/函数/类型
 int rand = 10;
 int Add(int left, int right)
 {
    
    
 return left + right;
 }

Note: A namespace defines a new scope, and all content in this namespace is limited to this namespace

Namespaces can be nested:

namespace N1
{
    
    
		namespace N2
		{
    
    
			int a;
			int b;
			int Add(int left, int right)
			 {
    
    
     			return left + right;
 			 }
 		}
} 		

Multiple namespaces with the same name are allowed in the same project, and the compiler will finally combine them into the same namespace.

Namespace usage

Let's talk about the use of namespaces:

 namespace N
{
    
    
 // 命名空间中可以定义变量/函数/类型
 int a = 0;
 int b = 1;
 int Add(int left, int right)
 {
    
    
 return left + right;
 }
 struct Node
 {
    
    
 struct Node* next;
 int val;
 };
 
}

There are three ways to use the above namespace N:

1. Add namespace name and scope qualifier

int main()
{
    
    
		//下面变量a是命名空间N里的变量,可以通过 N:a 的方式来使用a
    printf("%d\n", N::a);
    return 0;    
}

2. Use using to introduce a member in the namespace

//using N::b 相当于将命名空间N里的变量b和全局作用域连接起来了
//可以像使用普通变量一样使用它
using N::b;
int main()
{
    
    
	//通过 N:a 的方式来使用a
    printf("%d\n", N::a);
    printf("%d\n", b);
    return 0;    
}

3. Using the using namespace namespace name is equivalent to connecting the entire namespace with the global domain. At this time, the namespace will exist in name only and will not work. But the above two methods can still be used. But it's no longer necessary.

using namespace N;
int main()
{
    
    
    printf("%d\n", a);
    printf("%d\n", b);
    Add(10, 20);
    return 0;    
}

Conventions for using the std namespace:

  1. In daily practice, it is recommended to directly use namespace std, which is very convenient.
  2. When using namespace std is expanded, the standard library is fully exposed. If we define a type/object/function with the same name as the library, there will be conflicts. This problem rarely occurs in daily practice, but it is easy to occur when there are many codes and large scale in project development. Therefore, it is recommended to use it in project development. When using it like std::cout, specify the namespace + using std::cout to expand common library objects/types, etc.

input Output

#include<iostream>
// std是C++标准库的命名空间名,C++将标准库的定义实现都放到这个命名空间中
using namespace std;
int main()
{
    
    
cout<<"Hello world!!!"<<endl;
return 0; }
  1. When using the cout standard output object (console) and the cin standard input object (keyboard), you must include the <iostream> header file and use std by namespace usage.
  2. cout and cin are global stream objects, and endl is a special C++ symbol that represents newline output. 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++ input and output, and does not need to manually control the format like printf/scanf input and output.
    The input and output of C++ can automatically identify the variable type.

Note: In the early standard library, all functions were implemented in the global domain, declared in the header file with the suffix of . , and in order to use the namespace correctly, it is stipulated that the C++ header file does not contain .h; the old compiler (vc 6.0) also supports the <iostream.h> format, and the subsequent compiler does not support it, so it is recommended to use +std.

default parameters

What is the default parameter

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

void Func(int a = 0)
 {
    
    
 cout<<a<<endl; 
 }
 
int main()
 {
    
    
 Func();     // 没有传参时,使用参数的默认值,结果为0
 Func(10);   // 传参时,使用指定的实参,结果为10
return 0; 
 }

Classification of default parameters

All default parameters: All formal parameters of the function are assigned initial values. If no actual parameters are specified when calling the function, all default parameter values ​​will be used.

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

Semi-default parameters: Some formal parameters of the function are assigned initial values, and the parameters without defaults must be given actual parameter values ​​when calling the function.

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

Notice:

  1. The semi-default parameters must be given sequentially from right to left, and cannot be given alternately.
  2. Default parameters cannot appear in function declaration and definition at the same time (if there must be function declaration and definition, then only appear in function declaration)
  //a.h
  void Func(int a = 10);
  
  // a.cpp
  void Func(int a = 20)
 {
    
    }
  
  // 注意:如果声明与定义位置同时出现,恰巧两个位置提供的值不同,那编译器就无法确定到底该用那个缺省值。
  1. The default value must be a constant or a global variable.
  2. C language does not support (compiler does not support).

function overloading

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. The formal parameter lists of these functions with the same name (the number of parameters or types or order of types) are different, and are often used to deal with Implementing functions is similar to the problem of different data types.

There are three types of function overloading:

#include<iostream>
using namespace std;

// 1、参数类型不同
int Add(int left, int right)
 {
    
    
 cout << "int Add(int left, int right)" << endl;
 return left + right;
  }
  
double Add(double left, double right)
 {
    
    
 cout << "double Add(double left, double right)" << endl;
 return left + right;
  }
  
// 2、参数个数不同
void f()
{
    
    
 cout << "f()" << endl;
 }
 
void f(int a)
 {
    
    
 cout << "f(int a)" << endl; 
 }
 
// 3、参数类型顺序不同
void f(int a, char b) 
{
    
    
 cout << "f(int a,char b)" << endl; 
 }
 
void f(char b, int a)
{
    
    
 cout << "f(char b, int a)" << endl;
  }

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

C++ supports the principle of function overloading – name modification

Each compiler has its own function name modification rules. Judging from the name modification rules of compilers under Windows and Linux, the ones under Windows are more complicated, while the ones under Linux are more intuitive and easy to understand. Here we use the ones under Linux as an example. example.
From the following, we can see that the function name of the gcc compiler (for C language) remains unchanged after modification. And the g++ compiler (for C++) after the function modification becomes [_Z+function length+function name+type initial]

gcc compiler: (C language does not support function overloading, because its function modification rules only look at function names) insert image description here
g++ compiler: (C++ supports function overloading, because its function modification rules do not only look at function names)
insert image description herefrom the above figure It can also be seen from the C++ name modification rules that function overloading has nothing to do with the return value of the function.

The name modification rules under Windows are not intuitive enough, just have an impression.insert image description here

Guess you like

Origin blog.csdn.net/m0_52347974/article/details/128907771