[C++] Detailed introduction to basic knowledge (1)

Table of contents

1. C++ keywords

2. Namespace

  1. Definition of Namespace

  2. The use of namespaces

3. C++ input && output

4. Default parameters

  1. The concept of default parameters

  2. Classification of default parameters

    2.1 All default parameters

    2.2 Semi-default parameters


1. C++ keywords

Before we learn C++, I believe that most of you know more or less about C language. There are 32 keywords in C language. There are 63 C++ keywords in this article, as shown in the following figure:

2. Namespace

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 . The emergence of the namespace keyword is aimed at this problem.

  1. Definition of Namespace

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.

As follows:

//xx为命名空间的名称
namespace xx
{
    //命名空间中的内容既可以定义变量,也可以定义函数,还可以定义结构体
	int a = 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;
  		}
	}
}

//在同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间中。
//那么下面这个命名空间 xx 会和上面的第一个命名空间 xx 结合在一块
namespace xx
{
 	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.

  2. The use of namespaces

  There are three ways to use namespaces:

  (1) Add namespace name and scope qualifier

namespace N
{
	int a = 10;
}
int main()
{
	printf("%d\n",N::a);
	return 0;
}

  (2) Use using to introduce a member in the namespace

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

  (3) Use the using namespace namespace name to import

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

Note:  When using variables, follow the following two points: (1) When the variable is not placed in the namespace, the variable is found locally and globally by default. (2) When a variable is placed in a namespace, the variable is first searched in the namespace by default, and then searched locally and globally.

3. C++ input && output

#include<iostream>
// std是C++标准库的命名空间名,C++将标准库的定义实现都放到这个命名空间中
using namespace std;

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

illustrate:

     (1) When using cout standard output object (console) and cin standard input object (keyboard) , you must include the <iostream> header file and use std according to the namespace usage method.

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

     (3)  << is a stream insertion operator ,        and >> is a stream extraction operator .

     (4) It is more convenient to use C++ for input and output, and there is no need to manually control the format like printf/scanf input and output. The input and output of C++ can automatically identify the variable type.

     (5) In fact, cout and cin are objects of type ostream and istream respectively, and >> and << also involve operator overloading and other knowledge, which we will learn later, so we simply learn their usage here. Later we will have a chapter to learn more about the usage and principle of IO stream.

#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;
}

 Conventions for the std namespace:

std is the namespace of the C++ Standard Library:

  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.

4. Default parameters

  1. The concept 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 the function, if no actual parameter is specified, the default value of the formal parameter is adopted, otherwise the specified actual parameter is used.

As follows:

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

  2. Classification of default parameters

    2.1 All 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;
 }

    2.2 Semi-default parameters

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; ( we give default parameters when declaring )
  3. The default value must be a constant or a global variable ;
  4. C language does not support (compiler does not support)


If there are deficiencies in this article, you are welcome to comment below, and I will correct it as soon as possible.

 Old irons, remember to like and pay attention!!!   

Guess you like

Origin blog.csdn.net/m0_63198468/article/details/131218078