[C++ Elementary] Introduction to C++ - Introduction to C++, Namespace, Input and Output

insert image description here

1. Introduction to C++ Preface

1.1 What is C++?

 C language is a structured and modular language, suitable for handling smaller-scale programs. For complex problems and large-scale programs that require a high degree of abstraction and modeling, C language is not suitable. In order to solve the software crisis, in the 1980s, the computer industry proposed the idea of ​​OOP (object oriented programming: object-oriented ), and programming languages ​​supporting object-oriented programming emerged as the times require. In 1982, Dr. Bjarne Stroustrup introduced and expanded the concept of object-oriented on the basis of C language, and invented a new programming language. In order to express the origin relationship between the language and the C language, it is named C++ . Therefore: C++ is produced based on C language . It can not only carry out procedural programming of C language, but also carry out object-based programming characterized by abstract data types, and can also carry out object-oriented programming .

1.2 History of C++

 In 1979, when Benjani and others at Bell Labs tried to analyze the Unix kernel, they tried to modularize the kernel, so they extended it on the basis of the C language, added a class mechanism, and completed a runnable preprocessing program. , called C with classes.
 The development of language is like practicing skills and fighting monsters to upgrade. It is also a gradual process, from the shallower to the deeper. Let's first look at the historical version of C++.
insert image description here
 C++ is still evolving backwards . But: C++98 and C++11 are still the mainstream use in the society . All the key points are to master C++98 and C++11. As the understanding of C++ continues to deepen, you can have time to think about the updated characteristic.

2. C++ keywords

 C++ has a total of 63 keywords, as follows:
insert image description here

3. Namespace

3.1 Why is there a namespace?

 Large-scale programs generally use multiple independently developed libraries , and these libraries may define a large number of global variable names , such as classes, functions, and templates . When a program uses libraries from multiple vendors, it is inevitable that some names will conflict with each other. Multiple libraries placing names in the global namespace can cause namespace pollution . In order to solve the above problems, the concept of namespace
 is proposed , which divides the global namespace, and each namespace is a scope . Domain is a spatial concept. Common domains include: local domain, global domain, class domain, and namespace domain . Domains affect access and lifecycle .

3.2 Namespace definition

 A namespace definition consists of two parts: first the keywordsnamespace , followed by the name of the namespace . Following the namespace name is a series of declarations and definitions enclosed in curly braces . As long as the declaration can appear in the global scope, it can be placed in the namespace, mainly including: classes, variables (and their initialization operations), functions (and their definitions), templates and other namespaces (this means namespaces can be nested ):

namespace wcy//命名空间的名字
{
    
    
	//定义变量
	int rand = 10;
	//定义函数
	int Add(int left, int right)
	{
    
    
		return left + right;
	}
	//定义类型
	struct Node
	{
    
    
		struct Node* next;
		int val;
	};
	//嵌套命名空间
	namespace N2
	{
    
    
		int c;
		int d;
		int Sub(int left, int right)
		{
    
    
			return left - right;
		}
	}
}

 The above code defines a wcynamespace called , which contains variables, functions, types, and namespaces. Like other names, the name of a namespace must also be unique within the scope in which it is defined . If multiple namespaces with the same name appear in the same scope, the compiler will finally merge them , which means that the namespace Can be discontinuous . Namespaces can be defined in the global scope or in other namespaces , but not inside functions or classes .
Small tips:
 There is no need to follow the semicolon after the namespace scope.

3.3 Every namespace is a scope

Like other scopes, each name in a namespace must denote a unique entity  within that namespace . Because different namespaces have different scopes, it is possible to have members with the same name in different namespaces .
 Names defined in a namespace can be directly accessed by other members in the namespace, and can also be accessed by any unit in the nesting scope of these members. Code outside of that namespace must explicitly indicate which namespace the name used belongs to via scope qualifiers:: .

namespace wcy
{
    
    
	int a = 10;
}

int a = 30;

int main()
{
    
    
	int a = 20;
	printf("%d\n", a);
	printf("%d\n", ::a);
	printf("%d\n", wcy::a);
	return 0;
}

insert image description here
 The above code defines a variable in the local domain, the global domain, and the wcy namespace domain aand assigns different values. When the scope is not specified, the local variable will be accessed according to the principle of local priority; ::anothing on the left None means to search in the global domain; wcy::ait specifies to wcysearch in this domain.

3.4 How to use the namespace

Namespaces can be used in the following three ways:

  • Add namespace name and scope qualifier
