The namespace

Define the namespace: the namespace is followed by the name of the namespace

namespace test {
 void func(a){
      std: cout << "test" << endl; }}Copy the code

Calls properties or methods in the namespace

test::func();  
Copy the code
  • Using the instructions

Use the using namespace directive so that you don’t have to precede the namespace name when using it

using namespace test;

int main (a)
{
 
   // Call a function in the test namespace
   func();
   
   return 0;
}
Copy the code

If you intend to use only the Cout part of the STD namespace, you can use the following statement

using std: :cout;
Copy the code

Nested namespaces

namespace namespace_name1 {
   // Code declaration
   namespace namespace_name2 {
      // Code declaration}}// Access the member in namespace_name2
using namespace namespace_name1::namespace_name2;
// Access the members of namespace:name1
using namespace namespace_name1;

Copy the code

Reference vs pointer

References are declared by an & and Pointers by an *

int i = 10;
/ / reference
int&  r = i;
/ / pointer
int * p = &i;
Copy the code
  • Pointers can be null Pointers, and references do not have null references.
  • A pointer can point to another object at any time, while a reference, once initialized to an object, cannot be pointed to another object.
  • Pointers can be initialized at any time; references must be initialized at creation time.

Memory of the four-wheel drive model

model role
Stack storeThe parameter values of the function, the local variable values, the compiler automatically allocates the release
The heap area (heap) New,malloc allocated memory block, the developer manually allocated free
The global area Static global area (virtual, global, and static variables)Constant area (constant string), the program is released by the system
Code section Stores the binary code of the function body

Pointer in the stack area, constant in the constant area, malloc new in the heap area need to be manually freed memory


char* methodA(a){
    char* pa = "123456";// The pa pointer is in the stack area and "123456" is in the constant area. The pointer variable pa is released after the call
    char* p = NULL;     // The pointer variable p allocates 4 bytes in the stack
    p=(char*)malloc(100);// Create a memory space in the heap and assign the address to p
    strcpy(p, "1234567");// Copy the string from the constant to the heap
    return p;// Return to the calling function methodB(). Relative to methodA,methodB is the calling function. Relative to main, methodA() and methodB() are both called functions
}

char* methodB(a){
    char*pstr = NULL;
    pstr = methodA();
    return pstr;// This is where the pointer variable PSTR ends up being reclaimed
}

int  main(a){ 
   char*str = NULL;
   str = methodB();
   printf("str = %s\n",str);
   free(str);    // To prevent memory leaks, the value of memory allocated by the called function methodA() is passed to the calling function through the return value, and then the calling function frees the memory
   str = NULL;// Prevent wild Pointers
   return 0;
}
Copy the code

New To create an object, delete memory

#include <iostream> 
using namespace std; 
// Define A class named A
class A 
{ 
private: 
    int n; 
public: 
    A(int m):n(m) 
    { }
    // The destructor is called at the time of collection
    ~A(){} 
}; 
int main(a) 
{ 
    A a(1);  // Allocate on the stack
    A b = A(1);  // Allocate on the stack
    A* c = new A(1);  // Allocate in the heap
  delete c; //new Delete the created object when it is used up
    return 0; 
} 
Copy the code

object-oriented

Class definitions begin with class, followed by the name of the class. Virtual functions start with virtual and can determine the object to which the pointer points at run time and automatically call the corresponding function. A pointer object requires -> to call properties and associated methods

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

class People {
    public :
     // The most important feature of virtual functions is "dynamic linking". It can determine the object to which the pointer points at runtime and automatically call the corresponding function.
    virtual void eat(a){};static int age;
    People(){
    // initialize age++ each time
        age++;
    }
    char * name ;
    string sex ;
    void printName(a);
};
// The range resolution operator :: initializes the value of age
int People::age = 18;

void People::printName(a){
    printf(PrintName method call, name= %s\n",name);
}

int main(a){  
 People p;
 People *p1 =new People();
 p.name="Zhang";
 p1->name="Bill";
 p1->printName();
 printf("p.name = %s \n",p.name);
 printf("p1.name = %s \n",p1->name);
 printf("p.age = %d \n",p.age);
 printf("p1.age = %d \n",p1->age);
 return 0; } output: printName method call, name= li si p.name = Zhang SAN P1. Name = Li Si p.age =20 
p1.age = 20 

Copy the code