C + + Object-oriented programming

Source: Internet
Author: User
Tags class definition function prototype

  1. Object-oriented programming is based on three basic concepts: Data abstraction, inheritance, and dynamic binding. In C + +, you use classes for data abstraction, and class derivation to inherit another class from one class: Derived classes inherit members of the base class. Dynamic binding enables the compiler to decide at run time whether to use a function defined in a base class or a function defined in a derived class.
  2. In C + +, polymorphism is used only for references or pointers to types that are associated by inheritance
  3. In C + +, 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.
  4. Any non-static member function can be a virtual function in addition to a constructor function. Reserved words appear only in member function declarations inside the class and cannot be used on function definitions that occur outside the body of the class definition.
  5. Derived classes can access only the protected members of their base classes through derived class objects, and derived classes do not have special access rights to the protected members of their base class type objects.
  6. The declaration of a virtual function in a derived class must exactly match the definition in the base class, with one exception: a virtual function that returns a reference (or pointer) to the base class. A virtual function in a derived class can return a reference (or pointer) to a derived class of the type returned by the base class function.
  7. Once a function is declared as a virtual function in the base class, it is always a virtual function, and the derived class cannot change the fact that the function is a virtual function. When a derived class is redefining a virtual function, you can use the virtual reserved word, but it is not required.
  8. Because each derived class object has a base class part, the class can access the public and protected members of its base class as if those members were members of the derived class itself.
  9. A function call in C + + does not use dynamic binding by default. To trigger dynamic binding, two conditions must be met: first, only member functions that are specified as virtual functions can be dynamically bound, the member functions default to non-virtual functions, non-virtual functions are not dynamically bound, and second, function calls must be made through a reference or pointer to a base class type.
  10. A virtual function is determined at run time only by a reference or a pointer call. Only in these cases will the dynamic type of the object be known until run time.
  11. When a derived class virtual function calls the base class version, the use scope operator must be displayed. If the derived class function ignores this, the function call is determined at run time and will be called itself, resulting in infinite recursion.
  12. The public derived class inherits the interface of the base class, which has the same interface as the base class, in a well-designed class hierarchy, where objects of the public derived class can be used wherever the base class object is needed.
  13. Classes that are derived using private or protected do not inherit the interface of the base class, and instead, these derivations are often referred to as implementation inheritance. Derived classes use the inherited class in the implementation but the part that inherits the base class does not become part of its interface.
  14. Derived classes can restore the access level of an inherited member, but cannot make the access level more restrictive or looser than the original specified in the base class
  15. A defined class can be used as a base class. The reason for this limitation: Each derived class contains and can access the members of its base class, in order to use these members, the derived class must know what they are. This rule implies that a class cannot be derived from the class itself.
  16. If you need to declare a derived class, the declaration contains a declaration but does not contain a derived list. Like this is wrong: class A:class B;
  17. A non-virtual function is always determined at compile time based on the type of object, reference, or pointer that called the function
  18. In some cases, you want to override the virtual function mechanism and force a function call to use a specific version of a virtual function, you can use the scope operator, and only the code in the member function should overwrite the virtual function mechanism with the scope operator
  19. Friend relationships cannot inherit
  20. If the base class defines a static member, then there is only one such member in the entire inheritance hierarchy. No matter how many derived classes derive from the base class, there is only one instance per static member. Static members follow general access control: If a member is private in the base class, the derived class cannot access it. Assuming you can access members, you can access static members through the base class, or you can access static members through derived classes.
  21. If you have an object of a derived type, you can use its address to copy or initialize a pointer to a base class type. Similarly, a reference to a base class type can be initialized with a reference or object of a derived type. Strictly speaking, there is no similar conversion to objects. The compiler does not automatically convert a derived type object to a base class type object, but it is generally possible to copy or initialize a base class object using a derived type object (in effect, call the constructor when the function is initialized, call the assignment operator when the assignment is made, because there is a transformation in which the derived class refers to the base class reference. You can then call the copy constructor that has a (const) reference to the base class type directly.
  22. Like inherited member functions, conversions from derived classes to base classes may or may not be accessible. Whether the transformation is accessible depends on the access designator specified in the list of derived classes. In the case of public inheritance, both user code and descendant classes can use a transformation from a derived class to a base class. If the class is derived using private or protected inheritance, the user code cannot convert the derived type object to a base class object.
  23. An automatic conversion from a base class to a derived class does not exist. You cannot use a base class object when you need a derived class object, and sometimes even more surprisingly, conversions from the base class to the derived class also have limitations when the base class pointer or reference is actually bound to the derived class object. Because the compiler does not know whether the transformation is safe at run time, the compiler determines whether the conversion is legitimate and only looks at the static type.
  24. Constructors and replication control members cannot inherit, each class defines its own constructors and replication control members. Like any class, a composite version is used if the class does not define its own default constructors and replication control members.
  25. The constructors of a derived class are affected by the inheritance relationship, and each derived class constructor initializes the base class in addition to its own data member.
  26. The constructor initialization list provides the initial values for the class's base class and members, and it does not specify the order in which the initialization is performed. Initializes the base class first, and then initializes the members of the derived class according to the order of declaration
  27. If a derived class defines its own copy constructor, the copy constructor should generally show the base class portion of the object initialized with the base class copy constructor.
  28. The assignment operator is typically similar to a copy constructor: If a derived class defines its own assignment operator, the operator must display an assignment to the base class part.
  29. Destructors work differently than copy constructors and copy operators: derived class destructors are not responsible for revoking members of base class objects. 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 object's revocation order is reversed from the construction order: The derived class destructor is run first, and then the base class destructor is called up one time by the inheritance hierarchy
  30. The destructor for the automatic invocation of the base class part has an important effect on the design of the base class. When you delete a pointer to a dynamically allocated object, you need to run the destructor to clear the object before releasing the object's memory. When working with objects in an inheritance hierarchy, the static type of the pointer may be different from the dynamic type of the object being deleted, and the base class type pointer that actually points to the derived class object may be deleted. If you delete a base class pointer, you need to run the base class destructor and clear the members of the base class, and if the object is actually a derived type, the behavior is not defined. To ensure that the appropriate destructor is run, the destructor in the base class must be a virtual function, and if the destructor is a virtual function, then which destructor will be run by the pointer, depending on the type of object the pointer refers to. Like other virtual functions, the virtual function nature of the destructor is inherited. Therefore, if the destructor of the root class in the hierarchy is a virtual function, the derived class destructor will also be a virtual function, and the derived class destructor is a virtual function whether the derived class displays a definition destructor or a composition destructor. if a derived class object is assigned using new and is controlled by a pointer to its base class, it is often removed by a pointer to its base class (if the base class does not have a virtual destructor, the result will be indeterminate, and when it actually occurs, Destructors for derived classes are never called). If the base class has a virtual destructor, the destructor of the bottom-most derived class is called first, and then the destructor of each base class is called.
  31. Even if the destructor does not work, the root class of the inheritance hierarchy should also define a virtual destructor
  32. Each class maintains its own scope, and the name of the member is defined in the scope. In the case of inheritance, the scope of the derived class is nested within the scope of the base class. If the name cannot be determined in the scope of the derived class, the definition of the name is found in the perimeter base class scope. It is the hierarchical nesting of such scopes that allows us to directly access the members of the base class as if these members were members of a derived class.
  33. A derived class member with the same name as a base class member will mask direct access to the base class member
  34. A member function that uses a uniform name in a base class and a derived class behaves like a data member: A derived class member in a derived class scope masks a base class member. Base class members are masked even if the function prototype is different
  35. The first is the fact that, in the case of inheritance, the scope of the derived class is nested within the scope of the base class. Second, the functions declared in the local scope do not overload the functions defined in the global, and similarly, the functions defined in the derived class do not overload the members defined in the base class. When a function is called through a derived class object, the argument must match the version defined in the derived class, and the base class function is considered only if the derived class does not define the function at all.
  36. Like any other function, member functions (either virtual or non-virtual) can be overloaded. Derived classes can redefine 0 or more inherited versions, and if derived classes redefine overloaded members, only those members that are redefined in the derived class are accessible through derived types.
  37. If a derived class wants to use all of the overloaded versions by its own type, the derived class must redefine all overloaded versions, or none of them will be redefined. Sometimes a class needs to simply redefine the behavior of some versions of an overload set and want to inherit the meaning of other versions, in which case it can be tedious to redefine each base class version to redefine a version that needs to be specific. A derived class can provide a using declaration for an overloaded member without redefining each of the base class versions that it inherits. The grace of a using declaration gives you a name that cannot be specified, so that all overloaded instances of the function are added to the scope of the derived class, and then the derived class simply needs to redefine those functions that the type does have to define, and the other versions can be defined with inheritance.
  38. Defining a function as pure virtual indicates that the function provides an interface that can be overridden for descendant types, but the version in this class is never called. It is important that users will not be able to create objects that have a pure virtual function declaration class. This class is called an abstract base class.
  39. A handle class often needs to allocate a new copy of a well-known object without knowing the exact type of the object, and to implement a constructor that accepts an object of type X, you must first solve the problem: we do not know the actual type of the object given the constructor, he may be x, or it may be a subclass of X, in order to solve the problem The common approach is to define virtual operations for replication, which we call clone

C + + Object-oriented programming

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.