Part 3 category and data abstraction[Chapter 1 category]The basic idea behind classes is data abstraction and encapsulation. If a function is a const, the const must be specified in the declaration and definition of the function. Otherwise, an error occurs during compilation. The default function defined in the class is inline. The definition of the inline function must be visible in each source file that calls the function. Therefore, if the inline function is defined outside the class, then the function definition and class declaration must be placed in the same file. A function is inline. The description and definition of this function only need to be specified once in inline. You do not need to specify the description and definition as in Const. You can declare only one class without defining it, such as class screen. This method is called Forward Declaration. The declared class is of an incomplete type (incomplete type ), that is, you only know that this is a type, but you do not know which Members it contains. Incomplete types can only be used in a limited way: objects of this type cannot be defined; pointers or references to this type can be defined; this type can be used as a function parameter type or return value type. Function overload can be implemented based on whether the member function is const. You can also overload the function based on whether the pointer parameter (or the referenced parameter) points to const. However, if the parameter is not a non-referenced common const type, you cannot perform overload. If the member function is non-const, This is a const pointer to the class type. If the member function is const, This is a const pointer to the const class object. Const object cannot be const. When creating a const object, run the common const function to initialize the const object. The initialization list of the constructor can only appear in the function definition and cannot appear in the Declaration. An initialization can be any complex expression. Type objects, const types, and reference types without Default constructors must be initialized in the constructor initialization list. The const type and reference type can only be initialized in the initialization list. The initialization sequence of the members in the constructor initialization list is the defined sequence of variables, rather than the order in which the variables appear in the constructor initialization list. If no constructor is explicitly defined, the compiler generates a default constructor. If the constructor is explicitly defined, the compiler does not define the default constructor. In this case, it is best to explicitly define a default constructor. Otherwise, the class cannot be initialized by default. All the places where the class is used must be displayed and all its members cannot be initialized, and the objects of the class cannot be dynamically allocated. Therefore, if other constructors are defined, it is better to define another default constructor. The friend statement does not allow users to be controlled by access delimiters such as public and private. Static members are controlled by these access controllers. Static members can be accessed in a variety of ways: the class uses the scope OPERATOR: access, access through an object, access through a reference or pointer to this type of object. The static member function does not have the this pointer. It can directly access the static member of this class. However, when accessing non-static members, it must be accessed in the form of [Object Name. member name. Static member functions cannot be virtual functions. Static member functions cannot be const, but static data members can be const. Static data members must be initialized only once outside the class definition. Static data members can be used as default real parameters, but not static data members. The static data member type can be the class type to which the type belongs. The normal non-static member type cannot be its class type, unless the data member is a pointer or reference.
[Chapter 1 copy control]Copy constructor, value assignment operator, and destructor are collectively referred to as copy control ). Generally, the compiler automatically merges value assignment control members. When a class has pointer members, we must define the copy Control Members ourselves. Copy constructor: there is only one form parameter, and this form parameter is a reference to this class type object (usually const ). When the parameter is not of the reference type, the copy constructor replicates the real parameter. When the non-reference type is returned, the copy constructor replicates the parameter. When no explicit replication constructor is defined, the compiler will synthesize (synthesize) a replication constructor, which executes Member-by-member initialization, copy each non-static member of an existing object to the object being created. The value of the built-in type member is directly copied, and the class type uses the copy constructor of this class. If an array exists, each element of the array is also copied. If you want to disable replication, you should specify the replication constructor of the class as private. At this time, the friends and members of the class can still be copied. To prohibit the copying of friends and members, the copy constructor can be declared as private but not defined. If no value assignment operator is defined, the compiler will combine them into one. The synthesis assignment operator is the same as the synthesis copy constructor. assign values to members one by one. If there are several group members, assign values to each array element. Unlike the copy constructor and the value assignment operator, the compiler will synthesize a destructor whether or not the Destructor is explicitly defined, which revokes the members in reverse order declared by the members in the class. If you explicitly define a destructor, first run the explicitly defined destructor, and then run the merged destructor. Rule of three: If you need to explicitly define the destructor, you also need the value assignment operator and the copy constructor. C ++ uses one of the following methods to manage pointer members:
(1) pointer members adopt regular pointer-type behaviors. pointers of different objects in the same class point to the same piece of memory, which can easily lead to suspension of pointers. Such a class has all the defects of pointers. (2) Use smart pointers. Pointers are also shared memory, but they can prevent suspension pointers. (3) Take a value-type behavior. Memory units are not shared, and each Object Pointer Points to different memory units.
[Chapter 2 heavy-duty operators and conversions]The function call Operator () can accept any number of operands. The number of parameters of all other operators (including the implicit this pointer of the member function) must be equal to the number of operations of the operator. The overload operator must have at least one class type or enumeration type operand. You cannot redefine the operators used for built-in object types. The operator's priority, associativity, and number of operands cannot be changed. Besides the function call Operator (), the default real parameters used by other overload operators are invalid. The order in which the operands are evaluated is not guaranteed by the overload operator, especially the overload &, | and comma operator. Both operands must be evaluated, but the order in which the operands are evaluated is not specified. Therefore, overloading &, | and comma operators are not a good practice. Guiding principles for defining an overloaded operator function as a member function: (1) Generally, arithmetic and Relational operators are defined as non-member functions, while value assignment operators are defined as member functions of the class; (2) input>, output <, value =, subscript [], function call (), member access->, type conversion functions, and other operators must be defined as members, defining these operators as non-member functions will cause compilation failure. Composite value assignment operators such as + = can be defined as non-member functions. compilation does not report errors, but they are usually defined as member functions. (3) other operators that change the object state or closely associate with a given type, such as auto-increment, auto-increment, and reference, are usually defined as class member functions; (4) symmetric operators, for example, Arithmetic Operators, Relational operators, and bit operators, it is best to define them as common non-member functions. Operator <function header is usually: ostream & operator <(ostream & OS, const classtype & object); operator> function header is usually: istream & operator> (istream & OS, classtype & object); Value assignment operator = should return the reference of the left operand. As a member function, * This is returned. The results of the subscript operation [] may be left or right, so the reference type is usually returned. [] Must be defined as a member function. Generally, two overload functions are defined: one is non-const type and normal reference is returned; the other is const type and const reference is returned. The arrow operator> must be defined as a member function, and the returned value must be a pointer to the class type or a class object that defines its own arrow operator. The unreferenced operator * can be a non-member function, but is usually defined as a member function. The quote operator * is similar. The auto-increment operator ++ is usually defined as a class member. The prefix auto-increment operator should return a reference. The function header is usually classtype & operator ++ (); the suffix auto-increment operator should return values instead of references, and should also contain an int parameter. The purpose of this parameter is to distinguish between prefix and suffix, which is not applicable within the function, the compiler provides the initial value 0. The function header is usually classtype operator ++ (INT). The auto-subtraction operator is similar to the auto-increment operator. The call operator () should be defined as a member function, which reloads the class of the call operator. Its objects are usually called function objects, that is, they are objects that act like functions. The standard library defines a set of arithmetic, relational, and logical function object classes in the functional header file, such as plus <type>, 1__to <type>, and logical_or <type>. The standard library also defines a set of function adapters for special and extended mona1 and binary function objects. These function adapters include bind1st, bind2nd, and not1 and not2. Use count_if (VEC. begin (), VEC. end (), not1 (bind2nd (less_equal <int> (), 10 ))); the constructor that accepts a single parameter and does not specify it as explicit defines the conversion from other types to class types. The conversion operator defines the conversion of class type values to other types. The conversion function must be a member function. The input parameter table is empty and the return type cannot be specified. However, each conversion function must return a value of the specified type. The first part of the Conversion Function is usually operator type (); type is the target type to be converted, and the function should return the value of type. For example, class smallint {public:... operator int () const {return val;} PRIVATE: STD: size_t val ;...}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service