## introduction – C++ introduction

  • C++ is a superset of C. In fact, any legitimate C program is a legitimate C++ program.
  • C++ can be mixed with C code, C can be written in C++, but not the other way around.
  • C++ is an object-oriented programming language and C is procedural programming.
  • C++ an enhancement to C. C++ has the concept of class, reference, heap memory allocation in addition to C language malloc, free, and new, delete keywords.
  • C++ is a statically typed, compiled, general-purpose, case-sensitive, irregular programming language that supports procedural, object-oriented, and generic programming. Object-oriented programming

#####C++ fully supports object-oriented programming, including four features of object-oriented development:

  • encapsulation
  • abstract
  • inheritance
  • Polymorphic ###C++ namespace

#### Basic Concepts

Namespaces, also called namespaces, are similar to packages (categories) in Java. When the project is large, it is used to distinguish between code written by different people and code from different libraries. Function names, variable names, class names, and so on can be repeated under different namespaces.

Suppose there are two students named Zara in a class. In order to distinguish between them, we have to use some additional information, such as their home address, or their parents’ names, in addition to their names. The same applies to C++ applications. For example, you might write a function called xyz(), the same function xyz() that exists in another available library. Thus, the compiler cannot tell which XYZ () function you are using. Therefore, the concept of namespaces was introduced specifically to solve the above problem as additional information to distinguish functions, classes, variables, etc., with the same name in different libraries. Using a namespace defines a context. In essence, a namespace defines a scope.

#### Define a namespace The namespace is defined using the keyword namespace followed by the name of the namespace, as shown in the following figure:

Namespace namespace_name {// code declaration}Copy the code

To call a function or variable with a namespace, you need to prefix it with the name of the namespace, as follows:

name::code; // code can be a variable or a functionCopy the code

The instance

#include <iostream>using namespace std; // First namespace namespace first_space{voidfunc(){
      cout << "Inside first_space"<< endl; }} // Namespace second_space{voidfunc(){
      cout << "Inside second_space" << endl;
   }
}
 void main() {// call first_space::func(); // Call second_space::func(); system("pause"); } Inside first_space Inside second_spaceCopy the code

####using directive You can use the using namespace directive to use namespaces without having to prefix them with the name of the namespace. This instruction tells the compiler that subsequent code will use the name in the specified namespace.

#include <iostream>using namespace std; // First namespace namespace first_space{voidfunc(){
      cout << "Inside first_space"<< endl; }} // Namespace second_space{voidfunc(){
      cout << "Inside second_space" << endl;
   }
}
using namespace first_space;
void main() {// call the function func() in the first namespace; system("pause"); } Result output: Inside first_spaceCopy the code

The using directive can also be used to specify specific items in a namespace. For example, if you only intend to use the COUT part of the STD namespace, you could use the following statement:

using std::cout;
Copy the code

In subsequent code, you can use cOUT without prefixing the namespace name, but other items in the STD namespace still need to be prefixed with the namespace name, as follows:

#include <iostream>
using std::cout;
 
int main ()
{
 
   cout << "std::endl is used with std!" << std::endl;
   system("pause");
   return0; } STD ::endl is used with STD!Copy the code

Nested namespaces Namespaces can be nested, and you can define another namespace within one namespace, as follows:

Namespace namespace_name1 {// code declaration namespace namespace_name2 {// code declaration}}Copy the code

You can access members in a nested namespace by using the :: operator:

Using namespace namespace_name1::namespace_name2; Using namespace namespace_name1;Copy the code

In the above statement, if namespace_name1 is used, then elements in namespace_name2 are also available in that scope, as follows:

#include <iostream>using namespace std; // First namespace namespace first_space{voidfunc(){
      cout << "Inside first_space"<< endl; } // Second namespace namespace second_space{voidfunc(){
         cout << "Inside second_space" << endl;
      }
   }
}
using namespace first_space::second_space;
int main() {// call the function func() in the second namespace; system("pause");
   return0; } Result: Inside second_spaceCopy the code

Special thanks: rookie C++ tutorial