One, the same name hidden
The same name is hidden, that is, in C + + inheritance, as long as the function name of the subclass is the same as the function name of the parent class, the function in the subclass hides all functions in the parent class with the same name as the member function of the child class .
Special attention:
Unlike the overloads of functions, this requires only the same name of the function, and does not require the same argument list for the function . A member function with the same name as the child class in the parent class but with a different argument list is also hidden.
Example:
1#include <iostream>2 using namespacestd;3 classfather{ // parent class 4 Public:5Father () =default;6Father (intv): value (v) {}7 voidShow () {8 cout<<" call the member function in the Father Class Show ()"<< Endl; 9 }Ten voidShowinta) { One cout<<" call the member function in the Father class Show (int)"<< Endl; A } - Private: - intvalue; the }; - - classSon: Publicfather{ // sub-class - Public: +Son () =default; -Son (intv): value (v) {} + voidShow () { // Method Show () and method Show (int) in parent class are hidden with the same name A cout<<" call member function in Son class show ()"<< Endl; at } - Private: - intvalue; - }; - - intMain () { in Son S; - s.show (); to //s.show (1); //Error: Method Show (int) is hidden by the same name + s.father::show (); the method of the parent class show () and method Show (int) does quilt class son inherits, but the same name method in the Quilt class son show () is hidden, cannot be explicitly called by the object of the subclass -S.father::show (1); the return 0; *}
Second, assignment compatibility rules
The so-called assignment compatibility rule, that is, in any place where a base class object is required, can be substituted with the object of the public derived class of that base class, which mainly includes the following situations:
? An object of a derived class can be assigned to an object of the base class, at which point the assignment is primarily to assign the sub-object of the corresponding base class contained in the derived class object to the base class object
Special attention:
In turn (assigning a base class object to a derived class object) is not possible because new members in the derived class object will have no value assignable
:
Example:
1#include <iostream>2 using namespacestd;3 classfather{//Parent Class4 Public:5Father () =default;6Father (intvalue): Father_value (value) {}7 voidShow () {8cout<<"The value of a member variable in the Father class is:"<<father_value<<Endl;9 }Ten Private: One intFather_value; A }; - - classSon: Publicfather{//sub-class the Public: -Son () =default; -Son (intvalue): Father (value), Son_value (value) {} - voidShow () { +cout<<"The value of the member variable in the Son class is:"<<son_value<<Endl; - } + Private: A intSon_value; at }; - - intMain () { -Father Father (Ten); -Son son ( -);Father=son;//Assign a derived class object to a base class object in father.show (); - return 0; to}
? You can assign the address of a derived class object to a pointer variable of its base class
Special attention:
1. You can access only hidden objects in the derived class that are inherited by the base class, and you cannot access new members in the derived class
2. The essence of this operation is to point the pointer to a hidden object in the derived class that is inherited by the base class
3. You cannot assign the address of a base class object to a pointer variable of a derived class
:
Example:
1#include <iostream>2 using namespacestd;3 classfather{//Parent Class4 Public:5Father () =default;6Father (intvalue): Father_value (value) {}7 voidShow () {8cout<<"The value of a member variable in the Father class is:"<<father_value<<Endl;9 }Ten Private: One intFather_value; A }; - - classSon: Publicfather{//sub-class the Public: -Son () =default; -Son (intvalue): Father (value), Son_value (value) {} - voidShow () { +cout<<"The value of the member variable in the Son class is:"<<son_value<<Endl; - } + voidNew_func () { Acout<<"call a member method in the Son class: New_func ()"<<Endl; at } - Private: - intSon_value; - }; - - intMain () { inSon son ( -);Father *ptr=&son;//assigns the address of the derived class object to a pointer to the base class type Ptr->show ();//ptr->new_func () ; Error: A hidden object in a derived class that inherits from a base class can be accessed only through this pointer , and new members in derived classes cannot be accessed - return 0; the}
? A derived class object can initialize a reference to a base class
Special attention:
1. Hidden objects in derived classes that are inherited by the base class can only be accessed through this reference, and new members in derived classes cannot be accessed
2. The essence of this operation is an alias for a hidden object in a derived class inherited from a base class
3. You cannot initialize a reference to a derived class with a base class object
:
Example:
1#include <iostream>2 using namespacestd;3 classfather{//Parent Class4 Public:5Father () =default;6Father (intvalue): Father_value (value) {}7 voidShow () {8cout<<"The value of a member variable in the Father class is:"<<father_value<<Endl;9 }Ten Private: One intFather_value; A }; - - classSon: Publicfather{//sub-class the Public: -Son () =default; -Son (intvalue): Father (value), Son_value (value) {} - voidShow () { +cout<<"The value of the member variable in the Son class is:"<<son_value<<Endl; - } + voidNew_func () { Acout<<"call a member method in the Son class: New_func ()"<<Endl; at } - Private: - intSon_value; - }; - - intMain () { inSon son ( -);Father &father=son;//Initializes a reference to the base class with a derived class of father.show ();//father.new_func ();//Error: Access can only be accessed through this reference in a derived class inherited from a base class To a hidden object that cannot access a new member in a derived class - return 0; the}
C + +: Hidden and assignment-compatible rules with the same name