[C++] Namespace, etc.——Meow Meow Wants to Eat C Quack

I hope you are happy, I hope you are healthy, I hope you are happy, I hope you like it!

Finally, follow meow, follow meow, follow meow, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me!

Table of contents

Preface

Overview of C++ keywords, detailed later

Namespaces

 Definition of namespace

Use of namespaces

C++ input and output

Default parameters

function overloading

definition

Quote

inline function

Summarize


Preface

Without further ado, let’s rush!


Overview of C++ keywords, detailed later


Namespaces

InC/C++, there are a large number of variables, functions and classes to be learned later. These variables, functions and The names of the classes will all exist in the global scope, potentially causing a lot of conflicts. The purpose of using namespaces is to localize identifier names to avoid naming conflicts or name pollution< The emergence of /span> keywords is aimed at this problem. namespace,


 Definition of namespace

To define a namespace, you need to use thenamespacekeyword, followed by< a i=4>The name of the namespace, thenfollowed by a pair{}That's it, {} is a member of the named space .

// 命名空间中可以定义变量/函数/类型
namespace miao
{
int rand = 10;

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

struct Node
{
struct Node* next; int val;
};


//也可以嵌套
namespace N1
{
int a; int b;
int Add(int left, int right)
{
return left + right;
}

namespace N2
{
int c; int d;
int Sub(int left, int right)
{
return left - right;
}
}
}




//同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间中。
//多个文件有相同命名空间,将会合并。


Use of namespaces

Variables/functions/types can be defined in the namespace

Three uses of namespaces:

  • Add namespace name and scope qualifier

  • int main()
    {
        printf("%d\n", N::a);
    	return 0;
    }
    
  • Add namespace name and scope qualifier

  • 	using N::b;
    	int main()
    	{
        printf("%d\n", N::a);
    	printf("%d\n", b);
    	return 0;
    	}
    
  • Usingusing namespace Namespace name introduction

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

C++ input and output

#include<iostream>
// std是C++标准库的命名空间名,C++将标准库的定义实现都放到这个命名空间中 
using namespace std;
int main()
{
cout<<"Hello world!!!"<<endl; 
return 0;
}

cout standard output object (console)

cin standard input object (keyboard)

<< is the stream insertion operator, >> is the stream extraction operator


Default parameters

Default parameters areWhen declaring or defining a functionspecify a default value for the function's parameters . When calling this function, if no actual parameter is specified, the default value of the formal parameter is used, otherwise the specified actual parameter is used.

  • All default parameters
  • semi-default parameters

Notice:

  1. Semi-default parameters must be given from right to left and cannot be given at intervals
  2. Parameter omission cannot appear in both function declaration and definition.
  3. The default value must be a constant or global variable

  4. C language is not supported (the compiler does not support it)

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


function overloading

definition

Function overloading: is a special case of function,C++ allows inDifferent)Type orderorType or Number of parameters( Formal parameter list. declare several functions of the same name with similar functionsIn the same scope

	#include<iostream>
	using namespace std; 3
	// 1、参数类型不同
	int Add(int left, int right)
	{
	cout << "int Add(int left, int right)" << endl; 8
	return left + right;
	}

	double Add(double left, double right)
	{
	cout << "double Add(double left, double right)" << endl; 15
	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;
}	

Quote

Reference is not a new definition of a variable, but is an alias for an existing variable. The compiler will not allocate memory space for the reference variable, it shares the same memory space as the variable it refers to. (That’s the nickname, it’s that person)

Type & reference variable name (object name) = reference entity

void TestRef()
{
int a = 10;
int& ra = a;//<==定义引用类型
printf("%p\n", &a);
printf("%p\n", &ra);
}

//Citation typeRequired sum of quotationPropertyYes same typetype

//References cannot refer to constants

//References can be used as parameters or return values.


inline function

The function modified withinline is called an inline function, When compilingC++ the compiler will expand where calls inline functions, there is no overhead of function calls to establish stack frames, and inline functions improve the efficiency of program operation.

inline is a method of exchanging space for time. If the compiler treats the function as an internal Linked function processing, in the compilation phase, the function call will be replaced with the function body. Disadvantage: it may make the target file larger. Advantage: less calling overhead. , improve program operation efficiency

inline is just a suggestion for the compiler. Different compilers may have different implementation mechanisms for inline Different, general advice: make the function smaller( that is, the function is not Very long, there is no precise explanation, it depends on the internal implementation of the compiler), is not recursive and frequently called< The functions of /span>inlineFeatures, otherwise the compiler will ignore inline are decorated with


Summarize

Digging a hole, Miaomiao wants to write a blog about the analysis of preprocessing, compilation, assembly, linking, and the running of this set of programs. whee


I hope you are happy, I hope you are healthy, I hope you are happy, I hope you like it!

Finally, follow meow, follow meow, follow meow, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me!

おすすめ

転載: blog.csdn.net/ormstq/article/details/134126395