C++ Elementary-Namespace, default parameters and function overloading

We know that the hello world file suffix of C language is'.c', then the file suffix of C++ is'.cpp'.
First write the first C++ program Hello World to understand the language of C++.

#include <iostream>
using namespace std;	//1.命名空间

int main()
{
    
    
  cout<<"Hello World!"<<endl;	//2.输出
  return 0;
}

From the hello world of C++, you can see the intuitive difference with the C language :

  • The header file in C language ends with'.h', but C++ writes the name of the header file directly
  • There is one more statement in C++ using namespace std
  • The output statement in C++ is cout, and the newline statement is endl

Then I will start describing the basic knowledge of C++ from the following aspects:

1. Namespace

In C/C++, there are a large number of variables, functions and classes. The names of these variables, functions and classes will all exist in the global scope, which may cause a lot of conflicts. The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts or name pollution . The emergence of the namespace keyword addresses this problem.

For example, explain the naming conflict:

Suppose you and your colleague are responsible for different modules of a project. If you define a variable a and your colleague also defines a variable a , who defines the variable when the program uses the variable a ?Use namespace namespace in C++ to solve this problem

1.1 Definition of Namespace

Define the namespace, you need to usenamespaceKeyword, followed by the name of the namespace, and then a pair of {}, {} is the member of the namespace. There are three ways to define a namespace:

1.1.1 Ordinary namespace

The content in the namespace can be either variable definitions or function definitions .

//1. 普通的命名空间
namespace N1  // N1为命名空间的名称
{
    
    
   // 命名空间中的内容,既可以定义变量,也可以定义函数
   int a;
   int Add(int left, int right)
   {
    
    
       return left + right;
   }
}

1.2.2 Nesting of Namespaces

N3 can be nested in N2

//2. 命名空间可以嵌套
namespace N2
{
    
    
   int a;
   int b;
   int Add(int left, int right)
   {
    
    
       return left + right;
   }
    
   namespace N3	//N3嵌套在N2中
   {
    
    
       int c;
       int d;
       int Sub(int left, int right)
       {
    
    
           return left - right;
       }
   }
}

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

The content of N1 below will be merged with the content of N1 in the previous 1 into one space

namespace N1
{
    
    
   int Mul(int left, int right)
   {
    
    
       return left * right;
   }
}

Note:
A namespace defines a new scope , and all content in the namespace is limited to that namespace, that is, it can only be valid in its own namespace.

1.2 Use of command space

#include<stdio.h>

namespace N2
{
    
    
  int a = 10;
  int b = 20;
  int Add(int x, int y)
  {
    
    
    return x + y;
  }
  namespace N3
  {
    
    
    int c = 30;
    int d = 40;
    int Sub(int x ,int y)
    {
    
    
      return x - y;
    }
  }
}

int main()
{
    
    
  printf("%d\n", a);  // 该语句编译出错,无法识别a
  printf("%d\n", c);  // 该语句编译出错,无法识别c

  return 0;
}

If the above program directly uses a and b variables, an error will be reported. Let's take the second method as an example to explain the three ways of using namespaces:

1.2.1 Add namespace name and scope qualifier

Add the namespace name N1 and the scope qualifier:: before the variable name to access the variables a and c

int main()
{
    
    
  printf("%d\n", N2::a);
  printf("%d\n", N2::N3::c);

  return 0;
}

result:
Insert picture description here

1.2.2 Use using to introduce members in the namespace

using N2::a;

int main()
{
    
    
  printf("%d\n", a);
  printf("%d\n", N2::N3::c);

  return 0;
}

Insert picture description here

Access variable a can be used directly without adding N2::

1.2.3 Introduce using namespace name

using namespace N2;

int main()
{
    
    
  printf("%d\n", a);
  printf("%d\n", N3::c);
  printf("%d\n", Add(20,30));

  return 0;
}

Insert picture description here

You can access the variables and functions in the N2 variable and the nested namespace without entering N2::

Two, input & output

  1. useWhen cout standard output (console) and cin standard input (keyboard), Must include the <iostream> header file and std standard namespace.
  2. It is more convenient to use C++ input and output, without adding data format control , such as: shaping -%d, character -%c, etc.
