1. Data abstraction and Encapsulation
Data abstraction is a programming technology that separates interfaces and implementations. Designers are concerned about how to implement these interfaces, and users only know these interfaces, abstract, consider what these interfaces do. You don't have to consider how to implement this layer.
Encapsulation combines low-level elements to Form high-level entities. For example, a function is a form of encapsulation. A function itself can be seen as a large entity, the Implementation Details executed in the function are encapsulated, so the encapsulated elements hide their implementation details. You can call this function to execute the function encapsulation function, however, you cannot directly access the statements executed by the function. Similarly, a class is an encapsulated entity. A class is a large collection of many Members, hiding the members that implement the class.
When a vector of the standard library type is used, only the operations that can be performed are considered, that is, the value takes into account the interface, so it has abstract features, at the same time, external users cannot understand and access the details of this type, so they have encapsulation features. However, the array is different. You can operate the array directly through the memory of the number of arrays. Therefore, although the array definition is similar to the vector, the array does not have the Abstract Feature, it does not have encapsulation features.
2. copy constructor and default constructor of Classes
Whether the replication constructor of the definition class needs to be displayed in the class, and whether it is equivalent to copying all data members of the object during object replication of the class. If not, you need to display the definition replication constructor. Because the implicit replication constructor replicates data by value, after the implicit replication of data members is completed, the data members of the two objects will point to the same memory address, at the time of destruction, it will be released twice.
Class string
{
Public:
String ();
String (const char * str );
~ String ();
Private:
Char * data;
}
Therefore, when there are pointer members in a class, the replication constructor must be defined.
3. overload should be considered to avoid implicit type conversion
For example, in the String type String, class String {...}, You need to compare,
Bool operator = (const String &, const String &)
{
// The following statement exists somewhere in the code
If (caseOfString = "mingzi ")
{...}
}
Obviously, "the program is equivalent to if (caseOfString = String (" mingzi "), but we only want to read" mingzi "without the need to copy it. Therefore, you must use overload to avoid conversion, as shown in figure
Bool operator = (const String & str, const char * str1), bool operator = (const char * str1, const String & str ).