C + + Primer Plus reading notes-13th Chapter class succession

Source: Internet
Author: User

Chapter 13th Class Succession

1. If you are purchasing a C library from a vendor, you will not be able to extend or modify the function according to your needs unless the vendor provides the source code for the library function. However, if you are a class library, you can use the classes in the library to derive new classes as long as they provide the header and compiled code for the class method. And you can distribute your classes to other people without exposing them, while allowing them to add new features to the class.

2. The derived class constructor first creates the base class object, and if you do not call the base class constructor, the program uses the default base class constructor.

3. When creating a derived class object, the program first calls the base class constructor and then calls the derived class constructor. When a derived class object expires, the program calls the derived class destructor first, and then calls the base class destructor.

4. Derived class objects can use the methods of the base class, provided the method is not private.

5. A base-class pointer can point to a derived class object without an explicit type conversion, and a base class reference can reference a derived class object without an explicit type conversion. However, a base class pointer or reference can only be used to call a base class method and cannot call a derived class method.

This compatibility allows you to initialize a base class object with a derived class object, or you can assign a derived class object to a base class object.

6. Typically, C + + requires reference and pointer types to match the assigned type, but this rule is an exception to inheritance.

7. If you want to redefine a method of a base class in a derived class, you should typically declare a base class method as virtual virtual, and the program will select the method based on the type of object that the reference or pointer points to. If you do not use the keyword virtual, the program chooses the method based on the reference type or pointer type.

8. When a method is declared as virtual in a base class, it will automatically become a virtual method in the derived class. However, it is also a good idea to use the keyword virtual in a derived class declaration to indicate which functions are virtual functions.

9. The base class declares a virtual destructor. This is done to ensure that the destructor is called in the correct order when the derived class object is disposed.

10. The keyword virtual is used only in class declarations, not in method definitions.

11. Why do I need a virtual destructor?

If the destructor is not virtual, only the destructor corresponding to the pointer type is called, even if the pointer points to the derived class object. If the destructor is virtual, if the base-class pointer points to the derived class object, the destructor for the derived class object is called, and the destructor for the base class is automatically called. Therefore, the use of virtual destructors ensures that the correct destructor sequence is called.

12. The function call in the source code is interpreted as executing a specific function code block called the Function name Union (binding). In the C language, this is very simple, because each function name corresponds to a different function. In C + +, these tasks are more complex due to function overloading. The compilation process is called a static early, also known as an early-linking (binding). The compiler must generate code that can select the correct virtual method when the program runs, which is called the dynamic binding, or the late-linking (late binding).

In C + +, dynamic linking is related to invoking methods through pointers and references, and in a way, this is controlled by inheritance.

13. The compiler uses static linking for non-virtual methods, and dynamic linking for virtual methods.

14. Dynamic linking allows you to redefine a class method, why not set it as the default? There are two reasons--efficiency and conceptual models.

In order for the program to be able to make decisions at run-time, some methods must be taken to track the type of object that the base class pointer or reference points to, which adds additional processing overhead. Because the static binder is more efficient, it is set to the default choice for C + +.

When you design a class, you may include some member functions that are not redefined in the derived class. There are two benefits to not setting these functions to virtual functions. First, it's more efficient; second, it says don't redefine the function. This indicates that if you want to redefine the methods of the base class in the derived class, set them to virtual methods, otherwise, set to non-virtual methods.

15. How Virtual functions work P504

The compiler handles virtual functions by adding a hidden member to each object. A pointer to an array of function addresses is saved in the hidden member. This array is called a virtual function table. The compiler creates 1 virtual function tables for each class, shared for all objects of that class. Regardless of whether the virtual function contained in the class is one or 10, you only need to add 1 address members to the object, only the size of the table is different.

When a virtual function is called, the program looks at the address of the virtual function table stored in the object and then shifts to the corresponding function Address table. If you use the first virtual function defined in the class declaration, the program uses the first function address in the array and executes the function with that address. If you use the third virtual function in a declaration, the program uses a function that addresses the third element in the array.

