In C + + and its object-oriented theory, there is a scenario where a class inherits from another class, and if all two classes have a function name and the same member functions as the parameter and its return value, then the function of the subclass automatically hides the function corresponding to the parent class. The same name is hidden. In the process of development, we want the member function of the subclass and the member function of the parent class to be executed. Then you need to explicitly call a member function with the same name as the parent class in the member function of the subclass.
The following example shows a hidden concept with the same name and a method to avoid shadowing with the same name.
#include <iostream>using namespace Std;class baseclass{public:baseclass () {cout << baseclass::baseclass ( ) \ t "<< this << Endl;} ~baseclass () {cout << "baseclass::~baseclass () \ t" << this << Endl;} void ShowMsg () {cout << "baseclass::showmsg () \ t" << this << Endl;} void ShowMsg2 () {cout << "baseclass::showmsg2 () \ t" << this << Endl;}}; Class Subclassa:public Baseclass{public:subclassa () {cout << "Subclassa::subclassa () \ t" << this << Endl;} ~subclassa () {cout << "Subclassa::~subclassa () \ t" << this << Endl;} void ShowMsg () {baseclass::showmsg () cout << "subclassa::showmsg () \ t" << this << Endl;} void ShowMsg2 () {cout << "subclassa::showmsg2 () \ t" << this << Endl;}}; int main (int argc, char *argv[]) {cout << process begin at\t << (void*) &main << Endl; Subclassa subclassa;cout << "Hide test for base class members" << ENDL;SUBCLASSA.SHOWMSG2 () cout << "===== explicitly call test ======== for hidden base class members" << endl;subclassa.showmsg (); cout << "======end test=========" < < Endl;return 0;}
Log of the program:
Process begin at 0x401a65
Baseclass::baseclass () 0x62feee
Subclassa::subclassa () 0x62feee
Hide tests for base class members
SUBCLASSA::SHOWMSG2 () 0x62feee
===== a test that explicitly calls a hidden base class member ========
Baseclass::showmsg () 0x62feee
Subclassa::showmsg () 0x62feee
======end test=========
Subclassa::~subclassa () 0x62feee
Baseclass::~baseclass () 0x62feee
Hidden names and countermeasures in inheritance relationships in C + +