The core of object-oriented is: Data abstraction, inheritance, dynamic binding
Data Abstraction : Separating the interface of a class from its implementation
inheritance : Defining a similarity relationship for a type to model
Dynamic binding : Ignores the differences of similar classes and uses their objects uniformly
15.2 base classes and derived classes
classBase { Public: Base ()=default; Base (intvalue): Value (value) {}Virtual intGet_value () {returnvalue;}; Virtual~base () =default;Private: intvalue;protected: voidChange_value () {value++; }};
A base class should define a virtual function so that when the delete a base-class pointer to a derived class object points to the space allocated by new, it can have an ideal destructor call (the destructor of the derived class is called first, and then the base class is called. Otherwise, only the destructor of the base class will be called).
class Derived Final: public base{public: default; Derived (int value): Base (value) {} intoverridereturn value*value;}};
The derived class overrides the virtual function of the base class, and you can use the Override keyword (which does not have to be the old standard), and this function of the derived class automatically becomes virtual, regardless of whether the virtual keyword is added.
Final indicates that the class cannot be inherited.
If you need to use the override base class member, you need to add the base class's scope base::
15.2.3 type conversions and inheritance
- A pointer or reference to a derived class can be implicitly converted to a pointer reference to a base class, or vice versa.
- There is no expected type conversion between the object of the derived class and the object of the base class, because when the derived class is assigned to the base class, the actual occurrence is the process of executing the copy constructor, and finally a new base class object is obtained.
15.3 virtual function/abstract base class
If we want to invoke the virtual function that is being override, we can use the scope operator to make the call.
If the virtual function = 0, it is a pure virtual function. Classes with pure virtual functions are abstract base classes. We cannot create an abstract base class object.
15.5 access Control and inheritance
Protect
Protect members are inaccessible to users of the class
Derived class protect inherits the base class, the public member of the base class Wi Cheng protect member exists in the derived class
A member of a derived class can access the protect member of the base class through the object of the derived class (because it inherits from 2, equivalent to its own protect member), and cannot access the protect member of the base class through the object of the base class (because 1)
Private
Private members are not accessible to users of the class. All members of the base class that are inherited by private will be private in the derived class.
The derived class public inherits between the base classes before the type conversion can be
To elevate the access level of a base class member
classBase { Public: std::size_t size ()Const{returnN;}protected: std::size_t N;};classDerived:PrivateBase { Public: //It was private to the user, but it was promoted to public. usingbase::size;protected: //originally for the user is private, but promoted to the protected usingbase::n;};15.6 scope of classes in inheritance
When a member accesses an object, the member is required first from its static scope. If it is not found, go to the outer base class to find it.
If a derived class defines a member with the same name as the base class, the members of the base class are hidden in the scope of the derived class. If you want to use it, you can call it through the scope of the base class.
15.7 Constructors and copy control
When a base class is a virtual destructor, the destructor for the derived class can be correctly executed when a pointer to a base class or a reference to a derived class object is used to delete (delete).
The virtual destructor of the base class (even =default) also blocks the move operation of the composition. (Destructors generally conflict with the move operation of a composition) so the derived class has no synthetic move operations, which should generally be redefined as follows:
classQuote { Public: Quote ()=default;//memberwise Default InitializeQuote (Constquote&) =default;//memberwise CopyQuote (quote&&) =default;//memberwise Copyquote&operator=(Constquote&) =default;//Copy Assignquote&operator= (quote&&) =default;//Move Assign Virtual~quote () =default; //Other members as before};15.7.4 inherited constructors
The default, copy, and move constructors will not be inherited.
classBase { Public: Base (intN) {cout <<"Base"<<Endl;}};classDerived Final: Publicbase{ Public: usingbase::base; //the inherited base class constructor above will be generated by the compiler with the following code//However, if the user is customized, the constructor of the base class is overwritten//Derived (int n): Base (n) {cout << "Derived" << Endl;}};
The 15th chapter object-oriented programming