In summary, using virtual functions has some cost in terms of memory and execution speed, including:

1) Each object will grow to increase the amount of space to store the address of the virtual function table;

2) For each class, the compiler creates a virtual function Address table (array);

3) For each function call, you need to perform an additional action to find the address in the table.

16. A constructor function cannot be a virtual function.

The base class should provide a virtual destructor, even if it does not require a destructor.

17. If the derived class does not redefine the virtual function, the base class version of the function is used.

18. Redefining the inherited method is not an overload, it hides the method. P506

19. If the function in the base class is overloaded, you should redefine all the base class versions in the derived class. Otherwise, versions that are not redefined are hidden, and derived class objects cannot use them.

    1. The difference between private and protected is only shown in classes derived from the base class. Members of a derived class can directly access the protected members of the base class, but not the private members of the base class directly. Therefore, for the outside world, the behavior of protecting members is similar to that of private members, but for derived classes, the behavior of protecting members is similar to that of public members.

21. It is best to use private access control for class data members, not to protect access control, and to enable derived classes to access base class data through base class methods.

However, for member functions, it is useful to include access control, which allows derived classes to access intrinsic functions that are not available to the public.

22. A class that contains at least one pure virtual function is called an abstract base class.

C + + provides an unassigned function by using a pure virtual function, and the end of a pure virtual function declaration is = 0, see the area () method.

When a class declaration contains a pure virtual function, you cannot create an object of that class.

23. In summary, using =0 in a prototype indicates that the class is an abstract base class that can be defined without defining the function in the class.

24. ABC can be seen as an interface that must be implemented. ABC requires that a specific derived class override its pure virtual function-forcing a derived class to follow an excuse rule set by ABC.

25. If the base class uses dynamic memory allocations and redefine destructors, assignment functions, copy constructors, and derived classes do not use dynamic memory allocations, derived classes do not need to define display destructors, copy constructors, and assignment operators.

First, see if you need a destructor. If you do not define a destructor, the compiler defines a default destructor that does not perform any action. After the derived class destructor executes its own code, it calls the base class destructor.

Then look at the copy constructor. The default copy constructor performs member replication, but when a class member or inherited class component is copied, it is done using the copy constructor of the class.

The same is true for assignments. The default assignment operator for a class automatically assigns a base class component using the base class's assignment operator. Therefore, the default assignment operator is also appropriate.

26. When both the base and derived classes are in dynamic memory allocation, the destructor, copy constructor, and assignment operator functions of the derived class must be explicitly provided, and the base class element must be processed using the corresponding base class method. This requirement is met in three different ways. For destructors, this is done automatically; for constructors, this is done by calling the copy constructor of the base class in the initialization list; If you do not, the default constructor for the base class is called automatically. For assignment operators, this is done by explicitly invoking the assignment operator of the base class by using the scope resolution operator. P518

27. For a base class, you should provide a virtual destructor, even if it does not require a destructor.

28. Constructors, destructors, and assignment operators cannot be inherited.

29. You can assign a derived class object to a base class object, but at this point the assignment operator is responsible only for the base class member.

30. The answer to the question "whether a base class object can be assigned to a derived class object" is "maybe". If a derived class object contains a constructor that defines a base class object as a derived class object (a conversion constructor), you can assign the base class object to the derived class object. You can also do this if a derived class defines an assignment operator to assign a base class to a derived class object. If none of the above conditions are satisfied, you cannot do this unless you use explicit coercion of type conversions.

31. The friend function is not a class member and therefore cannot be inherited. However, you may want the friend function of the derived class to be able to use the friend function of the base class. To do this, you can convert a derived class reference or pointer to a base class reference or pointer by forcing the type conversion character, and then use the converted pointer or reference to call the base class's friend function.

32. A derived class method can invoke a public and protected base class method by using the scope resolution operator class.

C + + Primer Plus reading notes-13th Chapter class succession

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.