C ++ keywords, namespaces, input & output


C ++ keywords

ISO C ++ 98/03 keywords total of 63, here in strict accordance with the standard original layout:
Here Insert Picture Description

Namespace (namespace)

Namespaces

In the C / C ++, the variables, functions, and classes are back to learn there are a lot of variables, function names and class will exist in the global scope, it may lead to a lot of conflict. The purpose is to use the namespace identifier localized name to avoid naming conflicts or name contamination, there is the namespace keyword for this problem.

Namespace definition:

Define a namespace, you need to use the namespace keyword, followed by the name of the namespace, and then you can pick one pair {}, {} is the name of a member of the space.

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

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

// 同一个工程中允许存在多个相同名称的命名空间,编译器最后会合成同一个命名空间中。 
namespace N1
{
	int Mul(int left, int right)
	{
    return left * right;
	}
}

note:

A namespace to define a new scope, all content namespace are limited to the namespace. (It solves the C language can only use the file scoped defects)

Namespaces

namespace N {
	int a = 10;
	int b = 20;
	int Add(int left, int right)
	{
    	return left + right;
	}
	int Sub(int left, int right)
	{
    	return left - right;
	}
}
int main() {
	printf("%d\n", a); // 该语句编译出错,无法识别a
	return 0; 
}

Use namespaces in three ways:

  1. Plus namespace name and scope qualifier
int main() {
	printf("%d\n", N::a);
	return 0; 
}
  1. Use using namespace members introduced
using N::b;
int main()
{
   printf("%d\n", N::a);
   printf("%d\n", b);
   return 0;
}
  1. Using namespace using the namespace name introduced
using namespace N;
int main()
{
	printf("%d\n", N::a);
	printf("%d\n", b);
    Add(10, 20);
    return 0;
}

to sum up:

  1. Namespace can be solved functions, global variable name duplication, including in a different namespace repeat function is actually two completely unrelated functions.
  2. Namespace allows nested, nested inside and outside has nothing to do.
  3. Namespace allows the same name, the same name as the name space will be merged.
  4. Using keywords, you can reference a namespace, such that in the name space with the contents of their visible.
  5. If there are two names in the same space of two functions, even if I also quoted by using two namespaces, so also does not complain unless I use this function (because of the use function ambiguous, that is not know what the call).

Input & Output C ++

C ++ How can it be sound greetings to this wonderful world?

#include<iostream>
using namespace std;

int main()
{
	cout<<"Hello world!!!"<<endl;
	return 0;
}
  1. When using the standard output cout (console) and cin standard input (keyboard), it must contain <iostream> standard header files and std namespace.
  2. O in C ++ is more convenient, without increasing the control data format, such as: plastic - -% d, character - -% c

Note: All standard library functions early in the global domain implementation, declared in the header file .h suffix, use only the corresponding header will contain, and later under the name space std fact, in order to distinguish and C header files , as well as to the proper use of namespaces, the provisions of C ++ header files without .h; old compiler (vc 6.0) also supports <iostream.h> format, follow compiler has not supported, it is recommended to use + std way.

#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; 
}
Published 92 original articles · won praise 32 · views 4629

Guess you like

Origin blog.csdn.net/AngelDg/article/details/104703500