Derived class objects contain data members that inherit classes from the base class, and they form the "base class sub-object"
A private member in a base class that is not allowed to be accessed in a derived class member function, or to allow objects of derived classes to access them;
The true embodiment of the base class private, and the derived class does not open its permissions;
Public members in a base class:
If public inherits, it becomes a publicly member of the derived class and can be accessed in the derived class member function.
can also be accessed by the object of the derived class;
If the private inheritance method, it can only be accessed by the derived class member function, not by the object of the derived class;
#include <iostream>using namespacestd;classb{ Public: voidf () {cout<<"In b::f () ..."<<Endl; }};classD1: PublicB {};classD2:Privateb{ Public: voidg () {cout<<"in D2::g (), calling F () ..."<<Endl; f ();//in the case of private inheritance, the base class interface can be used in the subclass member function; }};intMain () {cout<<"In main () ..."<<Endl; D1 obj1; cout<<"calling Obj1.f () ..."<<Endl; D2 Obj2; cout<<"calling OBJ2.G () ..."<<Endl; OBJ2.G (); //error F () is a private inheritance, so obj2.f ()//The base class interface does not allow child class objects to be called;}
Overrides are different from overloading:
Function overloading requires that the function name must be the same, the function parameter requirements are different;
The function's rewrite is the function name and function parameters must be the same;
A base class has a defined member function that can be redefined in a derived class, which is called a function rewrite (override)
When an override occurs, other overloaded functions of the member function in the base class are masked and cannot be supplied to the derived class object;
You can use the using class name in a derived class: A member function name, and a derived class to restore the specified base class member functions (that is, to remove the mask) so that it can be reused;
#include <iostream>using namespacestd;classT {};classb{ Public: voidf () {cout<<"b::f () \ n"; } voidFinti) {cout<<"B::f ("<< I <<") \ n"; } voidFDoubled) {cout<<"B::f ("<< D <<") \ n"; } voidF (T) {cout<<"b::f (T) \ n"; }};classD1: Publicb{ Public: voidFinti) {cout<<"D1::f ("<< I <<") \ n"; }};intMain () {D1 D; D.F (Ten); D.F (4.9);//compile warning, automatic type conversion is performed//D.F ();//blocked, compile error//D.F (T ());//blocked, compile error//f () is overridden so that a member function with the same name in the base class cannot be accessed, and its member function is masked; return 0;}
If you want a member function in a derived class that has the same name as the base class, you can restore the member functions in the base class by doing the following:
The using B::f is added to the derived class;
class Public b{public: using b::f; use using base class Name:: function name; restore base class function void f (int"d1::f" ") \ n";}};
C + + Programming Method 3: function rewriting