C + + enhancements to C

Keywords: C C++

Header file name

In the new style of C + +, the expanded name of header file is cancelled


C language is a process oriented programming language.

The core of process oriented programming is: function decomposition, top-down, layer by layer decomposition and layer by layer refinement.

Program = data structure + algorithm

C + + adds object-oriented features.

Object oriented programming (OOP).

Object oriented has three characteristics: encapsulation, inheritance and polymorphism

Encapsulation: abstract the objective transaction into a class (package the data and methods together and distinguish the permissions to achieve protection
And the purpose of safe use of the data)

Inheritance: inheritance expresses the relationship between classes, which enables an object to inherit the characteristics of another class of objects
And ability

The purpose of inheritance is to avoid repeated development of common code and reduce code and data redundancy

Polymorphism: polymorphism can be simply summarized as "one interface, multiple methods", which literally means multiple forms. It is the core concept in the field of object-oriented programming

Namespace and scope operators

Scope operators are used to indicate the attribution of data and functions

The scope operator is just two colons


#include <iostream>

int a ;

int main(){
    int a = 10;
    std::cout<<a<<std::endl;//The operator without scope defaults to local a
    std::cout<<::a<<std::endl;//Plus the scope operator, the global a is used by default
    return 0;

namespace is used to solve the conflict between function names and variable names in C language

Namespace points to note:

1. Definition of namespace

 //Define A namespace named A (variables, functions)
 namespace A {
  int a = 100;
 namespace B {
  int a = 200;
 void test02()
  //A: : a belongs to a
  cout<<"A in a = "<<A::a<<endl;//100
  cout<<"B in a = "<<B::a<<endl;//200

2. Namespaces can only be defined globally

Namespace data and functions are still global scope. Even nested namespaces, their internal data and functions are still global

This is wrong

3. The namespace can be nested

You can also nest namespaces within namespaces

 namespace A {
  int a = 1000;
  namespace B {
  int a = 2000;
 void test03()
  cout<<"A Medium a = "<<A::a<<endl; //1000
  cout<<"B Medium a = "<<A::B::a<<endl; //2000

4. Multiple namespaces with the same name are allowed in the same project, and the compiler will finally synthesize them into the same namespace

In other words, if we write a namespace in every place in a project and use the namespace with this name later, the compiler will merge all the namespaces with the same name

 namespace A {
  int a = 100;
  int b = 200;
 //Add c to the existing namespace A
 namespace A {
  int c = 300;
 void test04()
  cout<<"A in a = "<<A::a<<endl;//100
  cout<<"A in c = "<<A::c<<endl;//200

5. The namespace can store variables and functions

namespace A {
  int a=100;//variable

  void func()//function
  cout<<"func ergodic a = "<<a<<endl;
 void test05()
  //Use of variables
  cout<<"A Medium a = "<<A::a<<endl;

  //Use of functions

6. Functions in the namespace can be defined outside the "namespace"

In the same namespace, no matter whether the function is defined internally or externally, when the function uses the data of the same namespace internally, there is no need to add the scope. Of course, there is no mistake in adding it, but when it needs to access the data or functions of other namespaces, it is necessary to add the scope operator

1 namespace A {
2  int a=100;//variable
4  void func();
5 }
7 void A::func()//Remember to add scope when defining member functions externally
8 {
9  //No scope is required to access the data of the namespace
10  cout<<"func ergodic a = "<<a<<endl;
11 }
13 void funb()//Ordinary function
14 {
15  cout<<"funb ergodic a = "<<A::a<<endl;
16 }
17 void test06()
18 {
19  A::func();
20  funb();
21 }

7. Nameless namespace means that the identifier in the namespace can only be accessed in this file, which is equivalent to giving this identifier
The symbol is added with static so that it can be used as an internal connection

Namespace can be used directly without adding a name. At this time, the data and functions inside this nameless namespace are no different from ordinary global data and functions. You can call it directly with a name

If an ordinary global variable or function has the same name as a variable or function in the nameless namespace, it will conflict again when calling. At this time, there is no way to call the data in the nameless namespace, but we can add two colons in front of the global data or function, that is, the scope operator

However, ordinary global variables or functions are external link attributes by default, but variables or functions inside nameless namespaces are internal link attributes

void f(){
    std::cout<<"Global namespace"<<std::endl;

    void f(){
        std::cout<<"Nameless namespace namespace"<<std::endl;

int main(){
    return 0;

8. The namespace can be aliased

namespace new name = old name

 namespace veryLongName{

 int a = 10;
 void func(){ cout << "hello namespace" << endl; }


 void test(){
  namespace shortName = veryLongName;
  cout << "veryLongName::a : " << shortName::a << endl;

Use of namespaces

There are three ways to use data and functions within a namespace

  1. using namespace namespace name

Using this method is to expand all the namespaces, and there is no need to add scope operators for later use

However, if it is expanded directly in the function, it will easily conflict with the global, not with the local, and the local takes precedence

1 namespace veryLongName {
2  int a=100;
3  void func(){cout<<"hello namespace"<<endl;}
4 }
5 void test07()
6 {
8  //Use veryLongName namespace
9  using namespace veryLongName;
11  //The variable that appears cannot be found from the veryLongName namespace from elsewhere
12  cout<<"a = "<<a<<endl;
13  func();
14 }
  1. using to bring in members from a namespace
using N::b;
int main()
printf("%d\n", N::a);
printf("%d\n", b);
return 0;

However, this method will conflict with data or functions with local names, that is, if there are variables or functions with the same name in the namespace and local names, this expansion method cannot be used

However, it will not conflict with the global. In this case, the namespace takes precedence over the global

  1. using declaration encountered function overload
 namespace A {
  //Function overload function name + parameter combination represents the entry address of the function
  void func(){cout<<" Nonparametric func"<<endl;}
  void func(int a){cout<<" int of func"<<endl;}
  void func(int a,int b){cout<<" int int of func"<<endl;}

 void test08()
  //Using indicates that using func in A will work on all FuncS
  using A::func;

Standard namespace

Standard namespace, std

 #include <iostream>
 //Use standard namespace std
 //All member names in std can be used directly
 //cout endl cin are all members of namespace std
 using namespace std;

 int main(int argc, char *argv[])
  std::cout << "Hello World!" << std::endl;
  cout << "Hello World!" << endl;
  return 0;

Posted by fleabay on Sun, 05 Dec 2021 02:33:23 -0800