C ++ Object-Oriented Programming details,
C ++ object-oriented programming is designed in the C ++ language. When we use a base class reference or pointer to call a virtual function, dynamic binding will occur, that is to say, the polymorphism base class in JAVA usually defines a virtual destructor, even if the function does not perform any actual operations. In C ++, there are two types of basic class member functions: 1. The base class wants the function to be rewritten by the derived class; 2. The base class wants the derived class to inherit directly without changing the function. For the former, the base class defines it as a virtual function. When a pointer or reference calls a virtual function, the call is dynamically bound (that is, polymorphism ). Non-static functions other than constructors can be virtual functions. The keyword virtual can only appear before the function declaration statement inside the class, but not before the Function Definition Statement outside the class. If the base class calls a function as a virtual function, the function is implicitly a virtual function in the derived class. If a member function is not declared as a virtual function, its parsing process occurs during compilation rather than runtime. Therefore, in the hierarchy of inheritance, this function has only one and cannot be rewritten into multiple versions. The derived class must re-declare all the virtual functions in the base class. A derived class can be added with the keyword virtual before such a function, but this is not necessary. If the class Access Control Character in the derived list is public, we can bind an object of the derived type to a reference or pointer of the base class. Although the derived classes have data members of the base classes, they can only be initialized through the constructors of the base classes.
Class A {public: int a; A (int p): a (p) {}} class B: public A {public: int B; B (int p1, int p2): A (p1), B (p2) {}} // The constructor of B First initializes A by the constructor of a, then executes the constructor of A, and then initializes B, when the final declaration of the constructor body of B is executed, the Declaration contains the class name but does not contain its derived list class B: public A; // error class B; // correctIf a class is treated as a base class, the class must be defined rather than declared.
Class A; // only declares class A class B: public A {...} // error, class A must be definedType conversion from a derived class to a base class only applies to a pointer or a reference type. A base class does not have an implicit type conversion. When we use a derived class object to initialize or assign a value to a base class object, only the basic part of the object in this derived class will be copied, moved, and assigned, and its derived class part will be ignored. Therefore, the base class object cannot be forcibly converted to a derived class object.
Class A {} class B: public A {} B B; A a (B); // actually, A copy constructor is called, that is, :: A (const A &) a = B; // In fact, the copy assignment operator is called, that is, A: operator = (const &)List content