C + + re-learning Path (v)

Source: Internet
Author: User
Tags class definition

1. Inheritance and dynamic binding simplify our programs in two ways

It is easier to define new classes that are similar but not identical to other classes, to make it easier to write programs that ignore the differences between these similar types P471

2. The type associated with inheritance is called a polymorphic type because in many cases the "many forms" of a derived type or base class type can be used interchangeably, and in C + +, polymorphism is used only for references or pointers to types that are associated by inheritance P472

3. The base class must indicate which functions are expected to be redefined by the derived class, and the function defined as virtual is that the base class expects the derived class to be redefined, and the base class expects that the function inherited by the derived class cannot be defined as a virtual function P472

4. Through dynamic binding we are able to write programs that use arbitrary types of objects in the inheritance hierarchy, without concern for the specific types of objects, and the programs that use these classes do not need to distinguish whether the function is defined in a base class or in a derived class P473

5. When a virtual function is called by a reference (or pointer) of a base class, a dynamic binding occurs, a reference (or pointer) can point either to a base class object or to a derived class object, which is the key to dynamic binding, which is determined at run time by a virtual function called by a reference (or pointer), a reference (or pointer) P473 defined by the actual type of the object being referred to

6. In addition to the constructor, any non-static member function can be a virtual function, and the reserved word virtual appears only in member function declarations inside the class and cannot be defined on a function that appears outside the class definition body P474

7. Like a public member, protected members can be accessed by derived class objects, and, like private members, protected members cannot be accessed by users of the class; Derived classes can access only the protected members of their base classes through derived class objects. Derived classes do not have special access rights to the protected members of their base class type objects P475

8. The inherited class must decide which parts of the implementation are declared as protected and which parts are declared private, that the members that you want to prohibit access to the derived class should be private, and that the members that provide the required operations or data for the derived class implement should be set to protected. An interface provided to a derived type is a combination of protected members and public members P479

9. Because each derived class object contains a base class part, you can bind a reference to a base class type to the base class part of a derived class object, or you can point to a derived class object with a pointer to the base class P479

double print_total (const item_base&*p = &10   = &bulk;  

10. Binding a reference or pointer to a base class type to a derived class object has no effect on the base class, there is no change in the object itself, it is still a derived class object, and the actual type of the object may be different from the static type of the object reference or pointer, which is the key to C + + dynamic binding P480

11. When a derived class virtual function calls the base class version, the scope operator must be explicitly used, and if the derived class function ignores this, the function call is determined at run time and will be a self-invocation, resulting in infinite recursion P481

12. Regardless of the access designator in the derived list, all classes inheriting base have the same access to the members in base, and the derived access designator controls the access of the user of the derived class to members inherited from base P483

13. A friend relationship cannot inherit, a friend of a base class has no special access to a member of a derived class, and if the base class is granted a friend relationship, only the base class has special access rights, and the derived class of the base class cannot access the class that grants the friend relationship 486

14. When you convert a derived class object to a base class type reference and pass the object to the function that you want to accept the reference to, the reference is bound directly to the object, although it appears that the object is being passed, that the actual argument is a reference to the object, that the object itself is not copied, and that the transformation does not alter the derived type Object The object is still a derived type Object , the base class part of the derived class type object is copied to the argument P488, when the derived class object is passed to a function that expects to receive a base class type object (rather than a reference), by initializing or assigning a base class object to the object.

15. A class can only initialize its own direct base class, which is the class specified in the derived list, and derived classes should respect the initialization intent of the base class by using the base class constructor, instead of assigning values to the members in the derived class constructor function body P492

16. Refactoring includes redefining the class hierarchy, moving operations and/or data from one class to another, and re-designing classes to accommodate the needs of the application to add new functions or handle other changes, most likely refactoring P492

C + + re-learning Path (v)

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.