[C++] namespace, default parameters and function overloading


1. Namespace

In a large-scale project, naming conflicts will inevitably occur in programs written by different members. In this case, namespaces can be a good solution to the problem of large naming conflicts. It can be used to avoid name (name) conflicts in different libraries or modules.

Names can be variables, functions, classes, structures, enumerations, and more. In addition, namespaces allow us to group names into different logical spaces, improving code readability and maintainability.

1. Namespace definition

A namespace is defined using the keyword namespace followed by the name of the namespace:

namespace namespace_name {
    
     
// 命名空间中的代码 
}

In order to call a name in a namespace, it needs to be preceded by the name of the namespace and the :: operator, as follows:

namespace_name::name;

We can also use the using namespace directive to omit the name of the namespace, but this may increase the risk of conflicts.

Namespaces can be defined in several different places, or 嵌套in other namespaces. Multiple namespaces with the same name are allowed in the same project, and the compiler will finally combine them into the same namespace.

The namespace is open, which means we can add new names to the existing namespace at any time.

A namespace defines a new scope, and everything in the namespace is confined to that namespace.

2. Namespace usage

Namespaces are used in three ways

  • Add namespace name and scope qualifier
namespace example {
    
     
	int a = 0;
	int b = 1;
	int Add(int left, int right)
	{
    
    
		return left + right;
	}
)

int main()
{
    
    
	printf("%d\n", example::a);
	return 0;
}
  • Use using to introduce a member in the namespace
namespace example {
    
     
	int a = 0;
	int b = 1;
	int Add(int left, int right)
	{
    
    
		return left + right;
	}
)

using example::b;
int main()
{
    
    
	printf("%d\n", example::a);
	printf("%d\n", b);
	return 0;
}
  • Use the using namespace namespace name to import
namespace example {
    
     
	int a = 0;
	int b = 1;
	int Add(int left, int right)
	{
    
    
		return left + right;
	}
)

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

2. Default parameters

I feel that this name is not well chosen, and I can’t understand what is called a default parameter from the literal meaning. English is easier to understand, default argument , the literal translation isdefault parameters

Simply put, it refers to specifying a default value for the parameters of the function when the function is declared or defined. When the function is called, if no actual parameter is specified, the default value is used; otherwise, the specified actual parameter is used.

1. Classification

  1. All default parameters

That is, all the parameters of the function are given a default value.

void Func(int a = 10, int b = 20, int c = 30)
{
    
    
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;
	cout<<"c = "<<c<<endl;
}
  1. semi-default parameter

It is to give some parameters default values
. Note:

  1. Semi-default parameters must be given sequentially from right to left, and cannot be given alternately, and can only be omitted from the last parameter when calling
  2. Default parameters cannot appear in function declaration and definition at the same time, the declaration is for the default parameter, and the definition is not for
void Func(int a, int b = 10, int c = 20)
{
    
    
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;
	cout<<"c = "<<c<<endl;
}

2. Other precautions

  1. Default arguments must be constants or global variables, not local variables or expressions.
  2. Default parameters cannot change the overloading rules of the function, that is, functions with the same name cannot be distinguished according to the number of default parameters.

3. Function overloading

A special case of functions, C++ allows several functions of the same name with similar functions to be declared in the same scope . The formal parameter lists of these functions with the same name ( parameter number or type or type order ) are different, and are often used to deal with the problem of implementing similar functions with different data types.

// 1、参数类型不同
int Add(int left, int right)
{
    
    
	//...
}

double Add(double left, double right)
{
    
    
	//...
}

// 2、参数个数不同
void f()
{
    
    
	//...
}

void f(int a)
{
    
    
	//...
}

// 3、参数类型顺序不同
void f(int a, char b)
{
    
    
	//...
}

void f(char b, int a)
{
    
    
	//...
}

If the function names and parameters of two functions are the same, but the return values ​​are different , it does not constitute overloading.


Summarize

The above can be said that C++ is making up for some of the less useful parts of the C language. Mastering this knowledge will help the subsequent learning of C++.

Guess you like

Origin blog.csdn.net/lyq2632750277/article/details/131793947