The following procedure:
# Include <iostream>
# Include <string>
Using namespace STD;
Class student
{
Public:
Static int number;
String name;
Public:
Student ()
{
}
Void set (string Str)
{
Name = STR;
Number ++; // call a static data member
}
Void print () // State member function print ()
{
STD: cout <name <": The number of the students is" <number <"numbers." <STD: Endl; // call a static data member
}
};
Int Student: Number = 0; // static data member Initialization
Int main (INT argc, char ** argv)
{
Student * S1;
S1 = new student ();
S1-> set ("111 ");
Student S2;
S2.set ("222 ");
S1-> Print ();
S2.print ();
Return 0;
}
For student classes, an object and a pointer are defined.
Class pointer: it is a memory address value that points to class objects stored in the memory (including values assigned by some member variables ).
The class constructor is used to allocate a piece of memory in the memory (including the values assigned by some member variables ).
Application Time:
1. Reference members: the object uses the "." operator; the pointer uses the "->" operator.
2. life cycle: if it is a member variable, it is the class destructor to release space; if it is a temporary variable in the function, the scope is the function body. the pointer needs to use Delete to release the allocated memory block in the corresponding place.
Note: to use new, you must delete ..
Class Object: uses the memory stack and is a local temporary variable.
Class pointer: uses a memory heap, which is a permanent variable unless you release it.
When a class is a base class with virtual functions and func is a virtual function, call FUNC:
Class Object: calls its own func;
Class pointer: calls the func class that is assigned to it space;
What are the differences between a class object and the class pointer (memory allocation using the new operator) in the application?
1. Classes and objects are two different things. objects are class instances;
2. objects are allocated in the stack, and objects generated using new are allocated in the heap;
3. to make full use of virtual functions, you must use pointers to access objects.
Pointers can achieve polymorphism, but objects cannot be used directly.
Execute the definition object in the stack space
Heap at New
Note the name type.
One is student.
One is student *
Student is a direct access to an object
Student * is used to indirectly access an object because a pointer is used as a media.
The type determines what you can do.
In fact, basically the same function is used to call the member variables and member functions of the class.
When you want to explicitly use this class, it is best to use objects. If you want to use dynamic binding in C ++, it is best to use pointers or references.
Pointers and references are more flexible to use and can easily implement polymorphism.
Class pointer: it is a memory address value that points to class objects stored in the memory (including values assigned by some member variables ).
The class constructor is used to allocate a piece of memory in the memory (including the values assigned by some member variables ).
Application Time:
1. Reference members: the object uses the "." operator; the pointer uses the "->" operator.
2. life cycle: if it is a member variable, it is the class destructor to release space; if it is a temporary variable in the function, the scope is the function body. the pointer needs to use Delete to release the allocated memory block in the corresponding place.
Note: to use new, you must delete ..
When a class is a base class with virtual functions and F is a virtual function, call F:
Class Object: calls its own F;
Class pointer: calls the Class F when the space is allocated to it;
1. When the class declaration has not been completed, you can declare a pointer to the class, but cannot declare the class object...
2. the pointer to the parent class can point to the subclass object ..
Memory is allocated when an object instance is defined. The pointer variable does not allocate the memory required for the class object.
The pointer variable is indirectly accessed, but can realize polymorphism (The subclass object can be called through the parent class pointer) without calling the constructor.
Direct declaration can be accessed directly, but cannot implement polymorphism. The declaration calls the constructor (allocated memory ).
The high efficiency depends on the program call process.
One of the essence of C ++ is polymorphism. Only pointers or references can achieve polymorphism. No object
Pointer:
First, implement polymorphism.
Second, in the function call, pass the pointer parameter. No matter how large your object or structure parameters are, you can use a pointer to transmit four bytes in the past. If an object is used, passing parameters will occupy too much resources.