C++primer (Fourth edition) Review notes-Article III: Classes and Data abstraction

Source: Internet
Author: User

Data Abstraction : refers to the ability to define data and function members;
Encapsulation : refers to the ability to protect class members from regular access.
interface : The member function defines the interface of the class. Encapsulates a class by setting the dimension to private for the data and member functions that define the class.

The 12th chapter: Class

1. The initialization of the constructor is only indicated in the definition of the constructor, not in the declaration.
2. The difference between an initialization list using a constructor and a member variable of a class in the constructor body is essentially that the former is initializing the variable, while the latter assigns the variable.
Understanding: The execution of a constructor is divided into two phases: the first initialization phase, and then the normal computing phase.
During the initialization phase, the constructor invokes the constructor of the class type (which is called by default when the list is not initialized) to initialize the member variables of the class type, executes the statement of the constructor body during the normal calculation phase, and therefore assigns a value to the member variable, overwriting the initial value of the initialization phase. For built-in types, depending on the location defined by the object, the initialization stage is different (for global objects, the built-in type is initialized to 0, the local variable is not initialized), and for those const object members, reference members, class types without a default constructor, the default constructor is called. But it does not), you must provide an initialization list for display initialization.
3, it is recommended that the constructor use the initialization list: first, the efficiency: eliminate the normal calculation phase of the copy process, and two: For those const object members, reference members, no default constructor of the class type (otherwise call its default constructor, but it does not), etc., you must provide an initialization list for display initialization.
4. Note: The order in which members are initialized is the same as the order in which members are defined (rather than in the order in which they appear in the initialization list), so you should try to give the initialization list in the order defined, and notice the problem with one member when initializing other members.
5. A class will automatically generate a composition's default constructor only if no constructors are defined. Initializes the class type by calling its default constructor, whereas for built-in types, initialization occurs when the object definition is global and not in the local object.
6. If a class includes members such as built-in or composite types (pointer references), you should not rely on the default constructor of the composition, and you should define your own constructors to initialize those variables.
7, a class should typically define a default constructor, and in the default constructor give the member the initial should be "empty" when the object is indicated.
8. The constructor that provides the default arguments for all the parameters also defines the default constructor.
9. The default constructor is used: Define an object using the default constructor of Class A:
A ();//This is wrong because it is interpreted by the compiler to define a function that returns an object a with null parameters. Therefore, the default constructor cannot be followed by parentheses (the non-default constructor with parameters can be enclosed in parentheses, and the argument value is given in parentheses). But this usage is right: a a=a ();//Right invokes the default constructor to create an object and initializes object A with that object.
10. Implicit class-type conversions: constructors that are invoked with a single argument define an implicit conversion from the parameter type to the class type. Thus, a type of the parameter can be passed where a class type is required, so that the compiler invokes the constructor and constructs a temporary object of the desired class type with the argument as the arguments. But note: This implicit conversion is what we need! Otherwise it should be avoided.
11. Suppress implicit conversions defined by constructors: Declare constructors as explict to prevent constructors from being used as an implicit type conversion: precede the constructor declaration with the keyword explicit (no more explicit on the body of the constructor definition outside the class). At this point the compiler does not stop using constructors as a type conversion (the compiler will error).
12. Use constructors for conversions: any constructor (including default) can display the creation of temporary objects, that is, the class name is immediately followed by parentheses, the parentheses are given to call the constructor parameters, and do not give the object name. (such as: a ();//Create an unnamed temporary object).
13. Normally, unless implicit conversions are required for explicit reasons, the constructor of a single argument should be preceded by a explict to avoid errors. When a type conversion is required, the create temporary object that is displayed using the constructor is available for display.
13. Display initialization of class members: When no constructor is defined and all data members are common, all data members can be initialized in the same way as the array elements are initialized
Such as:struct A { int i; int* p};
A a={0,0};//a.i=0; a.p=o
; (initialized based on the order in which data members are declared)
14. The friend mechanism allows a class to grant access to its non-shared members to the specified function or class. Friend can only appear inside the class. Friend declarations can be anywhere in a class: Friends are not members of the class, so they are not affected by the access control part of their declarations.
15. Static class Member: is part of a class, not part of an object (common to all objects of a class, so it can be used to pass information between all objects of a class, such as how many objects of that class are created). Static members, which are specified only when declared in a class, do not have to be repeatedly specified as static when defined inside and outside.
16. Static data member: exists in each object of the class type and is independent of a particular object of the class, associated with the class, not an object of that class. A static data member must be defined outside the class (exactly once), because it is not initialized by the constructor of the class, but rather when it is defined. Static data members are often defined in the definition file of a class's non-inline function (no repetition is specified as static), and as defined by the generic member function, you need to specify the fully qualified name of the variable agree after the type, to specify which class to belong to, such as:
int a::static_member=10; (Note that as soon as the full-qualified name appears, the content behind it is in the scope of the class.) Special: to cosnt static int data member, as long as its initializer is a constant expression, it can be initialized directly in the class (but other types of cosnt static data members, or need to be defined outside the class initialization):

class A{private:  staticconstint a =30//直接在类定义体中进行用常量表达式初始化staticconststring str;//不能在类中进行初始化。};

Static function member: Without this parameter, you can directly access the static members of the owning class, but not the non-static members directly.
17, static members follow the normal access rights. The static member function cannot be declared as a const function (because the const member function is not to modify its object) and cannot be declared as a virtual function. A static data member can be defined as any data type.
18, static members can be referred to as other ordinary members of the object, you can also directly use the scope operator directly to the class call: A::static_member. (Static members are non-object-owned and therefore can be used independently of the object). For example, the type of a static data member can be its class type.

13th Chapter: Copy Control

1, constructors and copy-control members (copy constructors, assignment operator functions, destructors) cannot be inherited, but these functions of the base class can be called in derived classes and cannot be defined as virtual functions (except destructors) (Assignment operator functions: because they cannot be inherited, the derived class must have its own version, Therefore, the definition of a virtual function is meaningless; a constructor: Run before an object is constructed, where the dynamic type of the object is not complete, so the definition is meaningless for the virtual function. Each class defines its own constructors and replication control members, and uses the composite version if it is not defined. If you define a constructor or copy control member yourself in a derived class, if the constructor or copy control member that calls the direct base class is not displayed, the default version of the base class is used to construct or copy the base class part (the constructor of the derived class can be indicated in the initialization list, The assignment operator can be displayed in the function body to call the base class version base::operator= (RHS) to complete the base class section). Note: You need to check for self-assignment in the assignment operator:

Drivied& Drivied::operator=(Derivied& rhs){    if(*this!=rhs){     base::operator=(rhs);  //赋值基类部分    //继续复制派生类自己定义的成员    }    Return *this;}

2. Contains only class types, built-in type classes can not define their own constructors and copy control members and destructors, and if there are pointer members, no longer dependent on the composite version.
3. Dynamic binding only takes place in a reference or pointer to a base class (which can be bound to a base class or derived class object) when a virtual function is called. A derived class can be used to assign a value to a base class or to construct a base class (an assignment constructor for a base class) because the parameters of the copy constructor and assignment operator are const references to the base class, so that a derived class may be bound to a pseudo-cut of the derived class object, which discards the derived custom member part. Note: Although a base class pointer or reference can be bound to a derived class object (which is actually the base class part), you can access only the base class part of the derived class object
4. The derived class destructor is not responsible for revoking the members of the base class object. The compiler always displays destructors that call the base class part of the derived class object. Each destructor is only responsible for clearing its own members. The undo order of an object is the opposite of construction: the destructor for the derived class is run first, and then the destructor for each base class is called up.
5, Virtual destructor: The destructor can be defined as virtual function, and the virtual property can be inherited (but the destructor itself cannot be inherited). When a base class pointer is dynamically refactored, the destructor of the base class is automatically selected based on the dynamic type of the pointer or the derived class destructor (because the destructor is declared as a virtual function). So even if the destructor of the base class does not need to do anything, you need to define a virtual destructor.
6. During the construction and destruction of the derived class object, the type of the derived class object is variable: at the time of construction, its base class part is constructed, at this time the base class type, and at the time of the destructor, the part of its derived class is first refactored, and the base class is the base class type when the base class destructor is called.
7. Overloading, covering and hiding
1). Overloaded: "Overloaded" occurs when a member function has the following characteristics
A Same scope (in the same class)
B function has the same name
C parameter types are different (implicit type conversions cannot be performed)
D Virtual keyword is optional
2). Overwrite (also called "Inheritance"): Refers to a derived class function that overrides a base class function, characterized by:
A Different scopes (in base class and derived class, respectively)
B Function name is the same
C Same parameters
D The base class function must have the virtual keyword
3). Hidden: Refers to a function of a derived class that masks a base class function with its same name (parameter list, return type), as follows:
A If a function of a derived class has the same name as a function of a base class, but the arguments are different, the functions of the base class will be hidden regardless of the virtual keyword, so be careful not to confuse the overloads.
B If a function of a derived class has the same name as a function of the base class, and the parameters are the same, but the base class function does not have the virtual keyword, the function of the base class is hidden
A base class member that is hidden, derived class objects can be displayed by a base class name with a scope character that describes the member to invoke the base class at. A base-class version member that is hidden in a derived class exists, is only hidden, and cannot be called through a derived class object.
Therefore, the underlying cause of the hidden (different scope) rule is actually the C + + name resolution process (that is, the first member of the name is found in the derived class domain, and if it is stopped, it will continue to be looked up in the base class field if it is not.) The overloads must be
In the same scope, find the best match when looking for)
The call phenomenon caused by the overlay rule is actually generated by the virtual function implementation principle of the class.
8. A class that contains or inherits one or more pure virtual functions is an abstract base class. Abstract base classes cannot create objects of the abstract base class except as part of the abstract base class of their derived class objects.

C++primer (Fourth edition) Review notes-Article III: Classes and Data abstraction

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.