#include <iostream>
using namespace std;
 
int main()
{
    
    
    int a;
    double b;
    char c;
    
    cin>>a;
    cin>>b>>c;
    
    cout<<a<<endl;
    cout<<b<<"  "<<c<<endl;
    return 0;
}

Insert picture description here

Three, the default parameters

3.1 Definition of default parameters

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 the actual parameter is not specified, the default value is used , otherwise the specified actual parameter is used .

#include <iostream>

using namespace std;

void TestFun(int a = 10)
{
    
    
  cout<<a<<endl;
}

int main()
{
    
    
  TestFun();	// 没有传参时,使用参数的默认值
  TestFun(20);  // 传参时,使用指定的实参
}

Insert picture description here

3.2 Default parameter classification

3.2.1 Full default parameters

All parameters with default values ​​are all default parameters

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

int main()
{
    
    
  TestFun1();
  TestFun1(1);
  TestFun1(1,2);
  TestFun1(1,2,3);

}

Insert picture description here

3.2.2 Semi-default parameters

The default values ​​for only some parameters are semi-default parameters

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

Insert picture description here
Note:
1. The semi-default parameters must be given in order from right to left, and cannot be given separately.
2. The default parameters cannot appear at the same time in the function declaration and definition.

//a.h
void TestFunc(int a = 10);
 
// a.c
void TestFunc(int a = 20)
{
    
    }
 
// 注意:如果生命与定义位置同时出现,恰巧两个位置提供的值不同,那编译器就无法确定到底该用那
个缺省值。

3. The default value must be a constant or a global variable
. 4. C language does not support (the compiler does not support)

Four, function overloading

4.1 The concept of function overloading

Function overloading : It is a special case of functions. C++ allows several functions of the same name with similar functions to be declared in the same scope . The parameter lists (number of parameters or types or order) of these functions of the same name must be different , and they are commonly used to handle them. The realization function is similar to the problem of different data types.

#include <iostream>
using namespace std;

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

}
 
int main()
{
    
    
  Add(10, 20);
  Add(10.0, 20.0);
  Add(10L, 20L);

  return 0;

}

4.2 C++ supports function overloading, while C language does not support function overloading

4.2.1 Process analysis

In C/C++, to run a program, it needs to go through the following stages: preprocessing, compilation, assembly, and linking.
For the specific functions of these four stages, please refer to the second section of this blog,
"Linux C language development tools-learning to use gcc, gdb and make/Makefile tools through the progress bar small program"
Suppose we have the following programs

#include <iostream>
using namespace std;

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

}
 
int main()
{
    
    
  Add(10, 20);
  Add(10.0, 20.0);
  Add(10L, 20L);

  return 0;

}

If you use C language to compile, you will find that because the function names are the same, the compilation fails, but C++ can compile.
Insert picture description here
We disassemble and see what is the modification of function names in C and C++?

  • Use the following naming under Linux to view the C language disassembly result
    objdump -S 05-C_FunOverload

Insert picture description here
Conclusion: Under Linux, after compiling with gcc, the modification of the function name has not changed.

  • Under Linux, use the following naming to view the C++ disassembly result.
    objdump -S 05-FunOverload
    Insert picture description here
    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.

4.2.2 Conclusion

Through the analysis, we can see that the name of the gcc function does not change after modification. The function of g++ becomes [_Z+function length+function name+type initials] after modification .

Through this, I understand that the C language cannot support overloading, because the functions of the same name cannot be distinguished. C++ is distinguished by function modification rules. As long as the parameters are different, the modified names are different, and overloading is supported.

4.3 external "C"

Sometimes in a C++ project, it may be necessary to compile certain functions in the style of C. Add extern "C" before the function, which means to tell the compiler to compile the function according to the C language rules . For example: tcmalloc is a project implemented by Google in C++. It provides two interfaces, tcmallc() and tcfree, but if it is a C project, it can’t be used, so he uses extern "C" to solve it.

extern "C" int Add(int left, int right);
 
int main()
{
    
    
    Add(1,2);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_40076022/article/details/113483132