int main()
{
    
    
    printf("%d\n", wcy::a);
    return 0;    
}
  • Use using to introduce a member in the namespace
using wcy::b;
int main()
{
    
    
    printf("%d\n", wcy::a);
    printf("%d\n", b);
    return 0;    
}
  • Use the using namespace namespace name to import

using namespace wcyNamespace expansion  can be used to promote the members of the namespace to include the namespace itself and using指示the nearest scope.

namespace wcy
{
    
    
	int a = 10;
	int b = 5;
	int c = 100;
}

int a = 30;

using namespace wcy;

int main()
{
    
    
	printf("%d\n", a);//a不明确,出现二义性
	printf("%d\n", ::a);//正确:访问全局的a
	printf("%d\n", wcy::a);//正确:访问wcy中的a
	printf("%d\n", b);//正确,去访问wcy中的b
	int c = 89;//当前局部变量的c隐藏了wcy::c
	c++;//当前局部的c设置成90
	return 0;
}

 Taking the above code as an example, by usingexpanding wcythe namespace, this process is equivalent to wcy"adding" the names in to the global scope, which allows the program to directly access wcyall the names in.
 When a namespace is injected into its outer scope, it is likely that names defined in the namespace will conflict with members in its outer scope. For example, in the main function, wcythe members of the conflict awith the members of the global scope . This kind of conflict is allowed , but to use conflicting names, we must explicitly indicate the version of the name . Anything unqualified in a function produces an ambiguity error.  In order to use a name like this, we have to use the scope operator to explicitly indicate the desired version. We use to indicate the global scope , and use to indicate the defined in . Because the scope of main is different from the scope of namespaces, declarations inside main can hide the names of certain members in the namespace . For example, local variables hide members of a namespace . There is no ambiguity in using it in main , it refers to the local variable c.amaina
a::aawcy::awcya
cwcy::cc

3.5 Nested Namespaces

 A nested namespace is also a nested scope, which is nested within the scope of the outer namespace. Names in nested namespaces follow the same rules as usual: a name declared in an inner namespace hides a member of the same name declared in an outer namespace. The name defined in the nested namespace is only valid in the inner namespace, and the code in the outer namespace wants to access it must add a qualifier before the name.

namespace wcy
{
    
    
	int a = 10;
	namespace wcy1
	{
    
    
		int a = 900;//将外层作用域的a隐藏了
		int d = 1000;
	}
	//int b = d;//不正确:d是未声明的标识符
	int b = wcy1::d;//正确访问的是wcy1里面的d

	namespace wcy2
	{
    
    
		int f = wcy1::d;//正确
	}
}

int main()
{
    
    
	printf("%d\n", wcy::wcy2::f);
	printf("%d\n", wcy::wcy1::a);
	printf("%d\n", wcy::a);
	return 0;
}

3.6 Namespaces of the C++ Standard Library

stdIt is the namespace of the C++ standard library, and C++ puts the definition and implementation of the standard library in this namespace.
Conventions for using the std namespace:

  • In daily practice, it is recommended to use it directly using namespace std, which is very convenient.
  • In project development, due to the large number of codes and large scale, conflicts are prone to occur, so it is recommended to specify a namespace to access or use usingcommonly used names to import.

Four, C++ input and output

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

Code description:

  • When using the cout standard output object (the console) and the cin standard input object (the keyboard) , you must include the header file and use std by namespace usage.
  • cout and cin are global stream objects , and endl is a special C++ symbol that represents newline output, and they are all included in the header file.
  • <<is the stream insertion operator and >>is the stream extraction operator.
  • It is more convenient to use C++ input and output, and does not need to manually control the format like printf and scanf input and output. The input and output of C++ can automatically identify variable types (essentially operator overloading), but the efficiency of cout and cin is relatively low.
  • In fact, cout and cin are objects of type ostream and istream respectively. >> and << also involve operator overloading and other knowledge, which I will share with you one by one later.

Note:
 In the early standard library, all functions were implemented in the global domain, declared in the header file with the suffix of std. File distinction, and in order to use the namespace correctly, it is stipulated that the header file of C++ does not contain .h. Some old compilers still support the <iostream.h> format, and now some new compilers no longer support it, so it is recommended to use <iostream> with std .


 Today's sharing is over here! If you think the article is not bad, you can support it three times in a row . Your support is the driving force for Chunren to move forward!
insert image description here

Guess you like

Origin blog.csdn.net/weixin_63115236/article/details/131467725